Search in sources :

Example 6 with ValueFilter

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

the class ValueClassLevelTest_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 ValueFilter() {

        @Override
        public Object process(Object object, String name, Object value) {
            return 30001;
        }
    });
    // 
    config.addFilter(// 
    ModelB.class, new ValueFilter() {

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

Example 7 with ValueFilter

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

the class ValueFilterTest_IntegerKey method test_namefilter.

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

        public Object process(Object source, String name, Object value) {
            if (name.equals("1001")) {
                return "wenshao";
            }
            return value;
        }
    };
    Map map = new HashMap();
    map.put(1001, 0);
    String text = JSON.toJSONString(map, filter);
    Assert.assertEquals("{1001:\"wenshao\"}", text);
}
Also used : HashMap(java.util.HashMap) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with ValueFilter

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

the class ValueFilterTest_field_boolean 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);
    Bean a = new Bean();
    serializer.write(a);
    String text = out.toString();
    Assert.assertEquals("{\"id\":false,\"name\":\"AAA\"}", text);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 9 with ValueFilter

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

the class ValueFilterTest_field_boolean 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 Bean(), filter));
}
Also used : ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 10 with ValueFilter

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

the class ValueFilterTest_field_long method test_valuefilter.

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

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

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