Search in sources :

Example 11 with ValueFilter

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

the class ValueFilterTest_field_long 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 12 with ValueFilter

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

the class ValueFilterTest_field_long 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 13 with ValueFilter

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

the class MTopFilterTest method test_0.

public void test_0() throws Exception {
    Model model = new Model();
    model.id = 1001;
    model.name = "yongbo";
    model.user = new Person();
    model.user.personId = 2002;
    final HashMap<String, Object> values = new HashMap<String, Object>();
    ValueFilter valueFilter = new ValueFilter() {

        @Override
        public Object process(Object object, String name, Object value) {
            values.put(name, value);
            return value;
        }
    };
    String jsonString = JSON.toJSONString(model, valueFilter);
    Assert.assertEquals("{\"id\":1001,\"name\":\"yongbo\",\"user\":{\"personId\":2002}}", jsonString);
    Assert.assertEquals(4, values.size());
    Assert.assertEquals(model.id, values.get("id"));
    Assert.assertSame(model.name, values.get("name"));
    Assert.assertEquals(model.user, values.get("user"));
    Assert.assertEquals(model.user.personId, values.get("personId"));
}
Also used : HashMap(java.util.HashMap) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 14 with ValueFilter

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

the class ValueFilterTest 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 15 with ValueFilter

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

the class FieldSerializerTest2 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) {
                if ("v".equals(name)) {
                    return "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)

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