Ejemplo n.º 1
0
        public void CreateConverterWorksForDynamicTypeWrapper(Type type, Type expected)
        {
            // Arrange
            JsonSerializerOptions       options   = new JsonSerializerOptions();
            DynamicTypeWrapperConverter converter = new DynamicTypeWrapperConverter();

            // Act
            JsonConverter typeConverter = converter.CreateConverter(type, options);

            // Assert
            if (expected == null)
            {
                Assert.Null(typeConverter);
            }
            else
            {
                Assert.Equal(expected, typeConverter.GetType());
            }
        }
Ejemplo n.º 2
0
        internal static void TestDynamicTypeWrapperConverterRead <T>() where T : GroupByWrapper
        {
            // Arrange
            JsonSerializerOptions       options       = new JsonSerializerOptions();
            DynamicTypeWrapperConverter converter     = new DynamicTypeWrapperConverter();
            JsonConverter <T>           typeConverter = converter.CreateConverter(typeof(T), options) as JsonConverter <T>;

            try
            {
                // Act
                ReadOnlySpan <byte> jsonReadOnlySpan = Encoding.UTF8.GetBytes("any");
                Utf8JsonReader      reader           = new Utf8JsonReader(jsonReadOnlySpan);
                typeConverter.Read(ref reader, typeof(AggregationWrapper), options);
            }
            catch (NotImplementedException ex)
            {
                // Assert
                Assert.Equal($"'{typeof(T).Name}' is internal and should never be deserialized into.", ex.Message);
            }
        }
Ejemplo n.º 3
0
        internal static void TestDynamicTypeWrapperConverterWrite <T>() where T : GroupByWrapper
        {
            // Arrange
            T wrapper = (T)Activator.CreateInstance(typeof(T));

            wrapper.GroupByContainer = new AggregationPropertyContainer()
            {
                Name  = "TestProp",
                Value = "TestValue"
            };

            JsonSerializerOptions       options       = new JsonSerializerOptions();
            DynamicTypeWrapperConverter converter     = new DynamicTypeWrapperConverter();
            JsonConverter <T>           typeConverter = converter.CreateConverter(typeof(T), options) as JsonConverter <T>;

            // Act
            string json = SerializeUtils.SerializeAsJson(jsonWriter => typeConverter.Write(jsonWriter, wrapper, options));

            // Assert
            Assert.Equal("{\"TestProp\":\"TestValue\"}", json);
        }