tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r930100 - in /tuscany/sca-java-2.x/trunk/modules/databinding-json/src: main/java/org/apache/tuscany/sca/databinding/json/ main/java/org/apache/tuscany/sca/databinding/json/jackson/ test/java/org/apache/tuscany/sca/databinding/json/
Date Thu, 01 Apr 2010 21:21:27 GMT
Author: rfeng
Date: Thu Apr  1 21:21:25 2010
New Revision: 930100

URL: http://svn.apache.org/viewvc?rev=930100&view=rev
Log:
Enhance the JSON databindign with Jackson 1.5.0

Added:
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
  (with props)
Modified:
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java?rev=930100&r1=930099&r2=930100&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java
Thu Apr  1 21:21:25 2010
@@ -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

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java?rev=930100&r1=930099&r2=930100&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java
Thu Apr  1 21:21:25 2010
@@ -26,12 +26,9 @@ import org.apache.tuscany.sca.databindin
 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 Pull
 
     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) {

Added: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java?rev=930100&view=auto
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
(added)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
Thu Apr  1 21:21:25 2010
@@ -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);
+        }
+    }
+
+}

Propchange: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java?rev=930100&r1=930099&r2=930100&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
Thu Apr  1 21:21:25 2010
@@ -24,10 +24,10 @@ import org.apache.tuscany.sca.databindin
 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 Pull
 
     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 Pull
             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);
         }

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java?rev=930100&r1=930099&r2=930100&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JSONTransformerTestCase.java
Thu Apr  1 21:21:25 2010
@@ -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.interfaced
 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();



Mime
View raw message