You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/10/25 04:58:55 UTC

svn commit: r588117 - in /incubator/tuscany/java/sca/modules/databinding-json/src: main/java/org/apache/tuscany/sca/databinding/json/ main/resources/META-INF/services/ test/java/org/apache/tuscany/sca/databinding/json/

Author: rfeng
Date: Wed Oct 24 19:58:54 2007
New Revision: 588117

URL: http://svn.apache.org/viewvc?rev=588117&view=rev
Log:
Add a transformer to convert data from simple or java bean types to JSON

Added:
    incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java   (with props)
    incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java   (with props)
Modified:
    incubator/tuscany/java/sca/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer

Added: incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java?rev=588117&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java Wed Oct 24 19:58:54 2007
@@ -0,0 +1,139 @@
+/*
+ * 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;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+
+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.impl.BaseTransformer;
+import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl;
+import org.apache.tuscany.sca.databinding.javabeans.JavaBeansDataBinding;
+import org.apache.tuscany.sca.interfacedef.util.TypeInfo;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+
+public class JavaBean2JSON extends BaseTransformer<Object, Object> implements PullTransformer<Object, Object> {
+    private final static Comparator<PropertyDescriptor> COMPARATOR = new Comparator<PropertyDescriptor>() {
+        public int compare(PropertyDescriptor o1, PropertyDescriptor o2) {
+            return o1.getName().compareTo(o2.getName());
+        }
+    };
+
+    private static final Object[] NULL = null;
+    private static final SimpleTypeMapperImpl MAPPER = new SimpleTypeMapperImpl();
+
+    public Object transform(Object source, TransformationContext context) {
+        try {
+            return toJSON(source);
+        } catch (Exception e) {
+            throw new TransformationException(e);
+        }
+    }
+
+    public Object toJSON(Object source) throws Exception {
+        if (source == null) {
+            return JSONObject.NULL;
+        }
+        Class<?> type = source.getClass();
+        if (isSimpleType(type)) {
+            return source;
+        } else if (type.isArray()) {
+            JSONArray array = new JSONArray();
+            int i1 = Array.getLength(source);
+            for (int j = 0; j < i1; j++) {
+                Object o = Array.get(source, j);
+                array.put(toJSON(o));
+            }
+            return array;
+        } else if (Collection.class.isAssignableFrom(type)) {
+            Collection c = (Collection)source;
+            JSONArray array = new JSONArray();
+            for (Object element : c) {
+                array.put(toJSON(element));
+            }
+            return array;
+        }
+        JSONObject json = new JSONObject();
+        BeanInfo beanInfo = Introspector.getBeanInfo(type);
+        PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
+        Collections.sort(Arrays.asList(propDescs), COMPARATOR);
+
+        for (int i = 0; i < propDescs.length; i++) {
+            PropertyDescriptor propDesc = propDescs[i];
+            Class<?> pType = propDesc.getPropertyType();
+            if ("class".equals(propDesc.getName())) {
+                continue;
+            }
+            Object pValue = propDesc.getReadMethod().invoke(source, NULL);
+            json.put(propDesc.getName(), toJSON(pValue));
+        }
+        return json;
+
+    }
+
+    @Override
+    protected Class getSourceType() {
+        return Object.class;
+    }
+
+    @Override
+    protected Class getTargetType() {
+        return Object.class;
+    }
+
+    public JavaBean2JSON() {
+    }
+
+    private static boolean isSimpleType(Class<?> javaType) {
+        return SimpleTypeMapperImpl.getXMLType(javaType) != null;
+    }
+
+    private static String getStringValue(Object o) {
+        if (o == null) {
+            return null;
+        }
+        TypeInfo info = SimpleTypeMapperImpl.getXMLType(o.getClass());
+        if (info != null) {
+            return MAPPER.toXMLLiteral(info.getQName(), o, null);
+        } else {
+            return String.valueOf(o);
+        }
+    }
+
+    @Override
+    public String getSourceDataBinding() {
+        return JavaBeansDataBinding.NAME;
+    }
+
+    @Override
+    public String getTargetDataBinding() {
+        return JSONDataBinding.NAME;
+    }
+
+}

Propchange: incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSON.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer?rev=588117&r1=588116&r2=588117&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer (original)
+++ incubator/tuscany/java/sca/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer Wed Oct 24 19:58:54 2007
@@ -18,4 +18,6 @@
 # Implementation classes for the transformers
 org.apache.tuscany.sca.databinding.json.JSON2XMLStreamReader;source=org.codehaus.jettison.json.JSONObject,target=javax.xml.stream.XMLStreamReader,weight=500
 org.apache.tuscany.sca.databinding.json.XMLStreamReader2JSON;source=javax.xml.stream.XMLStreamReader,target=org.codehaus.jettison.json.JSONObject,weight=500
+org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=java:complexType,target=org.codehaus.jettison.json.JSONObject,weight=20000
+org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=java:simpleType,target=org.codehaus.jettison.json.JSONObject,weight=20000
 org.apache.tuscany.sca.databinding.json.axiom.JSON2OMElement;source=org.codehaus.jettison.json.JSONObject,target=org.apache.axiom.om.OMElement,weight=500

Added: incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java?rev=588117&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java Wed Oct 24 19:58:54 2007
@@ -0,0 +1,134 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.codehaus.jettison.json.JSONObject;
+import org.junit.Test;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JavaBean2JSONTestCase {
+
+    private static class MyBean {
+        private String name;
+        private int age;
+        private boolean vip;
+        private String friends[];
+        private List<String> books;
+        private YourBean you;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public int getAge() {
+            return age;
+        }
+
+        public void setAge(int age) {
+            this.age = age;
+        }
+
+        public boolean isVip() {
+            return vip;
+        }
+
+        public void setVip(boolean vip) {
+            this.vip = vip;
+        }
+
+        public String[] getFriends() {
+            return friends;
+        }
+
+        public void setFriends(String[] friends) {
+            this.friends = friends;
+        }
+
+        public List<String> getBooks() {
+            return books;
+        }
+
+        public void setBooks(List<String> books) {
+            this.books = books;
+        }
+
+        public YourBean getYou() {
+            return you;
+        }
+
+        public void setYou(YourBean you) {
+            this.you = you;
+        }
+
+    }
+
+    private static class YourBean {
+        private int id;
+        private String name;
+
+        public int getId() {
+            return id;
+        }
+
+        public void setId(int id) {
+            this.id = id;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+    
+    @Test
+    public void testBean2JSON() throws Exception {
+        JavaBean2JSON converter = new JavaBean2JSON();
+        MyBean me = new MyBean();
+        me.setAge(30);
+        me.setBooks(new ArrayList<String>());
+        me.setFriends(new String[] {"John", "Mike"});
+        me.setVip(true);
+        me.setName("Me");
+        YourBean you = new YourBean();
+        you.setId(123);
+        you.setName(null);
+        me.setYou(you);
+        Object result = converter.transform(me, null);
+        Assert.assertTrue(result instanceof JSONObject);
+        System.out.println(result);
+        String json =
+            "{\"age\":30,\"books\":[],\"friends\":[\"John\",\"Mike\"],\"name\":\"Me\",\"vip\":true,\"you\":{\"id\":123,\"name\":null}}";
+        Assert.assertEquals(json, result.toString());
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/JavaBean2JSONTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org