Search in sources :

Example 11 with JSONSerializer

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

the class TypeUtilsTest_castToJavaBean method test_bean_2.

public void test_bean_2() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 123);
    PO vo = TypeUtils.castToJavaBean(map, PO.class);
    Assert.assertEquals(123, vo.id);
    SerializeWriter out = new SerializeWriter();
    try {
        SerializeConfig config = new SerializeConfig();
        JSONSerializer serializer = new JSONSerializer(out, config);
        config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", "ID")));
        serializer.write(vo);
        Assert.assertEquals("{\"ID\":123}", out.toString());
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 12 with JSONSerializer

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

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

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

the class RefTest method test_ref.

public void test_ref() throws Exception {
    JSONSerializer ser = new JSONSerializer();
    Assert.assertFalse(ser.containsReference(null));
}
Also used : JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 15 with JSONSerializer

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

the class ColorSerializerTest method test_rgb.

public void test_rgb() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Color.class).getClass());
    VO vo = new VO();
    vo.setValue(new Color(1, 1, 1, 0));
    Assert.assertEquals("{\"value\":{\"r\":1,\"g\":1,\"b\":1}}", JSON.toJSONString(vo, SerializerFeature.WriteMapNullValue));
}
Also used : Color(java.awt.Color) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)262 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)205 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