public void TryValidateObject_NoErrors_WithObjectDictionary() { // Arrange var parent = new Parent { PropertyA = 1, PropertyB = 1 }; var classWithDictionary = new ClassWithDictionary { Objects = new List <Dictionary <string, Child> > { new Dictionary <string, Child> { { "key", new Child { Parent = parent, PropertyA = 1, PropertyB = 2 } } } } }; var validationResults = new List <ValidationResult>(); // Act var result = _validator.TryValidateObjectRecursive(classWithDictionary, validationResults); // Assert Assert.True(result); Assert.Empty(validationResults); }
public void TestGenericDictionaryKeyObject() { var dictionary = new Dictionary <object, object>(); // Add multiple supported types. dictionary.Add(1, 1); dictionary.Add(new Guid("08314FA2-B1FE-4792-BCD1-6E62338AC7F3"), 2); dictionary.Add("KeyString", 3); dictionary.Add(MyEnum.Foo, 4); dictionary.Add(MyEnumFlags.Foo | MyEnumFlags.Bar, 5); const string expected = @"{""1"":1,""08314fa2-b1fe-4792-bcd1-6e62338ac7f3"":2,""KeyString"":3,""Foo"":4,""Foo, Bar"":5}"; string json = JsonSerializer.Serialize(dictionary); Assert.Equal(expected, json); // object type is not supported on deserialization. Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <Dictionary <object, object> >(json)); var @object = new ClassWithDictionary { Dictionary = dictionary }; json = JsonSerializer.Serialize(@object); Assert.Equal($@"{{""Dictionary"":{expected}}}", json); Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <ClassWithDictionary>(json)); }
public void Verify() { var instance = new ClassWithDictionary(); instance.Dict.Add("key", 123); var configuration = new ConfigurationContainer(); var serializer = configuration.Create().ForTesting(); serializer.Cycle(instance).Should().BeEquivalentTo(instance); }
public void VerifyOptimizedImplicitTyping() { var instance = new ClassWithDictionary(); instance.Dict.Add("key", 123); var configuration = new ConfigurationContainer().UseOptimizedNamespaces().EnableImplicitTyping(typeof(ClassWithDictionary)); var serializer = configuration.Create().ForTesting(); //serializer.WriteLine(instance); serializer.Cycle(instance).Should().BeEquivalentTo(instance); }
public void ClassWithDictionaryTest() { var source = new ClassWithDictionary { Dictionary = new Dictionary <string, object> { { "a key", "a value" } } }; using var stream = new MemoryStream(); BinaronConvert.Serialize(source, stream); stream.Seek(0, SeekOrigin.Begin); dynamic dest = BinaronConvert.Deserialize(stream); Assert.AreEqual("a value", ((IDictionary <string, object>)dest.Dictionary)["a key"]); }
public void Dictionary_properties_are_serialized_correctly() { var c = new ClassWithDictionary() { InterfaceDict = new Dictionary <string, int>() { ["test"] = 123, ["abc"] = 456 }, DictWithArray = new Dictionary <int, string[]>() { [1234] = new[] { "abc" }, [45678] = new[] { "def" }, } }; var result = Serializer.Serialize(c); Assert(() => result == "{ InterfaceDict = { [\"test\"] = 123, [\"abc\"] = 456 }, DictWithArray = { [1234] = [ \"abc\" ], [45678] = [ \"def\" ] } }"); }
public void TryValidateObject_object_with_dictionary_does_not_fail() { var classWithDictionary = new ClassWithDictionary { Objects = new List<Dictionary<string, Child>> { new Dictionary<string, Child> { { "key", new Child { PropertyA = 1, PropertyB = 2 } } } } }; var validationResults = new List<ValidationResult>(); var result = _validator.TryValidateObjectRecursive(classWithDictionary, validationResults); Assert.IsTrue(result); Assert.IsEmpty(validationResults); }