Closed masherak closed 2 years ago
Can you please give an example?
namespace: System.Text.Json
What framework do you use? What version of the JsonSerializer package?
Line
var deserializedValue1 = System.Text.Json.JsonSerializer.Deserialize<ExpandoObject>("JSON");
throws an exception because "JSON" isn't a valid JSON.
It is actual version of .NET 6
Yes "JSON" is really not valid JSON just replace it with some JSON with one field and you will se that it is not work.
Please provide working code example, result and result you expect.
var testObject1 = new
{
A = "ABC",
B = "CBA"
};
var testObject2 = new
{
A = "CBA",
B = "ABC"
};
var testJson1 = JsonSerializer.Serialize(testObject1);
var testJson2 = JsonSerializer.Serialize(testObject2);
Console.WriteLine($"testJson1: {testJson1}");
Console.WriteLine($"testJson2: {testJson2}");
var comparer = new Comparer(new ComparisonSettings { UseDefaultIfMemberNotExist = true });
var deserializedValue1 = JsonSerializer.Deserialize<ExpandoObject>(testJson1);
var deserializedValue2 = JsonSerializer.Deserialize<ExpandoObject>(testJson2);
comparer.Compare (deserializedValue1, deserializedValue2, out var configurationDifferences);
Console.WriteLine($"Count: {configurationDifferences.Count()}, Changes: {string.Join(',', configurationDifferences.Select(_ => $"{_.Value1} => {_.Value2}"))}");
With JsonSerializer you can see 0 changes, with JsonConvert it is work well.
Now I see the problem.
System.Text.Json.JsonSerializer.Deserialize
public JsonElement this[int index] { get; }
public JsonValueKind ValueKind { get; }
Only these 2 properties are used by comparer. It's why comparer says that objects are equal. As a solution, you can create a custom comparer for System.Text.Json.JsonElement and use Factory (https://github.com/ValeraT1982/ObjectsComparer#factory). I'll be considering fixing it in the next version.
Ok, thank you very much, have a nice day.
Helo, if I use deserialization method from class JsonSerializer and namespace: System.Text.Json so comparing end with zero differences everytime. If I use newtonsoft - JsonConvert it is working well.