Last active
April 26, 2021 20:38
-
-
Save kshyju/54d3dd1ee196a4d6c5fd61794d70027a to your computer and use it in GitHub Desktop.
Tests for Custom JsonConverter to handle arrays in non valid format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class Person | |
{ | |
public string FullName { get; set; } | |
[JsonConverter(typeof(VehiclesArrayConverter))] | |
public List<Vehicle>? Vehicles { get; set; } | |
public string[] Aliases { get; set; } | |
} | |
public sealed class Vehicle | |
{ | |
public int Year { get; set; } | |
public string Model { get; set; } | |
} | |
[TestClass] | |
public class PersonTests | |
{ | |
readonly JsonSerializerOptions _options = new JsonSerializerOptions | |
{ | |
PropertyNameCaseInsensitive = true, | |
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | |
IgnoreNullValues = true | |
}; | |
[TestMethod] | |
public void Deserialization_Without_Vehicles_Property() | |
{ | |
// Arrange | |
string json = @"{ 'fullName':'Kramer' }".Replace("'","\""); | |
// Act | |
var actual = JsonSerializer.Deserialize<Person>(json, _options); | |
// Assert | |
Assert.AreEqual("Kramer",actual?.FullName); | |
Assert.IsNull(actual?.Vehicles); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_As_Null() | |
{ | |
// Arrange | |
string json = @"{ | |
'fullName':'Kramer', | |
'Vehicles': null | |
}" | |
.Replace("'","\""); | |
// Act | |
var actual = JsonSerializer.Deserialize<Person>(json, _options); | |
// Assert | |
Assert.AreEqual("Kramer",actual?.FullName); | |
Assert.IsNull(actual?.Vehicles); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_As_Proper_EmptyArray() | |
{ | |
string json = @"{ | |
'FullName':'Kramer', | |
'Vehicles': [] | |
}" | |
.Replace("'","\""); | |
var actual = System.Text.Json.JsonSerializer.Deserialize<Person>(json, _options); | |
Assert.AreEqual("Kramer",actual.FullName); | |
Assert.AreEqual(0, actual.Vehicles.Count()); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_As_Proper_Object_With_Type_and_Value() | |
{ | |
// Arrange | |
string json = @"{ | |
'fullName':'Kramer', | |
'Vehicles': { | |
'$type': 'System.Collections.Generic.List`1[[Vehicle, My.Project]], mscorlib', | |
'$values':[ | |
{ | |
'year': 2012, | |
'model': 'Accord' | |
}, | |
{ | |
'year': 2000, | |
'model': 'Altima' | |
} | |
] | |
} | |
}" | |
.Replace("'","\""); | |
// Act | |
var actual = JsonSerializer.Deserialize<Person>(json, _options); | |
// Assert | |
Assert.AreEqual("Kramer",actual?.FullName); | |
CollectionAssert.AreEqual(new[] {"Accord","Altima"},actual.Vehicles.Select(a=>a.Model).ToArray()); | |
CollectionAssert.AreEqual(new[] {2012,2000},actual.Vehicles.Select(a=>a.Year).ToArray()); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_And_Colors_As_Object_With_Type_and_Value() | |
{ | |
// Arrange | |
string json = @"{ | |
'fullName':'Kramer', | |
'Colors': { | |
'$type': 'System.Collections.Generic.List`1[[Color, My.Project]], mscorlib', | |
'$values':[ | |
{ | |
'hex': 'red' | |
}, | |
{ | |
'hex': 'yellow' | |
} | |
] | |
}, | |
'Vehicles': { | |
'$type': 'System.Collections.Generic.List`1[[Vehicle, My.Project]], mscorlib', | |
'$values':[ | |
{ | |
'year': 2012, | |
'model': 'Accord' | |
}, | |
{ | |
'year': 2000, | |
'model': 'Altima' | |
} | |
] | |
} | |
}" | |
.Replace("'","\""); | |
// Act | |
var actual = JsonSerializer.Deserialize<Person>(json, _options); | |
// Assert | |
Assert.AreEqual("Kramer",actual?.FullName); | |
CollectionAssert.AreEqual(new[] {"Accord","Altima"},actual.Vehicles.Select(a=>a.Model).ToArray()); | |
CollectionAssert.AreEqual(new[] {2012,2000},actual.Vehicles.Select(a=>a.Year).ToArray()); | |
CollectionAssert.AreEqual(new[] {"red","yellow"},actual.Colors.Select(c=>c.Hex).ToArray()); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_As_Proper_Array() | |
{ | |
string json = @"{ | |
'FullName':'Kramer', | |
'Vehicles': [ | |
{ | |
'year': 2012, | |
'model': 'Accord' | |
}, | |
{ | |
'year': 2000, | |
'model': 'Altima' | |
} | |
], | |
'aliases':['hello','there'] | |
}" | |
.Replace("'","\""); | |
var person = System.Text.Json.JsonSerializer.Deserialize<Person>(json, _options); | |
Assert.AreEqual(2,person.Vehicles.Count); | |
CollectionAssert.AreEqual(new[] {"Accord","Altima"},person.Vehicles.Select(a=>a.Model).ToArray()); | |
CollectionAssert.AreEqual(new[] {2012,2000},person.Vehicles.Select(a=>a.Year).ToArray()); | |
CollectionAssert.AreEqual(new[] {"hello","there"},person.Aliases); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_And_Colors_As_Proper_Array() | |
{ | |
string json = @"{ | |
'FullName':'Kramer', | |
'Vehicles': [ | |
{ | |
'year': 2012, | |
'model': 'Accord' | |
}, | |
{ | |
'year': 2000, | |
'model': 'Altima' | |
} | |
], | |
'Colors': [ | |
{ | |
'hex': 'green' | |
} | |
], | |
'aliases':['hello','there'] | |
}" | |
.Replace("'","\""); | |
var person = System.Text.Json.JsonSerializer.Deserialize<Person>(json, _options); | |
Assert.AreEqual(2,person.Vehicles.Count); | |
CollectionAssert.AreEqual(new[] {"Accord","Altima"},person.Vehicles.Select(a=>a.Model).ToArray()); | |
CollectionAssert.AreEqual(new[] {2012,2000},person.Vehicles.Select(a=>a.Year).ToArray()); | |
CollectionAssert.AreEqual(new[] {"hello","there"},person.Aliases); | |
CollectionAssert.AreEqual(new[] {"green"},person.Colors.Select(c=>c.Hex).ToArray()); | |
} | |
[TestMethod] | |
public void Deserialization_Vehicles_As_Proper_Array_2() | |
{ | |
string json = @"{ | |
'FullName':'Kramer', | |
'Vehicles': { | |
'$type': 'System.Collections.Generic.List`1[[Vehicle, My.Project]], mscorlib', | |
'$values':[ | |
{ | |
'year': 2012, | |
'model': 'Accord' | |
}, | |
{ | |
'year': 2000, | |
'model': 'Altima' | |
} | |
] | |
}, | |
'aliases':['hello','there'] | |
}" | |
.Replace("'","\""); | |
var person = System.Text.Json.JsonSerializer.Deserialize<Person>(json, _options); | |
Assert.AreEqual(2,person.Vehicles.Count); | |
CollectionAssert.AreEqual(new[] {"Accord","Altima"},person.Vehicles.Select(a=>a.Model).ToArray()); | |
CollectionAssert.AreEqual(new[] {2012,2000},person.Vehicles.Select(a=>a.Year).ToArray()); | |
CollectionAssert.AreEqual(new[] {"hello","there"},person.Aliases); | |
} | |
[TestMethod] | |
public void Serialization_Works() | |
{ | |
var value = new Person | |
{ | |
FullName = "Cosmo", Vehicles = new List<Vehicle>() | |
{ | |
new Vehicle() {Model = "Civic", Year = 2021}, | |
new Vehicle() {Model = "Accord", Year = 2020} | |
} | |
}; | |
string expectedJson = "{\"fullName\":\"Cosmo\",\"vehicles\":[{\"year\":2021,\"model\":\"Civic\"},{\"year\":2020,\"model\":\"Accord\"}]}"; | |
var actual = JsonSerializer.Serialize(value, _options); | |
Assert.AreEqual(expectedJson,actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment