diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-13 17:42:16 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-13 17:42:16 +0000 |
commit | efccdd821b68280ee3b73c8ef5cda121bc27f620 (patch) | |
tree | c43374b274b5bc3e943826af279ff23a526174b9 /sca-java-2.x/trunk/modules/databinding-json/src/main | |
parent | d2413f4a139158cc70a996c92b0730e64a239c87 (diff) |
Improve jaxb/json databindings and jsonrpc binding to better handle interfaces
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1034820 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/databinding-json/src/main')
3 files changed, 72 insertions, 23 deletions
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 ca28ec5833..c7ae9d636d 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 @@ -38,7 +38,6 @@ public class JSON2Object implements PullTransformer<Object, Object> { public JSON2Object() { super(); - mapper = JacksonHelper.createObjectMapper(); } public Object transform(Object source, TransformationContext context) { @@ -47,6 +46,8 @@ public class JSON2Object implements PullTransformer<Object, Object> { } try { + Class<?> cls = context.getTargetDataType().getPhysical(); + ObjectMapper mapper = JacksonHelper.createObjectMapper(cls); JavaType javaType = TypeFactory.type(context.getTargetDataType().getGenericType()); if (source instanceof String) { String sourceString = (String) source; 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 ae3bb8fd48..00ddc2601d 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 @@ -25,6 +25,10 @@ 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; @@ -32,16 +36,63 @@ 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.deser.CustomDeserializerFactory; +import org.codehaus.jackson.map.deser.StdDeserializerProvider; import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; +import org.codehaus.jackson.map.ser.CustomSerializerFactory; import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; +import org.codehaus.jackson.xc.XmlAdapterJsonDeserializer; +import org.codehaus.jackson.xc.XmlAdapterJsonSerializer; /** * */ public class JacksonHelper { - public static ObjectMapper createObjectMapper() { - ObjectMapper mapper = new ObjectMapper(); + private 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(); + CustomDeserializerFactory deserializerFactory = new CustomDeserializerFactory(); + 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); + deserializerFactory.addSpecificMapping(a.type(), deserializer); + serializerFactory.addGenericMapping(a.type(), serializer); + StdDeserializerProvider deserializerProvider = + new StdDeserializerProvider(deserializerFactory); + mapper = new ObjectMapper(); + mapper.setSerializerFactory(serializerFactory); + mapper.setDeserializerProvider(deserializerProvider); + } + } + } + } + } + if (mapper == null) { + mapper = new ObjectMapper(); + } AnnotationIntrospector primary = new JaxbAnnotationIntrospector(); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); @@ -52,13 +103,13 @@ public class JacksonHelper { return mapper; } + public static JsonFactory getJsonFactory() { + return FACTORY; + } + 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(); + return MAPPER.writeValueAsString(node); } catch (IOException e) { throw new IllegalArgumentException(e); } @@ -66,7 +117,7 @@ public class JacksonHelper { public static String toString(JsonParser parser) { try { - JsonFactory jsonFactory = new JsonFactory(); + JsonFactory jsonFactory = getJsonFactory(); StringWriter sw = new StringWriter(); JsonGenerator generator = jsonFactory.createJsonGenerator(sw); JsonNode node = parser.readValueAs(JsonNode.class); @@ -78,7 +129,7 @@ public class JacksonHelper { } public static JsonParser createJsonParser(String content) { - JsonFactory jsonFactory = new JsonFactory(); + JsonFactory jsonFactory = getJsonFactory(); try { return jsonFactory.createJsonParser(content); } catch (IOException e) { @@ -87,26 +138,26 @@ public class JacksonHelper { } public static JsonParser createJsonParser(InputStream content) { - JsonFactory jsonFactory = new JsonFactory(); + JsonFactory jsonFactory = getJsonFactory(); try { return jsonFactory.createJsonParser(content); } catch (IOException e) { throw new IllegalArgumentException(e); } } - + public static JsonParser createJsonParser(Reader content) { - JsonFactory jsonFactory = new JsonFactory(); + 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 = new JsonFactory(); + JsonFactory jsonFactory = getJsonFactory(); JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); generator.writeTree(node); } catch (IOException e) { @@ -116,7 +167,7 @@ public class JacksonHelper { public static void write(JsonParser parser, OutputStream out) { try { - JsonFactory jsonFactory = new JsonFactory(); + JsonFactory jsonFactory = getJsonFactory(); JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); JsonNode node = parser.readValueAs(JsonNode.class); generator.writeTree(node); @@ -124,5 +175,5 @@ public class JacksonHelper { 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 d543d0554a..38b826b202 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 @@ -35,11 +35,9 @@ import org.codehaus.jackson.map.ObjectMapper; * @version $Rev$ $Date$ */ public class Object2JSON implements PullTransformer<Object, Object> { - private ObjectMapper mapper; public Object2JSON() { super(); - mapper = JacksonHelper.createObjectMapper(); } public Object transform(Object source, TransformationContext context) { @@ -58,12 +56,11 @@ public class Object2JSON implements PullTransformer<Object, Object> { if (targetType != null && targetType.isPrimitive()) { return source; } + ObjectMapper mapper = JacksonHelper.createObjectMapper(targetType); String value = mapper.writeValueAsString(source); - if (targetType == String.class || - targetType == Object.class || - targetType.isPrimitive()) { + if (targetType == String.class || targetType == Object.class || targetType.isPrimitive()) { return value; - } else if (targetType == BigDecimal.class){ + } else if (targetType == BigDecimal.class) { return value.toString(); } else if (JsonNode.class.isAssignableFrom(targetType)) { return JacksonHelper.createJsonParser(value).readValueAsTree(); |