Java Code Examples for org.json.XML#toJSONObject()

The following examples show how to use org.json.XML#toJSONObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Invalid XML string (partial CDATA chars in tag name)
 * Expects JSONException
 */
@Test
public void shouldHandleInvalidCDATABangInTag() {
    String xmlStr = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
        "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
        "   xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
        "    <address>\n"+
        "       <name>Joe Tester</name>\n"+
        "       <![[]>\n"+
        "   </address>\n"+
        "</addresses>";
    try {
        XML.toJSONObject(xmlStr);
        fail("Expecting a JSONException");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Expected 'CDATA[' at 204 [character 11 line 5]",
                e.getMessage());
    }
}
 
Example 2
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Valid XML with comments to JSONObject
 */
@Test
public void shouldHandleCommentsInXML() {

    String xmlStr = 
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
            "<!-- this is a comment -->\n"+
            "<addresses>\n"+
            "   <address>\n"+
            "       <![CDATA[ this is -- <another> comment ]]>\n"+
            "       <name>Joe Tester</name>\n"+
            "       <!-- this is a - multi line \n"+
            "            comment -->\n"+
            "       <street>Baker street 5</street>\n"+
            "   </address>\n"+
            "</addresses>";
    JSONObject jsonObject = XML.toJSONObject(xmlStr);
    String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
            "street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+
            "<another> comment \"}}}";
    JSONObject expectedJsonObject = new JSONObject(expectedStr);
    Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
 
Example 3
Source File: XMLConfigurationTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * 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,
            new XMLParserConfiguration(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);
}
 
Example 4
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Invalid XML string ('!' char and no closing tag brace)
 * Expects a JSONException
 */
@Test
public void shouldHandleInvalidBangNoCloseInTag() {
    String xmlStr = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
        "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
        "   xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
        "    <address>\n"+
        "       <name/>\n"+
        "       <!\n"+
        "   </address>\n"+
        "</addresses>";
    try {
        XML.toJSONObject(xmlStr);
        fail("Expecting a JSONException");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Misshaped meta tag at 213 [character 12 line 7]",
                e.getMessage());
    }
}
 
Example 5
Source File: XMLConfigurationTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * No SML start tag. The ending tag ends up being treated as content.
 */
@Test
public void shouldHandleNoStartTag() {
    String xmlStr = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
        "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
        "   xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
        "    <address>\n"+
        "       <name/>\n"+
        "       <nocontent/>>\n"+
        "   </address>\n"+
        "</addresses>";
    String expectedStr = 
        "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
        "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
        "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
    JSONObject jsonObject = XML.toJSONObject(xmlStr,
            XMLParserConfiguration.KEEP_STRINGS);
    JSONObject expectedJsonObject = new JSONObject(expectedStr);
    Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
 
Example 6
Source File: XMLConfigurationTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Invalid XML string ('!' char in tag)
 * Expects a JSONException
 */
@Test
public void shouldHandleInvalidBangInTag() {
    String xmlStr = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
        "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
        "   xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
        "    <address>\n"+
        "       <name/>\n"+
        "       <!>\n"+
        "   </address>\n"+
        "</addresses>";
    try {
        XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
        fail("Expecting a JSONException");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Misshaped meta tag at 214 [character 12 line 7]",
                e.getMessage());
    }
}
 
Example 7
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Invalid XML string (tag contains a frontslash).
 * Expects a JSONException
 */
@Test
public void shouldHandleInvalidSlashInTag() {
    String xmlStr = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
        "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
        "   xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
        "    <address>\n"+
        "       <name/x>\n"+
        "       <street>abc street</street>\n"+
        "   </address>\n"+
        "</addresses>";
    try {
        XML.toJSONObject(xmlStr);
        fail("Expecting a JSONException");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Misshaped tag at 176 [character 14 line 4]",
                e.getMessage());
    }
}
 
Example 8
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method, given an input string and expected result, convert to
 * JSONObject via file and compare actual to expected result.
 * 
 * @param xmlStr
 *            the string to parse
 * @param expectedStr
 *            the expected JSON string
 * @throws IOException
 */
private void compareFileToJSONObject(String xmlStr, String expectedStr) {
    try {
        JSONObject expectedJsonObject = new JSONObject(expectedStr);
        File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
        try(FileWriter fileWriter = new FileWriter(tempFile);){
            fileWriter.write(xmlStr);
        }
        try(Reader reader = new FileReader(tempFile);){
            JSONObject jsonObject = XML.toJSONObject(reader);
            Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
        }
    } catch (IOException e) {
        fail("file writer error: " +e.getMessage());
    }
}
 
Example 9
Source File: XMLConfigurationTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Invalid XML string (no end brace for tag)
 * Expects JSONException
 */
