Java 类com.alibaba.fastjson.serializer.ValueFilter 实例源码

项目:GitHub    文件:ValueFilterTest.java   
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);

    A a = new A();
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
项目:GitHub    文件:ValueFilterTest.java   
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);
}
项目:GitHub    文件:ValueFilterTest.java   
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);
}
项目:GitHub    文件:ValueFilterTest.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field.java   
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);

    A a = new A();
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
项目:GitHub    文件:ValueFilterTest_field.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_boolean.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_boolean.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_boolean.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_boolean.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_long.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_long.java   
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\":0,\"name\":\"AAA\"}", text);
}
项目:GitHub    文件:ValueFilterTest_field_long.java   
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);
}
项目:GitHub    文件:ValueFilterTest_field_long.java   
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);
}
项目:GitHub    文件:EnumFieldsTest8.java   
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);
}
项目:sns-todo    文件:WebMvcConfig.java   
private FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteDateUseDateFormat);
    fastJsonConfig.setSerializeFilters(new ValueFilter() {
        @Override
        public Object process(Object o, String s, Object source) {
            if (source == null) {
                return "";
            }
            if (source instanceof Date) {
                return ((Date) source).getTime();
            }
            return source;
        }
    });
    httpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return httpMessageConverter;
}
项目:boohee_v5.6    文件:JSON.java   
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) {
    if (filter != null) {
        if (filter instanceof PropertyPreFilter) {
            serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
        }
        if (filter instanceof NameFilter) {
            serializer.getNameFilters().add((NameFilter) filter);
        }
        if (filter instanceof ValueFilter) {
            serializer.getValueFilters().add((ValueFilter) filter);
        }
        if (filter instanceof PropertyFilter) {
            serializer.getPropertyFilters().add((PropertyFilter) filter);
        }
        if (filter instanceof BeforeFilter) {
            serializer.getBeforeFilters().add((BeforeFilter) filter);
        }
        if (filter instanceof AfterFilter) {
            serializer.getAfterFilters().add((AfterFilter) filter);
        }
    }
}
项目:westjson    文件:WestJsonThiner.java   
private ValueFilter createFirstValueFilter() {
    return new ValueFilter() {
        @Override
        public Object process(Object source, String name, Object value) {
            if (!(value instanceof String)) return value;

            String strValue = (String) value;
            if (bytesLen(strValue) <= 3) return value;

            String mappedValue = innerValueMapping.get(strValue);
            if (mappedValue != null) return mappedValue;

            valueBag.add(strValue);
            return value;
        }
    };
}
项目:westjson    文件:WestJsonThiner.java   
public ValueFilter createSecondValueFilter() {
    // 第一遍过滤器,如果启动了值映射分析,则还需要第二个过滤器,在第一次过滤器中进行值映射分析。
    return new ValueFilter() {
        @Override
        public Object process(Object source, String name, Object value) {
            if (!(value instanceof String)) return value;

            val key = (String) value;
            if (bytesLen(key) <= 3) return key;

            String valueCode = innerValueMapping.get(key);
            if (valueCode != null) return '@' + valueCode;

            if (valueBag.count(key) <= 2) return value;

            valueCode = BaseX.base62(innerValueMapping.size());
            innerValueMapping.put(key, valueCode);
            return '@' + valueCode;
        }
    };
}
项目:GitHub    文件:ValueFilterTest.java   
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));
}
项目:GitHub    文件:ValueFilterTest_field.java   
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));
}
项目:GitHub    文件:ValueFilterTest_field_boolean.java   
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));
}
项目:GitHub    文件:ValueFilterTest_field_long.java   
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));
}
项目:GitHub    文件:EnumFieldsTest7.java   
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);
}
项目:uavstack    文件:JSON.java   
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) {
    if (filter == null) {
        return;
    }

    if (filter instanceof PropertyPreFilter) {
        serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
    }

    if (filter instanceof NameFilter) {
        serializer.getNameFilters().add((NameFilter) filter);
    }

    if (filter instanceof ValueFilter) {
        serializer.getValueFilters().add((ValueFilter) filter);
    }

    if (filter instanceof PropertyFilter) {
        serializer.getPropertyFilters().add((PropertyFilter) filter);
    }

    if (filter instanceof BeforeFilter) {
        serializer.getBeforeFilters().add((BeforeFilter) filter);
    }

    if (filter instanceof AfterFilter) {
        serializer.getAfterFilters().add((AfterFilter) filter);
    }
}
项目:wenku    文件:FastJsonHttpMessageConvertersConfiguration.java   
@Bean
@ConditionalOnMissingBean({ FastJsonHttpMessageConverter4.class })
//当没有注册这个类时,自动注册Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();

    //使用最新的官方推荐配置对象的方式来注入fastjson的序列化特征
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty
     );

    //添加对json值的过滤,因为像移动APP,服务端在传json值时最好不要传null,而是使用“”,这是一个演示
    ValueFilter valueFilter = new ValueFilter() {//5
        //o 是class
        //s 是key值
        //o1 是value值
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);

    converter.setFastJsonConfig(fastJsonConfig);

    return converter;
}
项目:GitHub    文件:ValueFilterTest_field_int.java   
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;
        }

    };


    Bean a = new Bean();
    String text = JSON.toJSONString(a, filter);

    Assert.assertEquals("{\"id\":\"AAA\"}", text);
}
项目:GitHub    文件:ValueFilterTest_IntegerKey.java   
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);
}
项目:itmarry    文件:JSON.java   
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out);
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }

        serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

        if (filter != null) {
            if (filter instanceof PropertyPreFilter) {
                serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
            }

            if (filter instanceof NameFilter) {
                serializer.getNameFilters().add((NameFilter) filter);
            }

            if (filter instanceof ValueFilter) {
                serializer.getValueFilters().add((ValueFilter) filter);
            }

            if (filter instanceof PropertyFilter) {
                serializer.getPropertyFilters().add((PropertyFilter) filter);
            }

            if (filter instanceof BeforeFilter) {
                serializer.getBeforeFilters().add((BeforeFilter) filter);
            }

            if (filter instanceof AfterFilter) {
                serializer.getAfterFilters().add((AfterFilter) filter);
            }
        }

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}