Skip to content

Exclude properties from diff #29

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
tomasbruckner opened this issue Aug 19, 2022 · 7 comments · Fixed by #30
Closed

Exclude properties from diff #29

tomasbruckner opened this issue Aug 19, 2022 · 7 comments · Fixed by #30
Labels
enhancement New feature or request in-pr Issues already have an associated pull request next Features or bug fixes that currently being worked on and should be released next
Milestone

Comments

@tomasbruckner
Copy link

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
Copy link
Owner

weichch commented Aug 21, 2022

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.

@weichch weichch added enhancement New feature or request question Further information is requested next Features or bug fixes that currently being worked on and should be released next labels Aug 21, 2022
@weichch weichch added this to the v1 milestone Aug 21, 2022
@WojciechNagorski
Copy link

@weichch This would be great to have PropertyFilter.

@WojciechNagorski
Copy link

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
Copy link
Owner

weichch commented Sep 2, 2022

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
Copy link
Author

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

@weichch
Copy link
Owner

weichch commented Sep 12, 2022

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
Copy link

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

@weichch weichch added in-pr Issues already have an associated pull request and removed question Further information is requested labels Sep 13, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request in-pr Issues already have an associated pull request next Features or bug fixes that currently being worked on and should be released next
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants