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/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java | |
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/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java')
-rw-r--r-- | sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java | 83 |
1 files changed, 67 insertions, 16 deletions
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); } } - + } |