zjsonpatch
zjsonpatch copied to clipboard
1.0 <> 1
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.
Agreed, Its data portability issue trickled down because of language implementation.
any idea when can we get this fixed?
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
Jackson Coreutils might be helpful here as it provides numeric equivalence.
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