Skip to content
Browse files

Start to add the ability to allow partial json serialization

git-svn-id: https://svn.apache.org/repos/asf/tuscany/sca-java-2.x/trunk@1383017 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 517ca30 commit e1f9f789665127a40424d145e9775cb2b5f966cd @raymondfeng raymondfeng committed Sep 10, 2012
View
1 modules/databinding-json/META-INF/MANIFEST.MF
@@ -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,
View
17 ...json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java
@@ -22,6 +22,8 @@
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.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 void transform(Object source, OutputStream sink, TransformationContext co
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 void transform(Object source, OutputStream sink, TransformationContext co
} 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);
}
View
35 ...ing-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java
@@ -24,11 +24,13 @@
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.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,15 +106,23 @@ public static ObjectMapper createObjectMapper(Class<?> cls) {
}
}
}
- if ( cls != null && mapper == null ) {
+ if (cls != null && mapper == null) {
return MAPPER;
}
if (mapper == null) {
mapper = new ObjectMapper();
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 static ObjectMapper createObjectMapper(Class<?> cls) {
mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(pair)
.withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL)
.withDateFormat(StdDateFormat.getBlueprintISO8601Format()));
+
return mapper;
}
@@ -230,4 +245,20 @@ public static String write(JSONObject json) throws IOException {
}
}
+ 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;
+ }
}
View
9 ...nding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSON.java
@@ -30,6 +30,7 @@
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 Object transform(Object source, TransformationContext context) {
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()) {
View
29 ...on/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java
@@ -21,6 +21,7 @@
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 void testBean2JSON() throws Exception {
}
@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);

0 comments on commit e1f9f78

Please sign in to comment.
Something went wrong with that request. Please try again.