Search in sources :

Example 81 with SerializeConfig

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

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

the class ByteArrayTest method test_bytes.

public void test_bytes() throws Exception {
    VO vo = new VO();
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty };
    String text1 = JSON.toJSONString(vo, mapping, features);
    Assert.assertEquals("{\"value\":[]}", text1);
    String text2 = JSON.toJSONString(vo, features);
    Assert.assertEquals("{\"value\":[]}", text2);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature)

Example 83 with SerializeConfig

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

the class JSONSerializerDeprecatedTest method test_.

public void test_() throws Exception {
    JSONSerializer ser = new JSONSerializer(new SerializeConfig());
    ser.setDateFormat(new ISO8601DateFormat());
    Assert.assertEquals(null, ser.getDateFormatPattern());
    ser.close();
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 84 with SerializeConfig

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

the class JSONSerializerMapTest method test_0.

public void test_0() throws Exception {
    SerializeConfig map = new SerializeConfig();
    Assert.assertFalse(0 == size(map));
    Assert.assertEquals(true, map.get(Integer.class) == IntegerCodec.instance);
    Assert.assertEquals(true, map.put(Integer.class, IntegerCodec.instance));
    Assert.assertEquals(true, map.put(Integer.class, IntegerCodec.instance));
    Assert.assertEquals(true, map.put(Integer.class, IntegerCodec.instance));
    Assert.assertEquals(true, map.get(Integer.class) == IntegerCodec.instance);
    Assert.assertFalse(0 == size(map));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 85 with SerializeConfig

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

the class SerializeConfigTest method test_1.

public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setTypeKey("%type");
    Assert.assertEquals("%type", config.getTypeKey());
    Assert.assertEquals("{\"%type\":\"java.util.LinkedHashMap\"}", JSON.toJSONString(new LinkedHashMap(), config, SerializerFeature.WriteClassName));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)191 Date (java.util.Date)16 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)13 SimpleDateFormat (java.text.SimpleDateFormat)12 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)10 SimpleDateFormatSerializer (com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)8 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)6 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)4 HashMap (java.util.HashMap)4 DoubleSerializer (com.alibaba.fastjson.serializer.DoubleSerializer)3 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)3 SimplePropertyPreFilter (com.alibaba.fastjson.serializer.SimplePropertyPreFilter)3 AfterFilter (com.alibaba.fastjson.serializer.AfterFilter)2 BeforeFilter (com.alibaba.fastjson.serializer.BeforeFilter)2 NameFilter (com.alibaba.fastjson.serializer.NameFilter)2 PascalNameFilter (com.alibaba.fastjson.serializer.PascalNameFilter)2 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)2 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)2 BigDecimal (java.math.BigDecimal)2 DecimalFormat (java.text.DecimalFormat)2