weichch / system-text-json-jsondiffpatch

High-performance, low-allocating JSON object diff and patch extension for System.Text.Json. Support generating patch document in RFC 6902 JSON Patch format.
MIT License
102 stars 13 forks source link

Exclude properties from diff #29

Closed tomasbruckner closed 1 year ago

tomasbruckner commented 2 years ago

Hi, is there a way to ignore properties from diff? The JsonDiffPatch does it here https://github.com/wbish/jsondiffpatch.net/blob/master/Src/JsonDiffPatchDotNet/JsonDiffPatch.cs#L332

What I am missing in the JsonDiffPatch is that it only allows you to exclude paths instead of properties which is a problem with recursive objects or arrays. To ignore property foo in an array of objects

{ "bar" : [ { "foo": 1 }, { "foo": 2 }, { "foo": 3} ] }

you would have to do

exclude paths: "bar.arr[0].foo", "bar.arr[1].foo", "bar.arr[2].foo"

This is useful in some scenarios, but it would be amazing if there would also be an option to exclude properties by their name

exclude properties: "foo"

The JsonDiffPatch looks abandoned so I am looking for an alternative that would support this scenario.

Is it something doable?

weichch commented 2 years ago

Ignoring properties is currently not supported, but I do have plan to add this feature (hopefully next version), and it shouldn't be hard to do.

In a nutshell, I was planning to add a property filter like jsondiffpatch has here in JsonDiffOptions:

public Func<string, Context, bool>? PropertyFilter { get; set; }

A custom filter can get the property name and the diff context and return a boolean value to indicate whether to ignore the property.

I'm re-considering / restructuring the diff engine at the moment given my next priority is adding some support to JsonDocument diff. It will make clear sense to me how the Context can be defined to support both JsonNode and JsonDocument after this work.

WojciechNagorski commented 2 years ago

@weichch This would be great to have PropertyFilter.

WojciechNagorski commented 2 years ago

Any progress on PropertyFilter. It block me before using SystemTextJson.JsonDiffPatch and event entire System.Text.Json. Maybe I can help you with this feature?

weichch commented 2 years ago

Hi @WojciechNagorski

I actually prioritized this a little bit because it will be taking a bit longer to add JsonDocument diff support than I thought.

I've done the changes locally, which I pushed it up just now #30, and released a preview version. I still need to add some tests in order to release a stable version.

Do both of you and @tomasbruckner mind providing some test cases you'd like the property filter to cover?

tomasbruckner commented 2 years ago

@weichch that's great to hear! Thanks for asking, it is very considerate! For me, the example from the first post is sufficient.

weichch commented 2 years ago

Seems like there is a quirk in array diff where the property filter is not applied to array items, because the the comparison is done using DeepEquals not Diff, though I should probably force it to use Diff when PropertyFilter is set.

I tested jsondiffpatch also has the same issue. JsonDiffPatch.Net works fine because it always does match-by-position on array items that are objects. The workaround (if I can't find an effective way to fix this issue) is setting ArrayObjectItemMatchByPosition to true.

WojciechNagorski commented 2 years ago

For me it can works in the same way like in JsonDiffPatch.Net