Search in sources :

Example 31 with ValueFilter

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

the class EnumFieldsTest7 method test_enum.

public void test_enum() throws Exception {
    Model model = new Model();
    model.t1 = Type.A;
    model.t2 = null;
    ValueFilter filter = new ValueFilter() {

        public Object process(Object object, String name, Object value) {
            return null;
        }
    };
    String text = JSON.toJSONString(model, filter, SerializerFeature.WriteMapNullValue);
    Assert.assertEquals("{\"t1\":null,\"t2\":null}", text);
}
Also used : ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 32 with ValueFilter

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

the class EnumFieldsTest8 method test_enum.

public void test_enum() throws Exception {
    Model model = new Model();
    model.t1 = Type.A;
    model.t2 = null;
    ValueFilter valueFilter = new ValueFilter() {

        public Object process(Object object, String name, Object value) {
            return value;
        }
    };
    SerializeFilter[] filters = { valueFilter };
    String text = // 
    JSON.toJSONString(// 
    model, // 
    SerializeConfig.getGlobalInstance(), filters, null, // 
    0, // 
    SerializerFeature.QuoteFieldNames, // 
    SerializerFeature.BrowserCompatible, SerializerFeature.WriteEnumUsingName);
    Assert.assertEquals("{\"t1\":\"A\"}", text);
}
Also used : SerializeFilter(com.alibaba.fastjson.serializer.SerializeFilter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 33 with ValueFilter

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

the class FilterDemo method test_secure.

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

        public Object process(Object source, String name, Object value) {
            if (name.equals("name")) {
                return "WSJ";
            }
            return value;
        }
    };
    NameFilter nameFilter = new NameFilter() {

        public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "ID";
            }
            return name;
        }
    };
    String text = "{\"id\":123,\"name\":\"WJH\"}";
    Object object = JSON.parse(text);
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getValueFilters().add(filter);
    serializer.getNameFilters().add(nameFilter);
    serializer.write(object);
    String outText = out.toString();
    System.out.println(outText);
}
Also used : NameFilter(com.alibaba.fastjson.serializer.NameFilter) 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