Search in sources :

Example 16 with JSONSerializer

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

the class DateFormatSerializerTest method test_date_4.

public void test_date_4() throws Exception {
    SerializeWriter out = new SerializeWriter();
    SerializeConfig config = new SerializeConfig();
    JSONSerializer serializer = new JSONSerializer(out, config);
    SimpleDateFormat format = new SimpleDateFormat("yyyy");
    format.setTimeZone(JSON.defaultTimeZone);
    serializer.write(new VO(format));
    Assert.assertEquals("{\"format\":\"yyyy\"}", out.toString());
    JSON.parseObject(out.toString(), VO.class);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormat(java.text.SimpleDateFormat) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 17 with JSONSerializer

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

the class DateFormatSerializerTest method test_date_2.

public void test_date_2() throws Exception {
    SerializeWriter out = new SerializeWriter();
    SerializeConfig config = new SerializeConfig();
    JSONSerializer serializer = new JSONSerializer(out, config);
    serializer.config(SerializerFeature.WriteMapNullValue, true);
    serializer.write(new VO());
    Assert.assertEquals("{\"format\":null}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 18 with JSONSerializer

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

the class ErrorTest method test_error.

public void test_error() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(A.class, new ObjectSerializer() {

        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
            throw new IOException();
        }
    });
    JSONSerializer ser = new JSONSerializer(config);
    {
        Exception error = null;
        try {
            ser.write(new A());
        } catch (JSONException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        Exception error = null;
        try {
            B b = new B();
            b.setId(new A());
            ser.write(b);
        } catch (JSONException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : Type(java.lang.reflect.Type) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JSONException(com.alibaba.fastjson.JSONException) ObjectSerializer(com.alibaba.fastjson.serializer.ObjectSerializer) IOException(java.io.IOException) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 19 with JSONSerializer

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

the class JSONSerializerTest method test_8.

public void test_8() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.write(new B());
    Assert.assertEquals("{}", serializer.getWriter().toString());
}
Also used : JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 20 with JSONSerializer

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

the class JSONSerializerTest method test_9.

public void test_9() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    serializer.write(new D(3L));
    Assert.assertEquals("{\"id\":3}", serializer.getWriter().toString());
}
Also used : 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