Author: shawkins
Date: 2012-01-11 15:33:42 -0500 (Wed, 11 Jan 2012)
New Revision: 3788
Modified:
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java
trunk/engine/src/test/java/org/teiid/query/function/source/TestXMLSystemFunctions.java
Log:
TEIID-1896 fix for nested json array xml conversion
Modified:
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java 2012-01-11
20:21:29 UTC (rev 3787)
+++
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java 2012-01-11
20:33:42 UTC (rev 3788)
@@ -113,37 +113,33 @@
private static final class JsonToXmlContentHandler implements
ContentHandler {
private final XMLStreamWriter streamWriter;
- private String currentName;
- private LinkedList<Boolean> inArray = new LinkedList<Boolean>();
+ private boolean rootArray;
+ private LinkedList<String> nameStack = new LinkedList<String>();
private JsonToXmlContentHandler(String rootName,
XMLStreamWriter streamWriter) {
this.streamWriter = streamWriter;
- this.currentName = rootName;
+ this.nameStack.push(rootName);
}
@Override
public boolean startObjectEntry(String key)
throws org.json.simple.parser.ParseException, IOException {
- currentName = key;
- start();
+ this.nameStack.push(key);
return true;
}
@Override
public boolean startObject() throws org.json.simple.parser.ParseException,
IOException {
- if (inArray.peek()) {
- start();
- }
- inArray.push(false);
+ start();
return true;
}
private void start()
throws IOException {
try {
- streamWriter.writeStartElement(escapeName(currentName, true));
+ streamWriter.writeStartElement(escapeName(this.nameStack.peek(), true));
} catch (XMLStreamException e) {
throw new IOException(e);
}
@@ -157,23 +153,22 @@
} catch (XMLStreamException e) {
throw new IOException(e);
}
- inArray.push(false);
- start();
}
@Override
public boolean startArray() throws org.json.simple.parser.ParseException,
IOException {
- inArray.push(true);
+ if (this.nameStack.size() == 1) {
+ this.rootArray = true;
+ start();
+ }
return true;
}
@Override
public boolean primitive(Object value)
throws org.json.simple.parser.ParseException, IOException {
- if (inArray.peek()) {
- start();
- }
+ start();
try {
if (value != null) {
streamWriter.writeCharacters(value.toString());
@@ -184,9 +179,7 @@
} catch (XMLStreamException e) {
throw new IOException(e);
}
- if (inArray.peek()) {
- end();
- }
+ end();
return true;
}
@@ -202,24 +195,20 @@
@Override
public boolean endObjectEntry()
throws org.json.simple.parser.ParseException, IOException {
- end();
+ this.nameStack.pop();
return true;
}
@Override
public boolean endObject() throws org.json.simple.parser.ParseException,
IOException {
- inArray.pop();
- if (inArray.peek()) {
- end();
- }
+ end();
return true;
}
@Override
public void endJSON() throws org.json.simple.parser.ParseException,
IOException {
- end();
try {
streamWriter.writeEndDocument();
} catch (XMLStreamException e) {
@@ -230,7 +219,9 @@
@Override
public boolean endArray() throws org.json.simple.parser.ParseException,
IOException {
- inArray.pop();
+ if (this.nameStack.size() == 1 && rootArray) {
+ end();
+ }
return true;
}
}
Modified:
trunk/engine/src/test/java/org/teiid/query/function/source/TestXMLSystemFunctions.java
===================================================================
---
trunk/engine/src/test/java/org/teiid/query/function/source/TestXMLSystemFunctions.java 2012-01-11
20:21:29 UTC (rev 3787)
+++
trunk/engine/src/test/java/org/teiid/query/function/source/TestXMLSystemFunctions.java 2012-01-11
20:33:42 UTC (rev 3788)
@@ -194,7 +194,7 @@
@Test public void testJsonToXml() throws Exception {
String json =
"[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
- String expected = "<?xml version=\"1.0\"
?><Array><Array>0</Array><Array><_u0031_><_u0032_><_u0033_><_u0034_><_u0034_>5</_u0034_><_u0034_><_u0036_>7</_u0036_></_u0034_></_u0034_></_u0033_></_u0032_></_u0031_></Array></Array>";
+ String expected = "<?xml version=\"1.0\"
?><Array><Array>0</Array><Array><_u0031_><_u0032_><_u0033_><_u0034_>5</_u0034_><_u0034_><_u0036_>7</_u0036_></_u0034_></_u0033_></_u0032_></_u0031_></Array></Array>";
helpTestJson(json, "Array", expected);
}
@@ -214,7 +214,7 @@
@Test public void testJsonToXml1() throws Exception {
String json = "{ \"firstName\": \"John\",
\"lastName\": \"Smith\", \"age\": 25, \"address\":
{ \"streetAddress\": \"21 2nd Street\", \"city\": \"New
York\", \"state\": \"NY\", "+
"\"postalCode\": \"10021\" },
\"phoneNumber\": [ { \"type\": \"home\",
\"number\": \"212 555-1234\" }, { \"type\":
\"fax\", \"number\": \"646 555-4567\" } ] }";
- String expected = "<?xml version=\"1.0\"
?><Person><firstName>John</firstName><lastName>Smith</lastName><age>25</age><address><streetAddress>21
2nd Street</streetAddress><city>New
York</city><state>NY</state><postalCode>10021</postalCode></address><phoneNumber><phoneNumber><type>home</type><number>212
555-1234</number></phoneNumber><number><type>fax</type><number>646
555-4567</number></number></phoneNumber></Person>";
+ String expected = "<?xml version=\"1.0\"
?><Person><firstName>John</firstName><lastName>Smith</lastName><age>25</age><address><streetAddress>21
2nd Street</streetAddress><city>New
York</city><state>NY</state><postalCode>10021</postalCode></address><phoneNumber><type>home</type><number>212
555-1234</number></phoneNumber><phoneNumber><type>fax</type><number>646
555-4567</number></phoneNumber></Person>";
helpTestJson(json, "Person", expected);
}
@@ -224,6 +224,28 @@
helpTestJson(json, "Person", expected);
}
+ @Test public void testJsonToXml3() throws Exception {
+ String json = "{ \"kids\":[{ \"firstName\" :
\"George\" }, { \"firstName\" : \"Jerry\" }]}";
+ String expected = "<?xml version=\"1.0\"
?><Person><kids><firstName>George</firstName></kids><kids><firstName>Jerry</firstName></kids></Person>";
+ helpTestJson(json, "Person", expected);
+ }
+
+ @Test public void testJsonToXml4() throws Exception {
+ String json = "{ \"kids\":[{ \"firstName\" :
\"George\" }, { \"firstName\" : \"Jerry\" }]}";
+ String expected = "<?xml version=\"1.0\"
?><Person><kids><firstName>George</firstName></kids><kids><firstName>Jerry</firstName></kids></Person>";
+ helpTestJson(json, "Person", expected);
+ }
+
+ /**
+ * This shows an ambiguity with the approach in that array/object children of an array
cannot be distinguished
+ * @throws Exception
+ */
+ @Test public void testJsonToXml5() throws Exception {
+ String json = "[[],{\"x\": 1},[]]";
+ String expected = "<?xml version=\"1.0\"
?><Person><Person></Person><Person><x>1</x></Person><Person></Person></Person>";
+ helpTestJson(json, "Person", expected);
+ }
+
@BeforeClass static public void setUpOnce() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-6:00"));
}