Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Type attributes added

  • Loading branch information...
commit 2fb3450c23eab593d80240089b38c116cc8769aa 1 parent 0f8f4dd
@lukas-krecan authored
View
51 src/main/java/net/javacrumbs/json2xml/JsonSaxAdapter.java
@@ -28,6 +28,7 @@
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
+import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
@@ -62,6 +63,8 @@
private final ContentHandler contentHandler;
private final String namespaceUri;
+
+ private final boolean addTypeAttributes;
private static final JsonFactory JSON_FACTORY = new JsonFactory();
@@ -74,10 +77,16 @@ public JsonSaxAdapter(final String json, final ContentHandler contentHandler) {
public JsonSaxAdapter(final JsonParser jsonParser, final ContentHandler contentHandler) {
this(jsonParser, contentHandler, "");
}
+
public JsonSaxAdapter(final JsonParser jsonParser, final ContentHandler contentHandler, final String namespaceUri) {
+ this(jsonParser, contentHandler, namespaceUri, false);
+ }
+
+ public JsonSaxAdapter(final JsonParser jsonParser, final ContentHandler contentHandler, final String namespaceUri, final boolean addTypeAttributes) {
this.jsonParser = jsonParser;
this.contentHandler = contentHandler;
this.namespaceUri = namespaceUri;
+ this.addTypeAttributes = addTypeAttributes;
contentHandler.setDocumentLocator(new DocumentLocator());
}
@@ -176,10 +185,48 @@ private void parseValue() throws Exception {
private void startElement(final String elementName) throws SAXException {
- contentHandler.startElement(namespaceUri, elementName, elementName, EMPTY_ATTRIBUTES);
+ contentHandler.startElement(namespaceUri, elementName, elementName, getTypeAttributes());
}
+
+
+ protected Attributes getTypeAttributes() {
+ if (addTypeAttributes)
+ {
+ String currentTokenType = getCurrentTokenType();
+ if (currentTokenType!=null)
+ {
+ AttributesImpl attributes = new AttributesImpl();
+ attributes.addAttribute("", "type", "type", "string", currentTokenType);
+ return attributes;
+ }
+ else
+ {
+ return EMPTY_ATTRIBUTES;
+ }
+ }
+ else
+ {
+ return EMPTY_ATTRIBUTES;
+ }
+ }
+
+
+ protected String getCurrentTokenType() {
+ switch (jsonParser.getCurrentToken())
+ {
+ case VALUE_NUMBER_INT: return "int";
+ case VALUE_NUMBER_FLOAT: return "float";
+ case VALUE_FALSE: return "boolean";
+ case VALUE_TRUE: return "boolean";
+ case VALUE_STRING: return "string";
+ case VALUE_NULL: return "null";
+ case START_ARRAY: return "array";
+ default: return null;
+ }
+ }
+
- private void endElement(final String elementName) throws SAXException {
+ private void endElement(final String elementName) throws SAXException {
contentHandler.endElement(namespaceUri, elementName, elementName);
}
View
12 src/main/java/net/javacrumbs/json2xml/JsonXmlReader.java
@@ -42,6 +42,7 @@
private ContentHandler contentHandler;
private final String namespaceUri;
+ private final boolean addTypeAttributes;
public JsonXmlReader() {
@@ -49,10 +50,15 @@ public JsonXmlReader() {
}
public JsonXmlReader(String namespaceUri) {
- this.namespaceUri = namespaceUri;
+ this(namespaceUri, false);
}
- public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
+ public JsonXmlReader(String namespaceUri, boolean addTypeAttributes) {
+ this.namespaceUri = namespaceUri;
+ this.addTypeAttributes = addTypeAttributes;
+ }
+
+ public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
throw new UnsupportedOperationException();
}
@@ -105,7 +111,7 @@ public ErrorHandler getErrorHandler() {
public void parse(InputSource input) throws IOException, SAXException {
JsonParser jsonParser = new JsonFactory().createJsonParser(input.getCharacterStream());
- new JsonSaxAdapter(jsonParser, contentHandler, namespaceUri).parse();
+ new JsonSaxAdapter(jsonParser, contentHandler, namespaceUri, addTypeAttributes).parse();
}
View
42 src/test/java/net/javacrumbs/json2xml/JsonSaxAdapterTest.java
@@ -66,6 +66,34 @@
" </f>\n" +
" <g/>\n" +
"</document>\n";
+
+ private static final String XML_WITH_TYPES = "" +
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+ "<document xmlns=\"http://javacrumbs.net/test\">\n" +
+ " <a type=\"int\">1</a>\n" +
+ " <b type=\"int\">2</b>\n" +
+ " <c>\n" +
+ " <d type=\"string\">text</d>\n" +
+ " </c>\n" +
+ " <e type=\"array\">\n" +
+ " <e type=\"int\">1</e>\n" +
+ " <e type=\"int\">2</e>\n" +
+ " <e type=\"int\">3</e>\n" +
+ " </e>\n" +
+ " <f type=\"array\">\n" +
+ " <f type=\"array\">\n" +
+ " <f type=\"int\">1</f>\n" +
+ " <f type=\"int\">2</f>\n" +
+ " <f type=\"int\">3</f>\n" +
+ " </f>\n" +
+ " <f type=\"array\">\n" +
+ " <f type=\"int\">4</f>\n" +
+ " <f type=\"int\">5</f>\n" +
+ " <f type=\"int\">6</f>\n" +
+ " </f>\n" +
+ " </f>\n" +
+ " <g type=\"null\" />\n" +
+ "</document>";
@Test
public void testParse() throws Exception
@@ -85,6 +113,15 @@ public void testParseNamespace() throws Exception
Diff diff = XMLUnit.compareXML(xmlWithNamespace, xml);
assertTrue(diff.toString(), diff.similar());
}
+ @Test
+ public void testParseNamespaceWithAttributes() throws Exception
+ {
+ String xml = convertToXml(JSON, "http://javacrumbs.net/test", true);
+ System.out.println(xml);
+ XMLUnit.setIgnoreWhitespace(true);
+ Diff diff = XMLUnit.compareXML(XML_WITH_TYPES, xml);
+ assertTrue(diff.toString(), diff.similar());
+ }
@Test(expected=ParserException.class)
public void testMultipleRoots() throws Exception
@@ -98,11 +135,14 @@ public static String convertToXml(final String json) throws Exception {
return convertToXml(json, "");
}
public static String convertToXml(final String json, final String namespace) throws Exception {
+ return convertToXml(json, namespace, false);
+ }
+ public static String convertToXml(final String json, final String namespace, final boolean addTypeAttributes) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
InputSource source = new InputSource(new StringReader(json));
Result result = new StreamResult(out);
- transformer.transform(new SAXSource(new JsonXmlReader(namespace),source), result);
+ transformer.transform(new SAXSource(new JsonXmlReader(namespace, addTypeAttributes),source), result);
return new String(out.toByteArray());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.