Search in sources :

Example 1 with ValueFilter

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

the class ValueFilterTest method test_valuefilter_2.

public void test_valuefilter_2() throws Exception {
    ValueFilter filter = new ValueFilter() {

        public Object process(Object source, String name, Object value) {
            if (name.equals("name")) {
                return "AAA";
            }
            return value;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getValueFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", null);
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{\"name\":\"AAA\"}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 2 with ValueFilter

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

the class ValueFilterTest method test_toJSONString.

public void test_toJSONString() throws Exception {
    ValueFilter filter = new ValueFilter() {

        public Object process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "AAA";
            }
            return value;
        }
    };
    Assert.assertEquals("{\"id\":\"AAA\"}", JSON.toJSONString(new A(), filter));
}
Also used : A(com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 3 with ValueFilter

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

the class ValueFilterTest_field method test_valuefilter_3.

public void test_valuefilter_3() throws Exception {
    ValueFilter filter = new ValueFilter() {

        public Object process(Object source, String name, Object value) {
            if (name.equals("name")) {
                return null;
            }
            return value;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getValueFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "AA");
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 4 with ValueFilter

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

the class ValueFilterTest_field method test_valuefilter_1.

public void test_valuefilter_1() throws Exception {
    ValueFilter filter = new ValueFilter() {

        public Object process(Object source, String name, Object value) {
            if (name.equals("name")) {
                return "AAA";
            }
            return value;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getValueFilters().add(filter);
    A a = new A();
    serializer.write(a);
    String text = out.toString();
    Assert.assertEquals("{\"id\":0,\"name\":\"AAA\"}", text);
}
Also used : A(com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A) SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 5 with ValueFilter

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

the class ValueFilterTest_field method test_toJSONString.

public void test_toJSONString() throws Exception {
    ValueFilter filter = new ValueFilter() {

        public Object process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "AAA";
            }
            return value;
        }
    };
    Assert.assertEquals("{\"id\":\"AAA\"}", JSON.toJSONString(new A(), filter));
}
Also used : A(com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Aggregations

ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)33 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)19 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)19 HashMap (java.util.HashMap)10 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)6 NameFilter (com.alibaba.fastjson.serializer.NameFilter)3 JSONException (com.alibaba.fastjson.JSONException)2 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)2 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)2 FastJsonHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4)2 Bean (org.springframework.context.annotation.Bean)2 SerializeFilter (com.alibaba.fastjson.serializer.SerializeFilter)1 Map (java.util.Map)1