deblockt / json-diff Goto Github PK
View Code? Open in Web Editor NEWA library to generate a json diff on java
License: Apache License 2.0
A library to generate a json diff on java
License: Apache License 2.0
Can be usefull to add a the possibility in LenientJsonArrayPartialMatcher to ignore difference between null array, array field not exist or empty array.
So this three example become the same
{
name: "example"
array : null
}
{
name: "example"
array : []
}
{
name: "example"
}
Steps to reproduce
Run the Sample.java with
Test case 1
expected - {}
actual - {}
Test case 2
expected - { "a": {} }
actual - { "a": {} }
NPE will be thrown as below
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "diff" is null at com.deblock.jsondiff.viewer.PatchDiffViewer.toDiff(PatchDiffViewer.java:153) at com.deblock.jsondiff.viewer.PatchDiffViewer.toString(PatchDiffViewer.java:98) at java.base/java.lang.String.valueOf(String.java:4215) at java.base/java.io.PrintStream.println(PrintStream.java:1047) at com.deblock.jsondiff.Sample.main(Sample.java:34)
val org = """{"d":2}"""
val new = """{"d":2, "e": {"f":"g"}}"""
val matcher = CompositeJsonMatcher(
LenientJsonArrayPartialMatcher(),
LenientJsonObjectPartialMatcher(),
StrictPrimitivePartialMatcher(),
)
val jsonDiff = DiffGenerator.diff(org, new, matcher)
val patch = PatchDiffViewer.from(jsonDiff)
Neither the PatchDiffViewer
nor the OnlyErrorDiffViewer
detect any difference. I'm having a look to see if I can find the cause.
Steps to reproduce :
Use 0.0.8 version of the library.
Use
new LenientJsonArrayPartialMatcher(), // comparing array using lenient mode (ignore array order and extra items)
new LenientJsonObjectPartialMatcher(), // comparing object using lenient mode (ignoring extra properties)
new LenientNumberPrimitivePartialMatcher(new StrictPrimitivePartialMatcher()) // comparing primitive types and manage numbers (100.00 == 100)
);
Pass the below json for both expected and actual,
{ "a":[] }
Print the similarity score, expected 100, actual NaN
I'm using json-diff to diff quite large files, and need a variation of the OnlyErrorDiffViewer
. Having a text node value change caused nothing in the OnlyErrorDiffViewer
. The PatchDiffViewer
however showed that the change was detected.
Some sleuthing showed that the hard-coded >= 99.9
in JsonObjectDiff:display
is the culprit. Changing this to == 100.0
fixed my issue (my diff gave a similarityRate
of 99.97).
I think the 99.9 is wrong. For this kind of work, it should just be a simple == 100.0
. There's no need to be careful with floating point comparisons, because two json strings that represent the same floating point value will always have the same in-memory representation. Any change whatsoever will cause the result to become different from 100.
Thanks for the library, with the patch it's very useful for us at strmprivacy.io for tracking changes in large documents.
Hello, I recently discovered your JSON comparison implementation, and it appears to be the most suitable one for my specific use case among the open-source options I explored. While playing with some examples, I encountered this scenario,
The scenario,
expected
[ { "a": 1, "d": 5 }, { "a": 1, "b": 5 } ]
actual
[ { "a": 1, "d": 2 }, { "a": 1, "b": 3 } ]
The result is,
The property "$.0.d" didn't match. Expected 5, Received: 2
The property "$.1.b" didn't match. Expected 5, Received: 3
Same scenario when order of actual is changed,
expected : same as above
actual :
[ { "a": 1, "b": 3 }, { "a": 1, "d": 2 } ]
The result is
The property "$.0.d" in the expected json is not found
The property "$.1.b" in the expected json is not found
Dependancy on order of actual for the comparison seems undesirable to me. Thoughts?
The OnlyErrorDiffViewer provides all the mismatch details between the expected and actual json as string in certain format. As a end user, i may want it in a different format than what's provided. Is it possible to expose a method that can fetch all the details displayed as an object so that it can be used as per our use case.
If something close to the below is returned it would be useful,
The mismatched errors could be a list of object, each object can have 3 string data members - path, expectedVal, actualVal
The not found in expected errors could be a list of string paths
The not found in actual errors could be a list of string paths
We need to setup the ci part of the project to:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.