zjsonpatch icon indicating copy to clipboard operation
zjsonpatch copied to clipboard

1.0 <> 1

Open boekhold opened this issue 8 years ago • 5 comments

If I compare the following 2 fragments:

expected:

{
    "value" : 1.0
}

test data:

{
    "value" : 1
}

zjsonpatch considers this different and generates a As far as I am aware, in JavaScript/JSON there is no distinction between integers and floats/doubles: they are all considered 'numbers'.

zjsonpatch should not generate a "replace" patch node for this.

boekhold avatar Aug 30 '16 06:08 boekhold

Agreed, Its data portability issue trickled down because of language implementation.

vishwakarma avatar Aug 30 '16 17:08 vishwakarma

any idea when can we get this fixed?

vinesh2011 avatar Dec 07 '16 14:12 vinesh2011

The problem is really in Jackson. If you have a Json document that has "field":"0", it is parsed internally as a java.lang.Integer. Similarly, "field":"0.0" is parsed as a Double. And JsonNode.equals() doesn't consider those to be the same, because it also compares the class.

In my case, the JSON I'm comparing is actually produced by org.json.JSONObject.toString(), which has the annoying habit of dropping the ".0" from "0.0", which (IMO) drops implicit type information for a value. If the original document author wrote "0.0", or the programmer used 'jsonObject.put("field", 0.0)', he clearly intended the field to represent a Double.

I actually just commented on an existing (closed) issue for org.json about this: https://github.com/stleary/JSON-java/issues/134

boekhold avatar Dec 08 '16 06:12 boekhold

Jackson Coreutils might be helpful here as it provides numeric equivalence.

pankaj-s avatar Jun 26 '17 18:06 pankaj-s

Jackson Coreutils might be helpful here as it provides numeric equivalence.

Note: jackson-coreutils moved under java-json-tools group ID: https://github.com/java-json-tools/jackson-coreutils

grimsa avatar Aug 24 '20 12:08 grimsa