Search in sources :

Example 41 with PropertyFilter

use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.

the class PropertyFilterClassLevelTest_private method test_0.

public void test_0() throws Exception {
    Object[] array = { new ModelA(), new ModelB() };
    SerializeConfig config = new SerializeConfig();
    //
    config.addFilter(//
    ModelA.class, new PropertyFilter() {

        @Override
        public boolean apply(Object object, String name, Object value) {
            return false;
        }
    });
    //
    config.addFilter(//
    ModelB.class, new PropertyFilter() {

        @Override
        public boolean apply(Object object, String name, Object value) {
            return true;
        }
    });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{},{\"id\":1002}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter)

Example 42 with PropertyFilter

use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.

the class PropertyFilterTest method test_3.

public void test_3() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            if ("name".equals(name)) {
                return true;
            }
            return false;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "chennp2008");
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 43 with PropertyFilter

use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.

the class PropertyFilterTest method test_4.

public void test_4() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            if ("name".equals(name)) {
                return false;
            }
            return true;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 3);
    map.put("name", "chennp2008");
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{\"id\":3}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 44 with PropertyFilter

use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.

the class PropertyFilter_bool_field method test_3.

public void test_3() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            if ("name".equals(name)) {
                return true;
            }
            return false;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "chennp2008");
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 45 with PropertyFilter

use of com.alibaba.fastjson.serializer.PropertyFilter in project fastjson by alibaba.

the class PropertyFilter_bool_field method test_0.

public void test_0() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            return false;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getPropertyFilters().add(filter);
    A a = new A();
    serializer.write(a);
    String text = out.toString();
    Assert.assertEquals("{}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)59 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)54 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)54 HashMap (java.util.HashMap)20 JSONException (com.alibaba.fastjson.JSONException)2 NameFilter (com.alibaba.fastjson.serializer.NameFilter)2 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)2