Search in sources :

Example 1 with NameFilter

use of com.alibaba.fastjson.serializer.NameFilter 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)

Example 2 with NameFilter

use of com.alibaba.fastjson.serializer.NameFilter 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 3 with NameFilter

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

the class NameFilterTest_int method test_namefilter_1.

public void test_namefilter_1() throws Exception {
    NameFilter filter = new NameFilter() {

        public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "ID";
            }
            return name;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 0);
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{\"ID\":0}", text);
}
Also used : NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 4 with NameFilter

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

the class NameFilterTest_int method test_namefilter.

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

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

Example 5 with NameFilter

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

the class NameFilterTest_int_field method test_namefilter_1.

public void test_namefilter_1() throws Exception {
    NameFilter filter = new NameFilter() {

        public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
                return "ID";
            }
            return name;
        }
    };
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 0);
    serializer.write(map);
    String text = out.toString();
    Assert.assertEquals("{\"ID\":0}", text);
}
Also used : NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

NameFilter (com.alibaba.fastjson.serializer.NameFilter)41 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)36 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)36 HashMap (java.util.HashMap)17 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)3 JSONException (com.alibaba.fastjson.JSONException)2 PascalNameFilter (com.alibaba.fastjson.serializer.PascalNameFilter)2 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)2 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 Map (java.util.Map)1