I want to migrate my code from Newtonsoft Json.Net to Microsoft standard System.Text.Json. But I could not find an alternative for JToken.DeepEqual
Basically the code must compare two JSON in unit test. Reference JSON, and Result JSON. I used the mechanism in Newtonsoft to create two JObject and then compare them with JToken.DeepEqual. Here is the example code:
[TestMethod]
public void ExampleUnitTes()
{
string resultJson = TestedUnit.TestedMethod();
string referenceJson =
@"
{
...bla bla bla...
...some JSON Content...
...bla bla bla...
}";
JObject expected = ( JObject )JsonConvert.DeserializeObject( referenceJson );
JObject result = ( JObject )JsonConvert.DeserializeObject( resultJson );
Assert.IsTrue( JToken.DeepEquals( result, expected ) );
}
If I am correct the Newtonsoft JObject similar in System.Text.Json.JsonDocument, and I am able to create it, just I don't know how to compare the contents of it.
System.Text.Json.JsonDocument expectedDoc = System.Text.Json.JsonDocument.Parse( referenceJson );
System.Text.Json.JsonDocument resultDoc = System.Text.Json.JsonDocument.Parse( json );
Compare???( expectedDoc, resulDoc );
Of course, string compare is not a solution, because the format of the JSON doesn't matter and the order of the properties also doesn't matter.