Quibble is a JSON comparison and diff tool for .NET that makes it easy to find differences between JSON objects.
This library is useful in multiple scenarios, where the ability to compare JSON structures can simplify the data validation process. For example, in the context of automated testing and debugging.
Features of Quibble:
- Deep Comparison: Performs a deep comparison of JSON objects, including arrays and nested objects.
- Flexible and Configurable: Allows customization of comparison rules and exclusion of specific properties.
- Detailed Reports: Generates detailed reports on the differences found between JSON objects.
- Integration in Testing: Ideal for integration into automated testing frameworks like xUnit or NUnit.
Installing Quibble
To add Quibble to your .NET project, use the NuGet package manager. Open the terminal or the package manager console in Visual Studio and run the following command:
Install-Package Quibble
Or through the NuGet interface in Visual Studio, search for Quibble
and install it in your project.
How to Use Quibble
Basic JSON Comparison
To start using Quibble, you first need to import the appropriate namespace and then use the JsonStrings.Diff(...)
method to compare JSON objects. Here is a basic example:
using System;
using Quibble;
class Program
{
static void Main(string[] args)
{
var json1 = @"{ ""name"": ""John"", ""age"": 30 }";
var json2 = @"{ ""name"": ""John"", ""age"": 31 }";
var differences = JsonStrings.Diff(json1, json2);
Console.WriteLine("Differences found:");
foreach (var difference in differences)
{
Console.WriteLine(difference);
}
}
}
In this example, JsonComparer
compares two JSON strings and shows the differences found. The result will include details about the properties that have different values.
Differences found:
ValueDiff { Path = $.age, Left = 30 (30), Right = 31 (31) }
Comparison of Complex JSON
Quibble handles more complex comparisons, including arrays and nested objects. Below shows how to compare more elaborate JSON objects:
using System;
using Quibble;
class Program
{
static void Main(string[] args)
{
var json1 = @"{
""person"": { ""name"": ""John"", ""age"": 30 },
""hobbies"": [""reading"", ""swimming""]
}";
var json2 = @"{
""person"": { ""name"": ""John"", ""age"": 31 },
""hobbies"": [""reading"", ""cycling""]
}";
var differences = JsonStrings.Diff(json1, json2);
Console.WriteLine("Differences found:");
foreach (var difference in differences)
{
Console.WriteLine(difference);
}
}
}
In this case, Quibble compares a JSON object with a more complex object that includes both nested properties and arrays, showing differences in both cases.
ValueDiff { Path = $.person.age, Left = 30 (30), Right = 31 (31) }
ValueDiff { Path = $.hobbies[1], Left = swimming, Right = cycling }
For more information and to explore the complete documentation, visit the official Quibble repository on GitHub. Here you will find additional examples, documentation, and resources.