summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/databinding-json/src
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2011-07-20 16:41:00 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2011-07-20 16:41:00 +0000
commita9c4675e9050b41b47349f387806057f1219252b (patch)
tree4eb57ed0de4d7a3035111671b0aa7e39051c41fb /sca-java-2.x/trunk/modules/databinding-json/src
parent5b4ef5f167e0b2e08f71a4098b1596486d6c5f51 (diff)
Streamline the json serialization into output stream
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1148839 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/databinding-json/src')
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java23
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java1
-rw-r--r--sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer3
3 files changed, 22 insertions, 5 deletions
diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
index 920485a540..cb76f74baa 100644
--- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
+++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
@@ -27,6 +27,9 @@ import org.apache.tuscany.sca.databinding.TransformationException;
import org.apache.tuscany.sca.databinding.json.JSONDataBinding;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.json.JSONArray;
+import org.json.JSONObject;
/**
*
@@ -50,10 +53,22 @@ public class JSON2OutputStream implements PushTransformer<Object, OutputStream>
} else if (source instanceof JsonParser) {
JacksonHelper.write((JsonParser)source, sink);
} else {
- try {
- sink.write(source.toString().getBytes("UTF-8"));
- } catch (Exception e) {
- throw new TransformationException(e);
+ if (source instanceof String || source instanceof JSONObject
+ || source instanceof JSONArray
+ || source instanceof org.codehaus.jettison.json.JSONObject
+ || source instanceof org.codehaus.jettison.json.JSONArray) {
+ try {
+ sink.write(source.toString().getBytes("UTF-8"));
+ } catch (Exception e) {
+ throw new TransformationException(e);
+ }
+ } else {
+ ObjectMapper mapper = JacksonHelper.createObjectMapper(source.getClass());
+ try {
+ mapper.writeValue(sink, source);
+ } catch (Throwable e) {
+ throw new TransformationException(e);
+ }
}
}
}
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
index e684378c6e..895c9e70ce 100644
--- 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
@@ -98,7 +98,6 @@ public class JacksonHelper {
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
mapper.getDeserializationConfig().setAnnotationIntrospector(pair);
- // [rfeng] To avoid complaints about javaClass
mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
mapper.getSerializationConfig().setAnnotationIntrospector(pair);
mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer b/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer
index c3272d1425..f880719ff7 100644
--- a/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer
+++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer
@@ -15,3 +15,6 @@
# specific language governing permissions and limitations
# under the License.
org.apache.tuscany.sca.databinding.json.jackson.JSON2OutputStream;source=JSON;target=application/json#java.io.OutputStream,weight=50,public=true
+org.apache.tuscany.sca.databinding.json.jackson.JSON2OutputStream;source=javax.xml.bind.JAXBElement;target=application/json#java.io.OutputStream,weight=50,public=true
+org.apache.tuscany.sca.databinding.json.jackson.JSON2OutputStream;source=java:complexType;target=application/json#java.io.OutputStream,weight=50,public=true
+org.apache.tuscany.sca.databinding.json.jackson.JSON2OutputStream;source=java:simpleType;target=application/json#java.io.OutputStream,weight=50,public=true