A lightweight utility to compare JSON objects and hence practically any serialize-able entity. This utility comes with two distincrt object models, adhoc and detailed.
- Adhoc object model shows diffrences with "*" for changed properties "-" and "+" for removed and added ones respectively.
- Detailed object models groups changes in three properties: changed, added and removed.
This library uses Newtonsoft.Json as the underlying engine.
Note: Legacy version should work as they did before thanks to optional parameters added to the new functionalities.
Shows diffrences with "*" for changed properties "-" and "+" for removed and added ones respectively.
{
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
{
"name":"John",
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Audi",
"car4":"Jaguar"
}
}
var j1 = JToken.Parse(Read(json1));
var j2 = JToken.Parse(Read(json2));
var diff = JsonDifferentiator.Differentiate(j1,j2);
{
"-age": 30,
"*cars": {
"*car3": "Fiat",
"+car4": "Jaguar"
}
}
var j1 = JToken.Parse(Read(json1));
var j2 = JToken.Parse(Read(json2));
var diff = JsonDifferentiator.Differentiate(j1,j2, showOriginalValues: true);
{
"-age": 30,
"*cars": {
"*car3": "Audi",
"+car4": "Jaguar"
}
}
As mentioned this new object model groups changes in removed, changed and added properties in the result object recursively. Given the same json samples:
var j1 = JToken.Parse(Read(json1));
var j2 = JToken.Parse(Read(json2));
var diff = JsonDifferentiator.Differentiate(j1,j2, outputMode = OutputMode.Detailed);
{
"removed": {
"age": 30
},
"changed": {
"cars": {
"changed": {
"car3": "Fiat"
},
"added": {
"car4": "Jaguar"
}
}
}
}
var j1 = JToken.Parse(Read(json1));
var j2 = JToken.Parse(Read(json2));
var diff = JsonDifferentiator.Differentiate(j1,j2, outputMode = OutputMode.Detailed, showOriginalValues: true);
{
"removed": {
"age": 30
},
"changed": {
"cars": {
"changed": {
"car3": "Audi"
},
"added": {
"car4": "Jaguar"
}
}
}
}
var j1 = JToken.Parse(Read(json1));
var j2 = JToken.Parse(Read(json2));
var differentiator = new JsonDifferentiator(OutputMode outputMode: OutputMode.Symbol, showOriginalValues: false);
var diff = differentiator.Differentiate(j1,j2);
{
"removed": {
"age": 30
},
"changed": {
"cars": {
"changed": {
"car3": "Fiat"
},
"added": {
"car4": "Jaguar"
}
}
}
}