diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-30 19:00:47 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-30 19:00:47 +0000 |
commit | 396008440c7bcdb1cbb7a3c5d6270814dfb0e341 (patch) | |
tree | 5b36dc6a3ed53cb62560963a42b45a89b62dc4de /sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson | |
parent | ae9b105c6718dcb91b415e257acc8b8c8bf0c6c7 (diff) |
delete the beta2 branch as i'd not seen raymonds mail when i made it and we might as well wait till he's got his changes in before creating the beta2 branch
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1040700 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
5 files changed, 0 insertions, 464 deletions
diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java b/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java deleted file mode 100644 index 57b9d55b44..0000000000 --- a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java b/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java deleted file mode 100644 index c7ae9d636d..0000000000 --- a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.map.type.TypeFactory; -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 = TypeFactory.type(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.treeToValue((JsonNode)source, context.getTargetDataType().getPhysical()); - } 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/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java b/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java deleted file mode 100644 index 920485a540..0000000000 --- a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.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; - -/** - * - */ -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 JsonNode) { - JacksonHelper.write((JsonNode)source, sink); - } 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); - } - } - } - - public int getWeight() { - return 50; - } - -} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java b/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java deleted file mode 100644 index 00ddc2601d..0000000000 --- a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * 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.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 { - 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); - 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); - 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); - } - } - -} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java b/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java deleted file mode 100644 index 38b826b202..0000000000 --- a/sca-java-2.x/branches/2.0-Beta2/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * 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 (targetType == String.class || targetType == Object.class || targetType.isPrimitive()) { - return value; - } else if (targetType == BigDecimal.class) { - return value.toString(); - } 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); - } - } - - public String getSourceDataBinding() { - return JavaBeansDataBinding.NAME; - } - - public String getTargetDataBinding() { - return JSONDataBinding.NAME; - } - - public int getWeight() { - return 5000; - } -} |