Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@mmilkin
Copy link

@mmilkin mmilkin commented Oct 2, 2018

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.

…in will result in a "-0" string.

Based on ECMA-404 -0 is a valid number.
@johnjaylward
Copy link
Contributor

we specifically put in support for -0.
#188

Do you have a test case that shows the exact issue you are seeing?

@mmilkin
Copy link
Author

mmilkin commented Oct 2, 2018

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.

@johnjaylward
Copy link
Contributor

they are linked in the readme.md

@mmilkin
Copy link
Author

mmilkin commented Oct 2, 2018

Here is the short test for this in my project.

JSONObject jsonData = new JSONObject()
jsonData.put("key", -0.0);

String serialized = jsonData.toString();
Assert.assertEquals(-0.0, new JSONObject(serialized).get("key"));

@johnjaylward
Copy link
Contributor

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.

@mmilkin
Copy link
Author

mmilkin commented Oct 2, 2018

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

    protected static boolean isDecimalNotation(final String val) {
        return val.indexOf('.') > -1 || val.indexOf('e') > -1
                || val.indexOf('E') > -1 || "-0".equals(val);
    }

@mmilkin mmilkin closed this Oct 2, 2018
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

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.