public void CanConvertWorksForDynamicTypeWrapper(Type type, bool expected) { // Arrange DynamicTypeWrapperConverter converter = new DynamicTypeWrapperConverter(); // Act & Assert Assert.Equal(expected, converter.CanConvert(type)); }
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()); } }
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); } }
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); }