Calculate difference between objects of the same type.
The difference is presented in a generated MyClass.Diff class that can be serialized with System.Text.Json.
The goal is to have a compact and readable format.
The serialized format is similar to JSON Merge Patch but with some significant differences.
- JsonDiff is not working with generic JSON documents, its strongly typed to the classes marked up.
- JsonDiff differentiates between full and partial object replace. This is seen in the JSON format where partial replacements are prefixed with "^".
Example: A is completly replaced by the new values.
{
"A": { "B": 3, "C": 4 }
}
Example: A is partially modified, C is replaced by 4 and B is not modified.
{
"^A": { "C": 4 }
}
Further on other symbol prefixes may be used for other operations not yet supported. For example list or substring manipulation.
See examples in Demo
Assign attributes to control the source generation.
Assign this to every class that will be compared on a per property level. Classes missing this will be treated as "immutable", any change will be indicated by a complete copy.
- [JsonDiffAlways]
- [JsonDiffFullCloneAttribute]
- [JsonDiffIgnoreAttribute]
- [JsonDiffImmutableAttribute]
Two options: NuGet package or from source.
Install NuGet package: SilentOrbit.JsonDiff
Sample: Demo.NuGet on GitHub
Clone JsonDiff on GitHub
Sample: Demo.Source on GitHub (Same Repo)
Demo.csproj has 3 extra configurations. The purpose of these are to enable one time source generation.
Debug and Release behaves as normal, using the source generator.
GenerateSource Generates the source and saves it inside the project, allowing you to save the source code in git.
The saved source code is only used in DebugGenerated and ReleaseGenerated.
You may choose to copy the configuration from these into your project, allowing one time code generation.
| Configuration | Source Generation | Save to "Generated" | Build |
|---|---|---|---|
| Debug | Yes | No | Newly generated |
| Release | Yes | No | Newly generated |
| GenerateSource (Debug) | Yes | Yes | Newly generated |
| DebugGenerated | No | No | Previously generated |
| ReleaseGenerated | No | No | Previously generated |