Skip to content

silentorbit/JsonDiff

Repository files navigation

JsonDiff

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.

JSON format

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.

Read more...

Code Example

See examples in Demo

Features

Assign attributes to control the source generation.

Class Attribute

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.

Property Attributes

Installing

Two options: NuGet package or from source.

NuGet

Install NuGet package: SilentOrbit.JsonDiff

Sample: Demo.NuGet on GitHub

Source

Clone JsonDiff on GitHub

Sample: Demo.Source on GitHub (Same Repo)

Configurations

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

About

Track changes with support for JSON serialization

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published