Conversation
…in will result in a "-0" string. Based on ECMA-404 -0 is a valid number.
|
we specifically put in support for -0. Do you have a test case that shows the exact issue you are seeing? |
|
I was looking for test cases for the repo and could not find it, can you point me to the unit tests for this project. |
|
they are linked in the readme.md |
|
Here is the short test for this in my project. |
|
If I remember correctly, You can't use assertEquals for the double comparison. the get method returns an Object, in this case a Double. For comparing Doubles, you should really use Double.compare instead. for example in our test cases we have something like this: @Test
public void jsonObjectValues() {
String str =
"{"+
"\"negZeroKey\":-0.0,"+
"\"negZeroStrKey\":\"-0.0\""+
"}";
JSONObject jsonObject = new JSONObject(str);
assertTrue("opt negZeroKey should be a Double",
jsonObject.opt("negZeroKey") instanceof Double);
assertTrue("get negZeroKey should be a Double",
jsonObject.get("negZeroKey") instanceof Double);
assertTrue("opt negZeroKey should be double",
Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0);
assertTrue("opt negZeroStrKey with Default should be double",
Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
assertTrue("optNumber negZeroKey should return Double",
jsonObject.optNumber("negZeroKey") instanceof Double);
assertTrue("optNumber negZeroStrKey should return Double",
jsonObject.optNumber("negZeroStrKey") instanceof Double);
assertTrue("optNumber negZeroKey should be -0.0",
Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0);
assertTrue("optNumber negZeroStrKey should be -0.0",
Double.compare(jsonObject.optNumber("negZeroStrKey").doubleValue(), -0.0d) == 0);I trimmed out stuff that's not relevant to this case. All those tests pass with the current code. |
|
I stand corrected, this has been fixed, I must be on an old version. My version of the lib i was working with did not have |
When serializing JSONObject {"key": -0.0} to string and then back again will result in a "-0" string.
Based on ECMA-404 -0 is a valid number.