Search in sources :

Example 6 with PropertyFilter

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

the class PropertyFilterTest method test_1.

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

        public boolean apply(Object source, String name, Object value) {
            if ("id".equals(name)) {
                return true;
            }
            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("{\"id\":0}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 7 with PropertyFilter

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

the class PropertyFilterTest 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)

Example 8 with PropertyFilter

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

the class PropertyFilterTest method test_2.

public void test_2() 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);
    A a = new A();
    a.setName("chennp2008");
    serializer.write(a);
    String text = out.toString();
    Assert.assertEquals("{\"name\":\"chennp2008\"}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 9 with PropertyFilter

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

the class PropertyFilterTest2 method test_0.

public void test_0() throws Exception {
    class VO {

        public int id;

        public String name;
    }
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            return "id".equals(name);
        }
    };
    VO vo = new VO();
    vo.id = 123;
    vo.name = "gaotie";
    String text = JSON.toJSONString(vo, filter);
    Assert.assertEquals("{\"id\":123}", text);
}
Also used : PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter)

Example 10 with PropertyFilter

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

the class PropertyFilter_bool_field method test_2.

public void test_2() 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);
    A a = new A();
    a.name = "chennp2008";
    serializer.write(a);
    String text = out.toString();
    Assert.assertEquals("{\"name\":\"chennp2008\"}", 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