Skip to content

Conversation

@yaccob
Copy link
Contributor

@yaccob yaccob commented Nov 14, 2012

To introduce the opportunity to avoid implicit type conversion I have overloaded the parse method and added a new toJsonML(String) and a new toJsonML(XMLTokener) method using the overloaded parse method.
The changes have no impact on previously existing public methods - those should behave exactly as before.

One of the problems with the implicit type conversion is that xml data converted to json cannot be reverted to the original xml. Actually all non-string Json values are completely lost. This may be considered to be a separate problem of JSON.toString(JSONArray) which I did not try to solve directly, because for xml data converted using the new JsonML operations it is not relevant.

The following test case (groovy) illustrates the problems (testToJSONArray_*) and the solution (testToJsonML):

import org.json.JSONML
import org.junit.Test

class JsonMLTestCase {
    def originalXml = '<root><id>01</id><id>1</id><id>00</id><id>0</id><item id="01"/><title>True</title></root>'
    def expectedJsonString = '["root",["id","01"],["id","1"],["id","00"],["id","0"],["item",{"id":"01"}],["title","True"]]'

    @Test // test fails - JSON string lost leading zero and converted "True" to true. See test result in comment below
    void testToJSONArray_jsonOutput() {
        def actualJsonOutput = JSONML.toJSONArray(originalXml)
        assert actualJsonOutput == expectedJsonString
    }

    @Test // test fails - JSON string cannot be reverted to original xml. See test result in comment below
    void testToJSONArray_reversibility() {
        def revertedXml = JSONML.toString(org.json.JSONML.toJSONArray(originalXml))
        assert revertedXml == originalXml
    }

    @Test // test passes when using the new method toJsonML
    void testToJsonML() {
        def json = JSONML.toJsonML(originalXml)
        def reverseXml = org.json.JSONML.toString(json)
        assert originalXml == reverseXml
        assert json.toString() == expectedJsonString
    }
}


/****************** Test result:
testToJSONArray_jsonOutput(JsonMLTestCase)
expexted: ["root",["id","01"],["id","1"],["id","00"],["id","0"],["item",{"id":"01"}],["title","True"]]
actual:   ["root",["id",1],["id",1],["id","00"],["id",0],["item",{"id":1}],["title",true]]

testToJSONArray_reversibility(JsonMLTestCase)
expexted: <root><id>01</id><id>1</id><id>00</id><id>0</id><item id="01"/><title>True</title></root>
actual:   <root><id></id><id></id><id>00</id><id></id><item id="1"/><title></title></root>
********************/

… text

nodes) as strings.
For this purpose I overloaded parse() so that all existing toXXX() methods
could remain unchanged and will behave as before.
@schrepfler
Copy link

This is a perfectly valid patch, we've had this issue in production. There is no need to typecast to value types or make any hypothesis if the string a boolean or int.

@codler
Copy link

codler commented Aug 19, 2013

+1

@pdolega
Copy link

pdolega commented May 14, 2014

👍 got exactly the same problem; why isn't it already merged ?

@schrepfler
Copy link

It's funny to see how the JSON ecosystem is looking for work-arounds around the implicit type conversions: http://blog.getprismatic.com/schema-0-2-0-back-with-clojurescript-data-coercion/

@douglascrockford
Copy link
Contributor

JSON was designed to be language independent, which is why it works so well for data interchange.

johnjaylward pushed a commit to johnjaylward/JSON-java that referenced this pull request Jul 18, 2016
BGehrels pushed a commit to BGehrels/JSON-java that referenced this pull request Apr 29, 2020
test support for Numeric enhancements, Refactoring, Fix spelling:  stleary/pull/336
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.

5 participants