summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/databinding-json/src/main/java
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2010-04-01 21:21:25 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2010-04-01 21:21:25 +0000
commitb11e2ee704563e82f8b981cbcc672c4fb65bbc80 (patch)
tree5f504b41c2d9cb1f59d1d4b7287773e9cbeea6f4 /sca-java-2.x/trunk/modules/databinding-json/src/main/java
parentec66e5f51bc9d17d5332cdc4d30021801c355ae8 (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/src/main/java')
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java23
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java10
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java82
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java32
4 files changed, 122 insertions, 25 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);
}