diff options
Diffstat (limited to '')
5 files changed, 545 insertions, 0 deletions
diff --git a/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java new file mode 100644 index 0000000000..57b9d55b44 --- /dev/null +++ b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java @@ -0,0 +1,49 @@ +/* + * 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.InputStream; + +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.json.JSONDataBinding; + +/** + * + */ +public class InputStream2JSON implements PullTransformer<InputStream, Object> { + + public String getSourceDataBinding() { + return "application/json" + "#" + InputStream.class.getName(); + } + + public String getTargetDataBinding() { + return JSONDataBinding.NAME; + } + + public int getWeight() { + return 10; + } + + public Object transform(InputStream source, TransformationContext context) { + return JacksonHelper.createJsonParser(source); + } + +} diff --git a/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java new file mode 100644 index 0000000000..c1afd85d00 --- /dev/null +++ b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java @@ -0,0 +1,81 @@ +/* + * 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 org.apache.tuscany.sca.databinding.PullTransformer; +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.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.JavaType; + +/** + * @version $Rev$ $Date$ + */ +public class JSON2Object implements PullTransformer<Object, Object> { + // private ObjectMapper mapper; + + public JSON2Object() { + super(); + } + + public Object transform(Object source, TransformationContext context) { + if (source == null) { + return null; + } + + try { + Class<?> cls = context.getTargetDataType().getPhysical(); + ObjectMapper mapper = JacksonHelper.createObjectMapper(cls); + JavaType javaType = mapper.constructType(context.getTargetDataType().getGenericType()); + if (source instanceof String) { + String sourceString = (String) source; + if(sourceString.isEmpty()) { + return sourceString; + } else { + return mapper.readValue((String)source, javaType); + } + } else if (source instanceof JsonNode) { + return mapper.readValue((JsonNode)source, javaType); + } else if (source instanceof JsonParser) { + return mapper.readValue((JsonParser)source, javaType); + } else { + return mapper.readValue(source.toString(), javaType); + } + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public String getSourceDataBinding() { + return JSONDataBinding.NAME; + } + + public String getTargetDataBinding() { + return JavaBeansDataBinding.NAME; + } + + public int getWeight() { + return 5000; + } +} diff --git a/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java new file mode 100644 index 0000000000..1151d59570 --- /dev/null +++ b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java @@ -0,0 +1,94 @@ +/* + * 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.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.tuscany.sca.databinding.PushTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +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; + +/** + * + */ +public class JSON2OutputStream implements PushTransformer<Object, OutputStream> { + + public String getSourceDataBinding() { + return JSONDataBinding.NAME; + } + + public String getTargetDataBinding() { + return "application/json" + "#" + OutputStream.class.getName(); + } + + public void transform(Object source, OutputStream sink, TransformationContext context) { + if (source == null) { + return; + } + if (source instanceof InputStream) { + try { + InputStream input = (InputStream) source; + byte[] buffer = new byte[4096]; + int n = 0; + while (-1 != (n = input.read(buffer))) { + sink.write(buffer, 0, n); + } + input.close(); + } catch(IOException e) { + throw new TransformationException(e); + } + } else if (source instanceof JsonNode) { + JacksonHelper.write((JsonNode)source, sink); + } else if (source instanceof JsonParser) { + JacksonHelper.write((JsonParser)source, sink); + } else { + 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); + } + } + } + } + + public int getWeight() { + return 50; + } + +} diff --git a/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java new file mode 100644 index 0000000000..049907786c --- /dev/null +++ b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java @@ -0,0 +1,233 @@ +/* + * 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.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringWriter; + +import javax.xml.bind.annotation.adapters.XmlAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters; + +import org.codehaus.jackson.JsonEncoding; +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.DeserializationConfig; +import org.codehaus.jackson.map.MappingJsonFactory; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.annotate.JsonSerialize; +import org.codehaus.jackson.map.deser.BeanDeserializerFactory; +import org.codehaus.jackson.map.deser.StdDeserializerProvider; +import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; +import org.codehaus.jackson.map.module.SimpleDeserializers; +import org.codehaus.jackson.map.ser.CustomSerializerFactory; +import org.codehaus.jackson.map.util.StdDateFormat; +import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; +import org.codehaus.jackson.xc.XmlAdapterJsonDeserializer; +import org.codehaus.jackson.xc.XmlAdapterJsonSerializer; +import org.json.JSONObject; + +import com.fasterxml.jackson.module.jsonorg.JsonOrgModule; + +/** + * + */ +public class JacksonHelper { + public final static ObjectMapper MAPPER = createMapper(); + private final static JsonFactory FACTORY = new MappingJsonFactory(createMapper()); + + public static ObjectMapper createMapper() { + return createObjectMapper(null); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + public static ObjectMapper createObjectMapper(Class<?> cls) { + ObjectMapper mapper = null; + if (cls != null) { + // Workaround for http://jira.codehaus.org/browse/JACKSON-413 + Package pkg = cls.getPackage(); + if (pkg != null) { + XmlJavaTypeAdapters adapters = pkg.getAnnotation(XmlJavaTypeAdapters.class); + if (adapters != null) { + CustomSerializerFactory serializerFactory = new CustomSerializerFactory(); + BeanDeserializerFactory deserializerFactory = new BeanDeserializerFactory(null); + for (XmlJavaTypeAdapter a : adapters.value()) { + XmlAdapter xmlAdapter = null; + try { + xmlAdapter = a.value().newInstance(); + } catch (Throwable e) { + // Ignore + } + if (xmlAdapter != null) { + XmlAdapterJsonDeserializer deserializer = new XmlAdapterJsonDeserializer(xmlAdapter); + XmlAdapterJsonSerializer serializer = new XmlAdapterJsonSerializer(xmlAdapter); + SimpleDeserializers deserializers = new SimpleDeserializers(); + deserializers.addDeserializer(a.type(), deserializer); + deserializerFactory.withAdditionalDeserializers(deserializers); + serializerFactory.addGenericMapping(a.type(), serializer); + StdDeserializerProvider deserializerProvider = + new StdDeserializerProvider(deserializerFactory); + mapper = new ObjectMapper(); + mapper.registerModule(new JsonOrgModule()); + + mapper.setSerializerFactory(serializerFactory); + mapper.setDeserializerProvider(deserializerProvider); + } + } + } + } + } + if ( cls != null && mapper == null ) { + return MAPPER; + } + if (mapper == null) { + mapper = new ObjectMapper(); + mapper.registerModule(new JsonOrgModule()); + } + // Let's honor the Jackson annotations first + AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); + AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); + AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); + mapper.setDeserializationConfig(mapper.getDeserializationConfig().withAnnotationIntrospector(pair) + .without(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES) + .withDateFormat(StdDateFormat.getBlueprintISO8601Format())); + mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(pair) + .withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL) + .withDateFormat(StdDateFormat.getBlueprintISO8601Format())); + return mapper; + } + + public static JsonFactory getJsonFactory() { + return FACTORY; + } + + public static String toString(JsonNode node) { + try { + return MAPPER.writeValueAsString(node); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static String toString(JsonParser parser) { + try { + JsonFactory jsonFactory = getJsonFactory(); + 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 = getJsonFactory(); + try { + return jsonFactory.createJsonParser(content); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static JsonParser createJsonParser(InputStream content) { + JsonFactory jsonFactory = getJsonFactory(); + try { + return jsonFactory.createJsonParser(content); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static JsonParser createJsonParser(Reader content) { + JsonFactory jsonFactory = getJsonFactory(); + try { + return jsonFactory.createJsonParser(content); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static void write(JsonNode node, OutputStream out) { + try { + JsonFactory jsonFactory = getJsonFactory(); + JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); + generator.writeTree(node); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static void write(JsonParser parser, OutputStream out) { + try { + JsonFactory jsonFactory = getJsonFactory(); + JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); + JsonNode node = parser.readValueAs(JsonNode.class); + generator.writeTree(node); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static JSONObject read(InputStream is) throws IOException { + try { + return MAPPER.readValue(is, JSONObject.class); + } catch (Exception e) { + throw new IOException(e); + } + } + + /** + * Read from String into a org.json.JSONObject + * @param json + * @return + */ + public static JSONObject read(String json) { + try { + return MAPPER.readValue(json, JSONObject.class); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + + public static void write(JSONObject json, OutputStream out) throws IOException { + try { + MAPPER.writeValue(out, json); + } catch (Exception e) { + throw new IOException(e); + } + } + + public static String write(JSONObject json) throws IOException { + try { + return MAPPER.writeValueAsString(json); + } catch (Exception e) { + throw new IOException(e); + } + } + +} diff --git a/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java new file mode 100644 index 0000000000..2b77054934 --- /dev/null +++ b/sca-java-2.x/tags/2.0-RC2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java @@ -0,0 +1,88 @@ +/* + * 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.math.BigDecimal; + +import org.apache.tuscany.sca.databinding.PullTransformer; +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.apache.tuscany.sca.databinding.json.JSONHelper; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.map.ObjectMapper; + +/** + * @version $Rev$ $Date$ + */ +public class Object2JSON implements PullTransformer<Object, Object> { + + public Object2JSON() { + super(); + } + + public Object transform(Object source, TransformationContext context) { + if (source == null) { + return null; + } + + Class<?> targetType = null; + if (context != null && context.getTargetDataType() != null) { + targetType = context.getTargetDataType().getPhysical(); + } + if (targetType == null) { + targetType = String.class; + } + try { + if (targetType != null && targetType.isPrimitive()) { + return source; + } + ObjectMapper mapper = JacksonHelper.createObjectMapper(targetType); + String value = mapper.writeValueAsString(source); + if (JsonNode.class.isAssignableFrom(targetType)) { + return JacksonHelper.createJsonParser(value).readValueAsTree(); + } else if (targetType == String.class || targetType == Object.class || targetType.isPrimitive()) { + return value; + } else if (targetType == BigDecimal.class) { + return value.toString(); + } else if (JsonParser.class.isAssignableFrom(targetType)) { + return JacksonHelper.createJsonParser(value); + } else { + return JSONHelper.toJSON(value, targetType); + } + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public String getSourceDataBinding() { + return JavaBeansDataBinding.NAME; + } + + public String getTargetDataBinding() { + return JSONDataBinding.NAME; + } + + public int getWeight() { + return 5000; + } +} |