tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r1383017 - in /tuscany/sca-java-2.x/trunk/modules/databinding-json: META-INF/ src/main/java/org/apache/tuscany/sca/databinding/json/jackson/ src/test/java/org/apache/tuscany/sca/databinding/json/jackson/
Date Mon, 10 Sep 2012 18:11:53 GMT
Author: rfeng
Date: Mon Sep 10 18:11:53 2012
New Revision: 1383017

URL: http://svn.apache.org/viewvc?rev=1383017&view=rev
Log:
Start to add the ability to allow partial json serialization

Modified:
    tuscany/sca-java-2.x/trunk/modules/databinding-json/META-INF/MANIFEST.MF
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
    tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/META-INF/MANIFEST.MF?rev=1383017&r1=1383016&r2=1383017&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/META-INF/MANIFEST.MF (original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/META-INF/MANIFEST.MF Mon Sep 10 18:11:53
2012
@@ -27,6 +27,7 @@ Import-Package: javax.xml.namespace,
  org.codehaus.jackson.map.introspect,
  org.codehaus.jackson.map.module,
  org.codehaus.jackson.map.ser,
+ org.codehaus.jackson.map.ser.impl,
  org.codehaus.jackson.map.type,
  org.codehaus.jackson.map.util,
  org.codehaus.jackson.type,

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java?rev=1383017&r1=1383016&r2=1383017&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
Mon Sep 10 18:11:53 2012
@@ -22,6 +22,8 @@ package org.apache.tuscany.sca.databindi
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.List;
+import java.util.Set;
 
 import org.apache.tuscany.sca.databinding.PushTransformer;
 import org.apache.tuscany.sca.databinding.TransformationContext;
@@ -30,6 +32,9 @@ import org.apache.tuscany.sca.databindin
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.JsonParser;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.ser.FilterProvider;
+import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
+import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
 import org.json.JSONArray;
 import org.json.JSONObject;
 
@@ -50,16 +55,17 @@ public class JSON2OutputStream implement
         if (source == null) {
             return;
         }
+
         if (source instanceof InputStream) {
             try {
-                InputStream input = (InputStream) source;
+                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) {
+            } catch (IOException e) {
                 throw new TransformationException(e);
             }
         } else if (source instanceof JsonNode) {
@@ -79,7 +85,12 @@ public class JSON2OutputStream implement
             } else {
                 ObjectMapper mapper = JacksonHelper.createObjectMapper(source.getClass());
                 try {
-                    mapper.writeValue(sink, source);
+                    FilterProvider filterProvider = JacksonHelper.configureFilterProvider(context);
+                    if (filterProvider != null) {
+                        mapper.writer(filterProvider).writeValue(sink, source);
+                    } else {
+                        mapper.writeValue(sink, source);
+                    }
                 } catch (Throwable e) {
                     throw new TransformationException(e);
                 }

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java?rev=1383017&r1=1383016&r2=1383017&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
Mon Sep 10 18:11:53 2012
@@ -24,11 +24,13 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.StringWriter;
+import java.util.Set;
 
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
 
+import org.apache.tuscany.sca.databinding.TransformationContext;
 import org.codehaus.jackson.JsonEncoding;
 import org.codehaus.jackson.JsonFactory;
 import org.codehaus.jackson.JsonGenerator;
@@ -41,9 +43,13 @@ import org.codehaus.jackson.map.ObjectMa
 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.AnnotatedClass;
 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.ser.FilterProvider;
+import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
+import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
 import org.codehaus.jackson.map.util.StdDateFormat;
 import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
 import org.codehaus.jackson.xc.XmlAdapterJsonDeserializer;
@@ -100,7 +106,7 @@ public class JacksonHelper {
                 }
             }
         }
