Search in sources :

Example 86 with JSONSerializer

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

Example 87 with JSONSerializer

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

the class NameFilterTest_long 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 Long);
                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 88 with JSONSerializer

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

the class NameFilterTest_long 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 89 with JSONSerializer

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

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

Example 90 with JSONSerializer

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

the class NameFilterTest_short 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")) {
                Assert.assertTrue(value instanceof Short);
                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", (short) 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

JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)263 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)206 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)54 HashMap (java.util.HashMap)48 NameFilter (com.alibaba.fastjson.serializer.NameFilter)36 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)19 ArrayList (java.util.ArrayList)12 ListSerializer (com.alibaba.fastjson.serializer.ListSerializer)11 StringWriter (java.io.StringWriter)11 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)10 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)10 Map (java.util.Map)10 Date (java.util.Date)9 LinkedHashMap (java.util.LinkedHashMap)8 JSONException (com.alibaba.fastjson.JSONException)7 CollectionCodec (com.alibaba.fastjson.serializer.CollectionCodec)6 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)4 IOException (java.io.IOException)4 Color (java.awt.Color)3