public void TestCircular() { var obj = new Parent { Id = 1 }; obj.Children = new List <Child> { new Child { Id = 1, Parent = obj }, new Child { Id = 2, Parent = obj }, }; var actual = SafeJsonSerializer.Serialize(obj); var expected = JsonSerializer.Serialize( JsonSerializer.Deserialize <Parent> (File.ReadAllText("StaticSerializeTests/TestCircular/0.json")), new JsonSerializerOptions { WriteIndented = true, IgnoreNullValues = true }); Assert.Equal(expected, actual); }
public void TestTextOrderedEnumerable() { persons[0].Id = 3; var ps = new Person[] { persons[0], persons[1] }; var ps2 = new Person[] { persons[1], persons[0] }; var actual = SafeJsonSerializer.Serialize(ps, 99, true, null, true); var expected = JsonSerializer.Serialize(ps2, new JsonSerializerOptions { WriteIndented = true }); Assert.Equal(expected, actual); }
public void TestIOrderedEnumerable() { var ps = new Dictionary <string, Person>() { { "Moe", persons[0] }, { "Larry", persons[1] }, }; var orderedPersons = ps.OrderBy(x => x.Key); var actual = SafeJsonSerializer.Serialize(orderedPersons); var expected = JsonSerializer.Serialize(orderedPersons, new JsonSerializerOptions { WriteIndented = true }); Assert.Equal(expected, actual); }
public void TestComplexObject() { var personA = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[0])); var personB = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[0])); var personC = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[1])); var personD = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[1])); var personE = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[0])); var personF = JsonSerializer.Deserialize <Person>(JsonSerializer.Serialize(persons[1])); var co = new ComplexObject() { PersonDict = new Dictionary <string, Person> { { "Moe", personA }, { "Larry", personC } }, Persons = new Person[] { persons[0], persons[1] }, PersonDicts = new List <Dictionary <string, Person> >() { new Dictionary <string, Person> { { "Moe", personB }, { "Larry", personD } } }, PersonDictOrdered = (new Dictionary <string, Person> { { "Moe", personE }, { "Larry", personF } }).OrderBy(x => x.Key) }; var actual = SafeJsonSerializer.Serialize(co); var expected = JsonSerializer.Serialize(co, new JsonSerializerOptions { WriteIndented = true, IgnoreNullValues = true }); if (actual != expected) { var fsc = FileStringComparer.GetSideBySideFileStrings(expected, actual, "EXPECTED", "ACTUAL"); _output.WriteLine(fsc); } Assert.Equal(expected, actual); }
public void TestMaxDepth(int testCase) { var actual = SafeJsonSerializer.Serialize(aRecs, testCase); var expected = JsonSerializer.Serialize( JsonSerializer.Deserialize <A[]> (File.ReadAllText($"StaticSerializeTests/TestMaxDepth/{testCase}.json")), new JsonSerializerOptions { WriteIndented = true, IgnoreNullValues = true }); if (actual != expected) { var fsc = FileStringComparer.GetSideBySideFileStrings(expected, actual, "EXPECTED", "ACTUAL"); _output.WriteLine(fsc); } Assert.Equal(expected, actual); }
public void TestPropertiesToIgnore(int testCase) { var actual = SafeJsonSerializer.Serialize(persons, 99, true, propertiesToIgnore[testCase]); var expected = JsonSerializer.Serialize( JsonSerializer.Deserialize <List <Person> > (File.ReadAllText($"StaticSerializeTests/TestPropertiesToIgnore/{testCase}.json")), new JsonSerializerOptions { WriteIndented = true, IgnoreNullValues = true }); if (actual != expected) { var fsc = FileStringComparer.GetSideBySideFileStrings(expected, actual, "EXPECTED", "ACTUAL"); _output.WriteLine(fsc); } Assert.Equal(expected, actual); }