-        if ( cls != null && mapper == null ) {
+        if (cls != null && mapper == null) {
             return MAPPER;
         }
         if (mapper == null) {
@@ -108,7 +114,15 @@ public class JacksonHelper {
             mapper.registerModule(new JsonOrgModule());
         }
         // Let's honor the Jackson annotations first
-        AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
+        AnnotationIntrospector primary = new JacksonAnnotationIntrospector() {
+
+            @Override
+            public Object findFilterId(AnnotatedClass annotatedClass) {
+                Object filterId = super.findFilterId(annotatedClass);
+                return filterId == null ? "tuscanyFilter" : filterId;
+            }
+
+        };
         AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
         AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
         mapper.setDeserializationConfig(mapper.getDeserializationConfig().withAnnotationIntrospector(pair)
@@ -117,6 +131,7 @@ public class JacksonHelper {
         mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(pair)
             .withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL)
             .withDateFormat(StdDateFormat.getBlueprintISO8601Format()));
+
         return mapper;
     }
 
@@ -230,4 +245,20 @@ public class JacksonHelper {
         }
     }
 
+    private final static SimpleBeanPropertyFilter DEFAULT_FILTER = SimpleBeanPropertyFilter.serializeAllExcept();
+
+    public static FilterProvider configureFilterProvider(TransformationContext context) {
+        SimpleBeanPropertyFilter filter = DEFAULT_FILTER;
+        if (context != null) {
+            Set<String> included = (Set<String>)context.getMetadata().get("includedFields");
+            Set<String> excluded = (Set<String>)context.getMetadata().get("excludedFields");
+            if (included != null && !included.isEmpty()) {
+                filter = SimpleBeanPropertyFilter.filterOutAllExcept(included);
+            } else if (excluded != null && !excluded.isEmpty()) {
+                filter = SimpleBeanPropertyFilter.serializeAllExcept(excluded);
+            }
+        }
+        FilterProvider filters = new SimpleFilterProvider().addFilter("tuscanyFilter", filter);
+        return filters;
+    }
 }

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java?rev=1383017&r1=1383016&r2=1383017&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
Mon Sep 10 18:11:53 2012
@@ -30,6 +30,7 @@ import org.apache.tuscany.sca.databindin
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.JsonParser;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.ser.FilterProvider;
 
 /**
  * @version $Rev$ $Date$
@@ -57,7 +58,13 @@ public class Object2JSON implements Pull
                 return source;
             }
             ObjectMapper mapper = JacksonHelper.createObjectMapper(targetType);
-            String value = mapper.writeValueAsString(source);
+            String value = null;
+            FilterProvider filterProvider = JacksonHelper.configureFilterProvider(context);
+            if (filterProvider != null) {
+                value = mapper.writer(filterProvider).writeValueAsString(source);
+            } else {
+                value = mapper.writeValueAsString(source);
+            }
             if (JsonNode.class.isAssignableFrom(targetType)) {
                 return JacksonHelper.createJsonParser(value).readValueAsTree();
             } else if (targetType == String.class || targetType == Object.class || targetType.isPrimitive())
{

Modified: tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java?rev=1383017&r1=1383016&r2=1383017&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java
Mon Sep 10 18:11:53 2012
@@ -21,6 +21,7 @@ package org.apache.tuscany.sca.databindi
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
@@ -252,6 +253,34 @@ public class Object2JSONTestCase {
     }
 
     @Test
+    public void testBean2JSONWithFilter() throws Exception {
+        MyBean me = new MyBean();
+        me.setAge(30);
+        me.setBooks(new ArrayList<String>());
+        me.setFriends(new String[] {"John", "Mike"});
+        me.setVip(true);
+        me.setName("Me");
+        me.setDate(new Date());
+        YourBean you = new YourBean();
+        you.setId(123);
+        you.setName(null);
+        me.setYou(you);
+        Object2JSON t1 = new Object2JSON();
+        TransformationContext context = new TransformationContextImpl();
+        context.getMetadata().put("includedFields", Collections.singleton("name"));
+        Object result = t1.transform(me, context);
+        System.out.println(result);
+        Assert.assertTrue(result.toString().contains("name"));
+        Assert.assertFalse(result.toString().contains("age"));
+        context = new TransformationContextImpl();
+        context.getMetadata().put("excludedFields", Collections.singleton("name"));
+        result = t1.transform(me, context);
+        System.out.println(result);
+        Assert.assertFalse(result.toString().contains("name"));
+        Assert.assertTrue(result.toString().contains("age"));
+    }
+
+    @Test
     public void testString2JSON() throws Exception {
         Object2JSON t1 = new Object2JSON();
         Object result = t1.transform("ABC", null);



Mime
View raw message