Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Ignore element and child element order #231

Closed
tutufool opened this issue Jun 9, 2020 · 9 comments
Closed

Ignore element and child element order #231

tutufool opened this issue Jun 9, 2020 · 9 comments

Comments

@tutufool
Copy link

tutufool commented Jun 9, 2020

I'm wondering whether JsonUnit can meeting my requirement:
https://stackoverflow.com/questions/62277192/how-to-compare-2-json-by-ignoring-some-attributes-and-child-element-orders

By roughly go thru document, I found IGNORING_ARRAY_ORDER and IGNORING_VALUES might help in my case.

Yet I'm not sure whether I can get a "difference list" after applied those rules, rather than assertEquals.

@lukas-krecan
Copy link
Owner

Hi, you can take a look at DifferenceListener.

@tutufool
Copy link
Author

tutufool commented Jun 10, 2020

Could you help me take a look at:
https://github.com/tutufool/JsonUnit/blob/master/json-unit-core/src/test/java/net/javacrumbs/jsonunit/core/internal/DifferenceTest.java#L270

It seems not work for me, maybe I'm wrong at configuration.

I'm trying to ignore some attribute like 'timestamp' within the object in array, but I still need to check whether 'title' is same.

@lukas-krecan
Copy link
Owner

@tutufool
Copy link
Author

No lucky, and this is the config I used:

Configuration cfg = commonConfig().when(path("items.[*].timestamp"), thenIgnore()).when(path("items"), then(Option.IGNORING_ARRAY_ORDER))

source:

            {
		   "items":[
		      {
		         "id":222,
		         "title":"Test Item 222",
		         "timestamp":7654321
		      },
		      {
		         "id":111,
		         "title":"Test Item 111",
		         "timestamp":7654321
		      }
		   ]
		}

@lukas-krecan
Copy link
Owner

Can you please specify what you are trying to do, what do you expect to happen and what happens instead (as described in the issue template which you apparently ignored).

@tutufool
Copy link
Author

If you check my test case, you will find the test case failed for

https://github.com/tutufool/JsonUnit/blob/master/json-unit-core/src/test/java/net/javacrumbs/jsonunit/core/internal/DifferenceTest.java#L311

Take above json as example, I'd like to:

  1. ignore item order inside 'items'
  2. ignore attribute 'timestamp' when compare each item since I think it's a noise
  3. Do NOT ignore attribute 'title' when compare, since I think it's a real biz filed.

@tutufool
Copy link
Author

btw, this works if the 'timestamp' in the root level:
https://github.com/tutufool/JsonUnit/blob/master/json-unit-core/src/test/java/net/javacrumbs/jsonunit/core/internal/DifferenceTest.java#L226

But I'm no lucky to ignore it when it's in node of an array.

@lukas-krecan
Copy link
Owner

If I remember it correctly, the dot in .[ should not be there

@tutufool
Copy link
Author

Ah... my fault, this one works:

commonConfig().when(path("items[*].timestamp"), thenIgnore()).when(path("items"), then(Option.IGNORING_ARRAY_ORDER));

Thanks a lot !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants