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

org.json.JSONException: JSON does not allow non-finite numbers #385

Closed
sivaramansv opened this issue Nov 28, 2017 · 7 comments
Closed

org.json.JSONException: JSON does not allow non-finite numbers #385

sivaramansv opened this issue Nov 28, 2017 · 7 comments

Comments

@sivaramansv
Copy link

sivaramansv commented Nov 28, 2017

I have the following section in my XML document:
<dataId dt:dt="string">a98b174b86b54d7bb007d665382821d3</dataId>
and I am using the jar file json-20171018 for converting the XML document to JSON.

I am getting the following error:
org.json.JSONException: JSON does not allow non-finite numbers.
at org.json.JSONObject.testValidity(JSONObject.java:1261)

It has been told that it has been fixed, but I am getting the same error. Is there any workaround for this?
#27

@johnjaylward
Copy link
Contributor

this test passes:

    /**
     * see https://github.com/stleary/JSON-java/issues/385
     */
    @Test
    public void testNumberConversion() {
        String xmlStr = "<dataId dt:dt=\"string\">a98b174b86b54d7bb007d665382821d3</dataId>";
        JSONObject jsonObject = XML.toJSONObject(xmlStr);
        assertTrue("jsonObject should not be empty", jsonObject.length() != 0);
        assertNotNull(jsonObject.optJSONObject("dataId"));
        assertEquals("string",jsonObject.getJSONObject("dataId").optString("dt:dt"));
        assertEquals("a98b174b86b54d7bb007d665382821d3",jsonObject.getJSONObject("dataId")
                .optString("content"));
    }

Are you sure that is the section of your document that is causing the problem?

@sivaramansv
Copy link
Author

sivaramansv commented Nov 28, 2017

Yes, you are right. It works with this string. Do you know what kind of input might possibly throw this exception? I have about 1 million XML files that have these kinds of strings. I am finding it hard to tell what is causing this issue.

@johnjaylward
Copy link
Contributor

johnjaylward commented Nov 28, 2017

It would be something that causes under or overflow of a double. So some string that looks like:
98623497634589798e134987345983697862354987

To parse all values as strings and post-process them, you should install call the XML parser like this:

    /**
     * see https://github.com/stleary/JSON-java/issues/385
     */
    @Test
    public void testNumberConversion2() {
        String xmlStr = "<dataId dt:dt=\"string\">a98b174b86b54d7bb007d665382821d3</dataId>";
        JSONObject jsonObject = XML.toJSONObject(xmlStr, true);
        assertTrue("jsonObject should not be empty", jsonObject.length() != 0);
        assertNotNull(jsonObject.optJSONObject("dataId"));
        assertEquals("string",jsonObject.getJSONObject("dataId").optString("dt:dt"));
        assertEquals("a98b174b86b54d7bb007d665382821d3",jsonObject.getJSONObject("dataId")
                .optString("content"));
    }

note the second parameter is true. This indicates to the parser to not try to convert values found in the XML files into booleans or numbers. It will instead keep the raw strings. So if you have an XML like this:

<root>
   <data>5</data>
   <data>true</data>
</root>

your processed JSON would look like:

{
 "root" : {
    "data" : [{
      "content" : "5"
      }, {
      "content" : "true"
    }]
  }
}

@sivaramansv
Copy link
Author

sivaramansv commented Nov 28, 2017

Following is my code snippet
import org.json.XML;
public String convert(Text input) {
if (input != null) {
return input != null ? XML.toJSONObject(input.toString(), true).toString() : null;
}
return input;
}

Following is my stack trace:
java.lang.NoSuchMethodError: org.json.XML.toJSONObject(Ljava/lang/String;Z)Lorg/json/JSONObject;
at com.intel.mfg.oasys.OasysTextJsonConverter.convert(OasysTextJsonConverter.java:21)
at com.intel.mfg.oasys.OasysTextJsonConverter.convert(OasysTextJsonConverter.java:1

Do you know which library or version of jar file I need to use?

@johnjaylward
Copy link
Contributor

This has been in the library since the 20160807 release. Did you compile your own version, or did you download from Maven?

The maven builds are located here: https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22

Also make sure that no other dependencies in your project have an older copy of the JSON library.

@sivaramansv
Copy link
Author

Yes, it looks like I had an older copy of the JSON present in the project. So, I updated it to the latest version of jar file : json-20171018.jar and it worked!! Thank you.

@johnjaylward
Copy link
Contributor

you are welcome.

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

2 participants