Permalink
Browse files

add SimplePropertyFilter

  • Loading branch information...
1 parent 10aecda commit 5135e26cb77455419228fb3334b764abeca6dbc6 wenshao committed Jul 29, 2012
@@ -35,6 +35,7 @@
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.FieldDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
+import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
@@ -236,7 +237,7 @@ public static final JSONObject parseObject(String text) {
}
fieldDeser.setValue(object, refValue);
}
-
+
return size;
}
@@ -382,21 +383,22 @@ public static final String toJSONString(Object object, SerializerFeature... feat
out.close();
}
}
-
+
/**
* @since 1.1.14
*/
- public static final String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features) {
+ public static final String toJSONStringWithDateFormat(Object object, String dateFormat,
+ SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
try {
JSONSerializer serializer = new JSONSerializer(out);
for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
serializer.config(feature, true);
}
-
+
serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
-
+
if (dateFormat != null) {
serializer.setDateFormat(dateFormat);
}
@@ -409,6 +411,30 @@ public static final String toJSONStringWithDateFormat(Object object, String date
}
}
+ public static final String toJSONStringWithPropertyFilter(Object object, PropertyFilter propertyFilter,
+ SerializerFeature... features) {
+ SerializeWriter out = new SerializeWriter();
+
+ try {
+ JSONSerializer serializer = new JSONSerializer(out);
+ for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
+ serializer.config(feature, true);
+ }
+
+ serializer.config(SerializerFeature.WriteDateUseDateFormat, true);
+
+ if (propertyFilter != null) {
+ serializer.getPropertyFilters().add(propertyFilter);
+ }
+
+ serializer.write(object);
+
+ return out.toString();
+ } finally {
+ out.close();
+ }
+ }
+
public static final byte[] toJSONBytes(Object object, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();
@@ -600,6 +626,6 @@ public static final Object toJSON(Object javaObject, ParserConfig mapping) {
public static final <T> T toJavaObject(JSON json, Class<T> clazz) {
return TypeUtils.cast(json, clazz, ParserConfig.getGlobalInstance());
}
-
+
public final static String VERSION = "1.1.22";
}
@@ -0,0 +1,58 @@
+package com.alibaba.fastjson.serializer;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class SimplePropertyFilter implements PropertyFilter {
+
+ private final Class<?> clazz;
+ private final Set<String> includes = new HashSet<String>();
+ private final Set<String> excludes = new HashSet<String>();
+
+ public SimplePropertyFilter(String... properties){
+ this (null, properties);
+ }
+
+ public SimplePropertyFilter(Class<?> clazz, String... properties){
+ super();
+ this.clazz = clazz;
+ for (String item : properties) {
+ if (item != null) {
+ this.includes.add(item);
+ }
+ }
+ }
+
+ public Class<?> getClazz() {
+ return clazz;
+ }
+
+ public Set<String> getIncludes() {
+ return includes;
+ }
+
+ public Set<String> getExcludes() {
+ return excludes;
+ }
+
+ public boolean apply(Object source, String name, Object value) {
+ if (source == null) {
+ return true;
+ }
+
+ if (clazz != null && !clazz.isInstance(source)) {
+ return true;
+ }
+
+ if (this.excludes.contains(name)) {
+ return false;
+ }
+
+ if (includes.size() == 0 || includes.contains(name)) {
+ return true;
+ }
+
+ return false;
+ }
+
+}
@@ -0,0 +1,153 @@
+package com.alibaba.json.bvt.serializer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SimplePropertyFilter;
+
+public class SimplePropertyFilterTest extends TestCase {
+
+ private VO vo;
+
+ private A a;
+
+ private Map<String, Object> map;
+
+ protected void setUp() throws Exception {
+ vo = new VO();
+ vo.setId(123);
+ vo.setName("sandzhangtoo");
+
+ a = new A();
+ a.setId(123);
+ a.setName("sandzhangtoo");
+
+ map = new HashMap<String, Object>();
+ map.put("id", 123);
+ map.put("name", "sandzhangtoo");
+ map.put(null, null);
+ }
+
+ public void test_name() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class, "name");
+ Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_name_0() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter("name");
+ Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_name_a() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class, "name");
+ Assert.assertEquals("{\"id\":123,\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(a, filter));
+ }
+
+ public void test_name_a1() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter("name");
+ Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(a, filter));
+ }
+
+ public void test_id() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class, "id");
+ Assert.assertEquals("{\"id\":123}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_id_0() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter("id");
+ Assert.assertEquals("{\"id\":123}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_map() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class, "name");
+ Assert.assertEquals("{\"id\":123,\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(map, filter));
+ }
+
+ public void test_map_id() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter("id");
+ Assert.assertEquals("{\"id\":123}", JSON.toJSONStringWithPropertyFilter(map, filter));
+ }
+
+ public void test_map_name() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter("name");
+ Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(map, filter));
+ }
+
+ public void test_all() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class);
+ Assert.assertEquals("{\"id\":123,\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_all_map() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class);
+ Assert.assertEquals("{\"id\":123,\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(map, filter));
+ }
+
+ public void test_exclude_id() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class);
+ filter.getExcludes().add("id");
+ Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_exclude_id_map() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class);
+ filter.getExcludes().add("id");
+ Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public void test_exclude_name() throws Exception {
+ SimplePropertyFilter filter = new SimplePropertyFilter(VO.class);
+ filter.getExcludes().add("name");
+ Assert.assertEquals("{\"id\":123}", JSON.toJSONStringWithPropertyFilter(vo, filter));
+ }
+
+ public static class VO {
+
+ 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;
+ }
+
+ }
+
+ public static class A {
+
+ 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;
+ }
+
+ }
+}

0 comments on commit 5135e26

Please sign in to comment.