mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #52 from johnjaylward/OptionalTypeConversion
updates Test cases to support new JSONML and XML conversion options
This commit is contained in:
commit
62524b531d
2 changed files with 81 additions and 0 deletions
|
@ -698,4 +698,41 @@ public class JSONMLTest {
|
|||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string with lost leading zero and converted "True" to true. See test
|
||||
* result in comment below.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_jsonOutput() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]";
|
||||
final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false);
|
||||
assertEquals(expectedJsonString, actualJsonOutput.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string cannot be reverted to original xml. See test result in
|
||||
* comment below.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_reversibility() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false));
|
||||
assertNotEquals(revertedXml, originalXml);
|
||||
}
|
||||
|
||||
/**
|
||||
* test passes when using the new method toJsonML.
|
||||
*/
|
||||
@Test
|
||||
public void testToJsonML() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
|
||||
final JSONArray json = JSONML.toJSONArray(originalXml,true);
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
|
||||
final String reverseXml = JSONML.toString(json);
|
||||
assertEquals(originalXml, reverseXml);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package org.json.junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONML;
|
||||
import org.json.JSONObject;
|
||||
import org.json.XML;
|
||||
import org.junit.Rule;
|
||||
|
@ -723,4 +725,46 @@ public class XMLTest {
|
|||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string lost leading zero and converted "True" to true.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_jsonOutput() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}";
|
||||
final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false);
|
||||
|
||||
assertEquals(expectedJsonString, actualJsonOutput.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON string cannot be reverted to original xml.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONArray_reversibility() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String revertedXml = XML.toString(XML.toJSONObject(originalXml, false));
|
||||
|
||||
assertNotEquals(revertedXml, originalXml);
|
||||
}
|
||||
|
||||
/**
|
||||
* test passes when using the new method toJsonArray.
|
||||
*/
|
||||
@Test
|
||||
public void testToJsonXML() {
|
||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}";
|
||||
|
||||
final JSONObject json = XML.toJSONObject(originalXml,true);
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
|
||||
final String reverseXml = XML.toString(json);
|
||||
// this reversal isn't exactly the same. use JSONML for an exact reversal
|
||||
final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>";
|
||||
|
||||
assertEquals(expectedReverseXml, reverseXml);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue