Search in sources :

Example 16 with ValueFilter

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

the class FieldSerializerTest3 method toJSONString.

public static final String toJSONString(Object object, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                if ("id".equals(name)) {
                    return false;
                }
                return true;
            }
        });
        serializer.getNameFilters().add(new NameFilter() {

            public String process(Object source, String name, Object value) {
                return name;
            }
        });
        serializer.getValueFilters().add(new ValueFilter() {

            public Object process(Object source, String name, Object value) {
                if ("v".endsWith(name)) {
                    return "xxx";
                }
                return value;
            }
        });
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }
        serializer.write(object);
        return out.toString();
    } catch (StackOverflowError e) {
        throw new JSONException("maybe circular references", e);
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) JSONException(com.alibaba.fastjson.JSONException) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 17 with ValueFilter

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

the class FastjsonConfig method fastJsonHttpMessageConverter.

@Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.BrowserCompatible);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    ValueFilter valueFilter = new ValueFilter() {

        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);
    converter.setFastJsonConfig(fastJsonConfig);
    return converter;
}
Also used : FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) Bean(org.springframework.context.annotation.Bean)

Example 18 with ValueFilter

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

the class FastjsonConfig method fastJsonHttpMessageConverter.

@Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.BrowserCompatible);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    ValueFilter valueFilter = new ValueFilter() {

        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);
    converter.setFastJsonConfig(fastJsonConfig);
    return converter;
}
Also used : FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) Bean(org.springframework.context.annotation.Bean)

Example 19 with ValueFilter

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

the class Bug_for_issue_572_private method test_for_issue_1.

public void test_for_issue_1() throws Exception {
    Model model = new Model();
    model.id = 1001;
    model.name = "wenshao";
    ValueFilter valueFilter = new ValueFilter() {

        @Override
        public Object process(Object object, String name, Object value) {
            if (value instanceof Number) {
                return null;
            }
            return value;
        }
    };
    String text = JSON.toJSONString(model, valueFilter, SerializerFeature.WriteNonStringValueAsString);
    Assert.assertEquals("{\"id\":\"1001\",\"name\":\"wenshao\"}", text);
}
Also used : ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 20 with ValueFilter

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

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

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