@Test
public void shouldHandleNoCloseStartTag() {
    String xmlStr = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
        "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
        "   xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
        "    <address>\n"+
        "       <name/>\n"+
        "       <abc\n"+
        "   </address>\n"+
        "</addresses>";
    try {
        XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
        fail("Expecting a JSONException");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Misplaced '<' at 193 [character 4 line 6]",
                e.getMessage());
    }
}
 
Example 10
Source File: VtoV.java    From DETA_BackEnd with Apache License 2.0 5 votes vote down vote up
public Map<String, Object> XmlToMap(String response){
	JSONObject responseJson = XML.toJSONObject(response);
	Gson gson = new Gson();
	Type type = new TypeToken<Map<String, Object>>(){}.getType();
	Map<String, Object> responseMap =gson.fromJson(responseJson.toString(), type);
	return responseMap;
}
 
Example 11
Source File: XMLConfigurationTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Empty JSONObject from a non-XML string.
 */
@Test
public void shouldHandleNonXML() {
    String xmlStr = "{ \"this is\": \"not xml\"}";
    JSONObject jsonObject = 
            XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
    assertTrue("xml string should be empty", jsonObject.isEmpty());
}
 
Example 12
Source File: SearchController.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Converts input xml stream into output json stream.
 * WARNING this implementation stores xml data into memory.
 * <p>
 * @param xml_stream
 * @param output
 * @throws IOException
 * @throws JSONException
 */
void toJSON(InputStream xml_stream, OutputStream output)
      throws IOException, JSONException
{
   StringWriter writer = new StringWriter();
   IOUtils.copy(xml_stream, writer, "UTF-8");

   JSONObject xmlJSONObj = XML.toJSONObject(writer.toString().trim());
   String jsonPrettyPrintString = xmlJSONObj.toString(3);

   output.write(jsonPrettyPrintString.getBytes());
}
 
Example 13
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * JSONObject from a null XML string.
 * Expects a NullPointerException
 */
@Test(expected=NullPointerException.class)
public void shouldHandleNullXML() {
    String xmlStr = null;
    JSONObject jsonObject = XML.toJSONObject(xmlStr);
    assertTrue("jsonObject should be empty", jsonObject.isEmpty());
}
 
Example 14
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method, given an input string and expected result,
 * convert to JSONObject via reader and compare actual to expected result.
 * @param xmlStr the string to parse
 * @param expectedStr the expected JSON string
 */
private void compareReaderToJSONObject(String xmlStr, String expectedStr) {
    JSONObject expectedJsonObject = new JSONObject(expectedStr);
    Reader reader = new StringReader(xmlStr);
    JSONObject jsonObject = XML.toJSONObject(reader);
    Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
 
Example 15
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 16
Source File: VtoV.java    From Deta_DataBase with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> XmlToMap(String response){
	//下面这行代码独立作者 罗瑶光 2016年已转让给走四方。本系统没有用到这函数。
	JSONObject responseJson = XML.toJSONObject(response);
	Gson gson = new Gson();
	Type type = new TypeToken<Map<String, Object>>(){}.getType();
	Map<String, Object> responseMap =gson.fromJson(responseJson.toString(), type);
	return responseMap;
}
 
Example 17
Source File: VtoV.java    From Deta_Cache with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> XmlToMap(String response){
	JSONObject responseJson = XML.toJSONObject(response);
	Gson gson = new Gson();
	Type type = new TypeToken<Map<String, Object>>(){}.getType();
	Map<String, Object> responseMap =gson.fromJson(responseJson.toString(), type);
	return responseMap;
}
 
Example 18
Source File: VtoV.java    From DETA_BackEnd with Apache License 2.0 4 votes vote down vote up
public JSONObject XmlToJsonObject(String response ){
	JSONObject responseJson = XML.toJSONObject(response);
	return responseJson;
}
 
Example 19
Source File: VtoV.java    From Deta_DataBase with Apache License 2.0 4 votes vote down vote up
public static JSONObject XmlToJsonObject(String response ){
	//下面这行代码独立作者 罗瑶光 2016年已转让给走四方。本系统没有用到这函数。
	JSONObject responseJson = XML.toJSONObject(response);
	return responseJson;
}
 
Example 20
Source File: XMLTest.java    From JSON-Java-unit-test with Apache License 2.0 2 votes vote down vote up
/**
 * Convenience method, given an input string and expected result,
 * convert to JSONObject and compare actual to expected result.
 * @param xmlStr the string to parse
 * @param expectedStr the expected JSON string
 */
private void compareStringToJSONObject(String xmlStr, String expectedStr) {
    JSONObject jsonObject = XML.toJSONObject(xmlStr);
    JSONObject expectedJsonObject = new JSONObject(expectedStr);
    Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}