Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.getLong produces inconsistent results #332

Closed
rajasuba opened this issue Apr 27, 2017 · 4 comments
Closed

JSON.getLong produces inconsistent results #332

rajasuba opened this issue Apr 27, 2017 · 4 comments

Comments

@rajasuba
Copy link

rajasuba commented Apr 27, 2017

Json.getLong produces inconsistent results. Here is the following sample snippet which produces different results across different machine with the same JDK version and same JSON jar

        `String str = "26315000000253009";
	JSONObject json = new JSONObject();
	json.put("key", str);
	
	System.out.println("The value of json.get is  : " +json.get("key"));
	System.out.println("The value of json.getLong is : "+json.getLong("key"));
	System.out.println("The value of json.getString is : "+json.getString("key"));`

Different results

The value of json.get is : 26315000000253009
The value of json.getLong is : 26315000000253008
The value of json.getString is : 26315000000253009

The value of json.get is : 26315000000253009
The value of json.getLong is : 26315000000253009
The value of json.getString is : 26315000000253009

JSON version : 2.0
MD5Sum value of Json jar : 2f7f899f0766e65017744a4c4fc14d46
JDK Version : 1.8.0_92

What could be the reason for this inconsistent results? It would be great help if someone could help me know about this.

json.jar.zip
This is the json.jar which I'm using.

@johnjaylward
Copy link
Contributor

we don't have a version 2.0. All our versions are dates. Are you sure you are using this library?

@johnjaylward
Copy link
Contributor

johnjaylward commented Apr 27, 2017

I added a test for this that looks like this against our current code base:

    @Test
    public void testLongFromString(){
        String str = "26315000000253009";
        JSONObject json = new JSONObject();
        json.put("key", str);
        
        final Object actualKey = json.opt("key");
        assert str.equals(actualKey) : "Incorrect key value. Got " + actualKey
                + " expected " + str;
        
        final long actualLong = json.optLong("key");
        assert actualLong != 0 : "Unable to extract long value for string " + str;
        assert 26315000000253009L == actualLong : "Incorrect key value. Got "
                + actualLong + " expected " + str;

        final String actualString = json.optString("key");
        assert str.equals(actualString) : "Incorrect key value. Got "
                + actualString + " expected " + str;
    }

All the tests pass. I'm guessing you are using a different library.

@johnjaylward
Copy link
Contributor

You can see a list of our recent releases here:
https://github.com/stleary/JSON-java/releases

or from Maven here:
https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22

Please try updating to the latest version and if using Maven, Gradle, Ant, etc., ensure you have the proper group and artifact ids. Many people have forked this project over it's long life and some are not supported well.

@stleary
Copy link
Owner

stleary commented Apr 28, 2017

Sounds like this is resolved due to behavior not matching our released code. But let me know if you think it should be reopened.

@stleary stleary closed this as completed Apr 28, 2017
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

No branches or pull requests

3 participants