diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-04-01 21:21:25 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-04-01 21:21:25 +0000 |
commit | b11e2ee704563e82f8b981cbcc672c4fb65bbc80 (patch) | |
tree | 5f504b41c2d9cb1f59d1d4b7287773e9cbeea6f4 /sca-java-2.x/trunk/modules/databinding-json | |
parent | ec66e5f51bc9d17d5332cdc4d30021801c355ae8 (diff) |
Enhance the JSON databindign with Jackson 1.5.0
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@930100 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/databinding-json')
5 files changed, 173 insertions, 73 deletions
diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java index bc6184189a..cdcad3a472 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java @@ -19,6 +19,9 @@ package org.apache.tuscany.sca.databinding.json; +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; @@ -39,16 +42,24 @@ public class JSONHelper { JSONObject json = null; if (source instanceof JSONObject) { json = (JSONObject)source; - } else if (source instanceof org.json.JSONObject) { - try { - json = new JSONObject(((org.json.JSONObject)source).toString()); - } catch (JSONException e) { - throw new IllegalArgumentException(e); - } + } else if (source instanceof org.json.JSONObject || source instanceof String) { + json = stringToJettision(source.toString()); + } else if (source instanceof JsonNode) { + json = stringToJettision(JacksonHelper.toString((JsonNode)source)); + } else if (source instanceof JsonParser) { + json = stringToJettision(JacksonHelper.toString((JsonParser)source)); } return json; } + private static JSONObject stringToJettision(String content) { + try { + return new JSONObject(content); + } catch (JSONException e) { + throw new IllegalArgumentException(e); + } + } + /** * Convert to org.json.JSONObject * @param source diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java index 6e9f0623f1..5edcc6701e 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java @@ -26,12 +26,9 @@ import org.apache.tuscany.sca.databinding.javabeans.JavaBeansDataBinding; import org.apache.tuscany.sca.databinding.json.JSONDataBinding; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; -import org.codehaus.jackson.map.AnnotationIntrospector; import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; import org.codehaus.jackson.map.type.TypeFactory; import org.codehaus.jackson.type.JavaType; -import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; /** * @version $Rev$ $Date$ @@ -41,12 +38,7 @@ public class JSON2Object implements PullTransformer<Object, Object> { public JSON2Object() { super(); - mapper = new ObjectMapper(); - AnnotationIntrospector primary = new JaxbAnnotationIntrospector(); - AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); - AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); - mapper.getDeserializationConfig().setAnnotationIntrospector(pair); - mapper.getSerializationConfig().setAnnotationIntrospector(pair); + mapper = JacksonHelper.createObjectMapper(); } public Object transform(Object source, TransformationContext context) { diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java new file mode 100644 index 0000000000..1e3399017c --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.databinding.json.jackson; + +import java.io.IOException; +import java.io.StringWriter; + +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonGenerator; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.map.AnnotationIntrospector; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; +import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; + +/** + * + */ +public class JacksonHelper { + public static ObjectMapper createObjectMapper() { + ObjectMapper mapper = new ObjectMapper(); + AnnotationIntrospector primary = new JaxbAnnotationIntrospector(); + AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); + AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); + mapper.getDeserializationConfig().setAnnotationIntrospector(pair); + mapper.getSerializationConfig().setAnnotationIntrospector(pair); + return mapper; + } + + public static String toString(JsonNode node) { + try { + JsonFactory jsonFactory = new JsonFactory(); + StringWriter sw = new StringWriter(); + JsonGenerator generator = jsonFactory.createJsonGenerator(sw); + generator.writeTree(node); + return sw.toString(); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static String toString(JsonParser parser) { + try { + JsonFactory jsonFactory = new JsonFactory(); + StringWriter sw = new StringWriter(); + JsonGenerator generator = jsonFactory.createJsonGenerator(sw); + JsonNode node = parser.readValueAs(JsonNode.class); + generator.writeTree(node); + return sw.toString(); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static JsonParser createJsonParser(String content) { + JsonFactory jsonFactory = new JsonFactory(); + try { + return jsonFactory.createJsonParser(content); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + +} diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java index 340cf99f37..bd99dfd881 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java @@ -24,10 +24,10 @@ import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; import org.apache.tuscany.sca.databinding.javabeans.JavaBeansDataBinding; import org.apache.tuscany.sca.databinding.json.JSONDataBinding; -import org.codehaus.jackson.map.AnnotationIntrospector; +import org.apache.tuscany.sca.databinding.json.JSONHelper; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; -import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; /** * @version $Rev$ $Date$ @@ -37,12 +37,7 @@ public class Object2JSON implements PullTransformer<Object, Object> { public Object2JSON() { super(); - mapper = new ObjectMapper(); - AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); - AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); - AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); - mapper.getDeserializationConfig().setAnnotationIntrospector(pair); - mapper.getSerializationConfig().setAnnotationIntrospector(pair); + mapper = JacksonHelper.createObjectMapper(); } public Object transform(Object source, TransformationContext context) { @@ -50,8 +45,25 @@ public class Object2JSON implements PullTransformer<Object, Object> { return null; } + Class<?> targetType = null; + if (context != null && context.getTargetDataType() != null) { + targetType = context.getTargetDataType().getPhysical(); + } + if (targetType == null) { + targetType = String.class; + } try { - return mapper.writeValueAsString(source); + String value = mapper.writeValueAsString(source); + if (targetType == String.class) { + return value; + } else if (JsonNode.class.isAssignableFrom(targetType)) { + return JacksonHelper.createJsonParser(value).readValueAsTree(); + } + if (JsonParser.class.isAssignableFrom(targetType)) { + return JacksonHelper.createJsonParser(value); + } else { + return JSONHelper.toJSON(value, targetType); + } } catch (Exception e) { throw new TransformationException(e); } diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java b/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java index 874378bf92..1aa6cf3dab 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java @@ -23,7 +23,6 @@ import java.io.StringReader; import java.io.StringWriter; import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; @@ -43,64 +42,68 @@ import org.apache.tuscany.sca.interfacedef.util.XMLType; import org.json.JSONObject; import org.junit.Test; -public class JSONTransformerTestCase { - private static final String IPO_XML = "<?xml version=\"1.0\"?>" + "<ipo:purchaseOrder" - + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" - + " xmlns:ipo=\"http://www.example.com/IPO\"" - + " xsi:schemaLocation=\"http://www.example.com/IPO ipo.xsd\"" - + " orderDate=\"1999-12-01\">" - + " <shipTo exportCode=\"1\" xsi:type=\"ipo:UKAddress\">" - + " <name>Helen Zoe</name>" - + " <street>47 Eden Street</street>" - + " <city>Cambridge</city>" - + " <postcode>CB1 1JR</postcode>" - + " </shipTo>" - + " <billTo xsi:type=\"ipo:USAddress\">" - + " <name>Robert Smith</name>" - + " <street>8 Oak Avenue</street>" - + " <city>Old Town</city>" - + " <state>PA</state>" - + " <zip>95819</zip>" - + " </billTo>" - + " <items>" - + " <item partNum=\"833-AA\">" - + " <productName>Lapis necklace</productName>" - + " <quantity>1</quantity>" - + " <USPrice>99.95</USPrice>" - + " <ipo:comment>Want this for the holidays</ipo:comment>" - + " <shipDate>1999-12-05</shipDate>" - + " </item>" - + " </items>" - + "</ipo:purchaseOrder>"; +public class JSONTransformerTestCase { + private static final String IPO_XML = + "<?xml version=\"1.0\"?>" + "<ipo:purchaseOrder" + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + + " xmlns:ipo=\"http://www.example.com/IPO\"" + + " xsi:schemaLocation=\"http://www.example.com/IPO ipo.xsd\"" + + " orderDate=\"1999-12-01\">" + + " <shipTo exportCode=\"1\" xsi:type=\"ipo:UKAddress\">" + + " <name>Helen Zoe</name>" + + " <street>47 Eden Street</street>" + + " <city>Cambridge</city>" + + " <postcode>CB1 1JR</postcode>" + + " </shipTo>" + + " <billTo xsi:type=\"ipo:USAddress\">" + + " <name>Robert Smith</name>" + + " <street>8 Oak Avenue</street>" + + " <city>Old Town</city>" + + " <state>PA</state>" + + " <zip>95819</zip>" + + " </billTo>" + + " <items>" + + " <item partNum=\"833-AA\">" + + " <productName>Lapis necklace</productName>" + + " <quantity>1</quantity>" + + " <USPrice>99.95</USPrice>" + + " <ipo:comment>Want this for the holidays</ipo:comment>" + + " <shipDate>1999-12-05</shipDate>" + + " </item>" + + " </items>" + + "</ipo:purchaseOrder>"; - private static final String JSON_STR = "{\"xsl:root\":{\"@xmlns\":{\"xsl\":\"http://foo.com\"},\"data\":{\"$\":\"my json string\"}}}"; + private static final String JSON_STR = + "{\"xsl:root\":{\"@xmlns\":{\"xsl\":\"http://foo.com\"},\"data\":{\"$\":\"my json string\"}}}"; @Test public void testXML2JSON() throws Exception { - ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); - - XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(IPO_XML)); + ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); + StAXHelper staxHelper = StAXHelper.getInstance(extensionPointRegistry); + + XMLStreamReader reader = staxHelper.createXMLStreamReader(new StringReader(IPO_XML)); XMLStreamReader2JSON t1 = new XMLStreamReader2JSON(extensionPointRegistry); - JSONObject json = (JSONObject) t1.transform(reader, null); + JSONObject json = (JSONObject)t1.transform(reader, null); Assert.assertNotNull(json); - // Cannot round-trip as we hit a bug in Jettison: http://jira.codehaus.org/browse/JETTISON-37 + // Cannot round-trip as we hit a bug in Jettison: http://jira.codehaus.org/browse/JETTISON-93 /* - JSON2XMLStreamReader t2 = new JSON2XMLStreamReader(); - XMLStreamReader reader2 = t2.transform(json, null); - StringWriter sw = new StringWriter(); - XMLStreamWriter streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); - new XMLStreamSerializer().serialize(reader2, streamWriter); - streamWriter.flush(); - System.out.println(sw.toString()); - */ + JSON2XMLStreamReader t2 = new JSON2XMLStreamReader(); + XMLStreamReader reader2 = t2.transform(json, null); + StringWriter sw = new StringWriter(); + XMLStreamWriter streamWriter = staxHelper.createXMLStreamWriter(sw); + staxHelper.save(reader2, streamWriter); + streamWriter.flush(); + System.out.println(sw.toString()); + */ + } @Test public void testJSON2XML() throws Exception { - ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); - StAXHelper helper = StAXHelper.getInstance(extensionPointRegistry); - + ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); + StAXHelper helper = StAXHelper.getInstance(extensionPointRegistry); + JSON2XMLStreamReader t2 = new JSON2XMLStreamReader(); XMLStreamReader reader2 = t2.transform(new JSONObject(JSON_STR), null); StringWriter sw = new StringWriter(); @@ -126,7 +129,7 @@ public class JSONTransformerTestCase { public void testString2JSON() throws Exception { String json = "{\"name\":\"John\",\"age\":25}"; String2JSON t1 = new String2JSON(); - JSONObject jsonObject = (JSONObject) t1.transform(json, null); + JSONObject jsonObject = (JSONObject)t1.transform(json, null); Assert.assertEquals(jsonObject.getString("name"), "John"); Assert.assertEquals(jsonObject.getInt("age"), 25); JSON2String t2 = new JSON2String(); |