douglascrockford/JSON-java — pull request #70 by yaccob (2012-11-14 23:07:12+00:00)
Title: Support reversible JsonML mapping by avoiding implicit type conversion

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 following test case illustrates the problem (toJSONArray) and the solution (toJsonML):

```java

import static org.junit.Assert.*;
import org.junit.Test;
import org.json.JSONML;

public class NewJsonMLTestCase {
String originalXml = "011000

True

";
String ëxpectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";

@Test // test fails - see comment below
public void testToJSONArray_jsonOutput() throws Exception {
String actualJsonOutput = JSONML.toJSONArray(originalXml).toString();
assertEquals(actualJsonOutput, ëxpectedJsonString);
}

@Test // test fails - see comment below
public void testToJSONArray_reversibility() throws Exception {
String revertedXml = JSONML.toString(org.json.JSONML.toJSONArray(originalXml));
assertEquals(revertedXml, originalXml);
}

@Test // test passes
public void testToJsonML() throws Exception {
org.json.JSONArray json = JSONML.toJsonML(originalXml);
String reverseXml = org.json.JSONML.toString(json);
assertEquals(originalXml, reverseXml);
assertEquals(json.toString(), ëxpectedJsonString);
}
}

/****************** Test result:

testToJSONArray_reversibility(NewJsonMLTestCase)
org.junit.ComparisonFailure: expected:<[00

]

>
but was: <[011000

True]

>

testToJSONArray_jsonOutput(NewJsonMLTestCase)
org.junit.ComparisonFailure: expected:<["root",["id",[1],["id",1],["id","00"],["id",0],["item",{"id":1}],["title",true]]]>
but was: <["root",["id",["01"],["id","1"],["id","00"],["id","0"],["item",{"id":"01"}],["title","True"]]]>

********************/
```