Search in sources :

Example 96 with SerializeConfig

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

the class WriteClassNameTest method test_writeClassName.

public void test_writeClassName() throws Exception {
    Entity object = new Entity();
    object.setId(123);
    object.setName("jobs");
    object.setAverage(3.21F);
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(false);
    String text = JSON.toJSONString(object, config, SerializerFeature.WriteClassName);
    System.out.println(text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 97 with SerializeConfig

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

the class EnumTest2 method test_enum_noasm.

public void test_enum_noasm() throws Exception {
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    Date date = new Date(1308841916550L);
    // 1308841916550
    Assert.assertEquals("1308841916550", JSON.toJSONString(date, mapping));
    // "2011-06-23T23:11:56.550"
    Assert.assertEquals("\"2011-06-23T23:11:56.550+08:00\"", JSON.toJSONString(date, mapping, SerializerFeature.UseISO8601DateFormat));
    SerializerFeature[] features = { SerializerFeature.UseISO8601DateFormat, SerializerFeature.UseSingleQuotes };
    // '2011-06-23T23:11:56.550'
    Assert.assertEquals("'2011-06-23T23:11:56.550+08:00'", JSON.toJSONString(date, mapping, features));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) Date(java.util.Date)

Example 98 with SerializeConfig

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

the class PropertyPreFilterClassLevelTest method test_0.

public void test_0() throws Exception {
    Object[] array = { new ModelA(), new ModelB() };
    SerializeConfig config = new SerializeConfig();
    // 
    config.addFilter(// 
    ModelA.class, new SimplePropertyPreFilter("name"));
    // 
    config.addFilter(// 
    ModelB.class, new SimplePropertyPreFilter("id"));
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{},{\"id\":1002}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimplePropertyPreFilter(com.alibaba.fastjson.serializer.SimplePropertyPreFilter)

Example 99 with SerializeConfig

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

the class ValueClassLevelTest_private method test_0.

public void test_0() throws Exception {
    Object[] array = { new ModelA(), new ModelB() };
    SerializeConfig config = new SerializeConfig();
    //
    config.addFilter(//
    ModelA.class, new ValueFilter() {

        @Override
        public Object process(Object object, String name, Object value) {
            return 30001;
        }
    });
    //
    config.addFilter(//
    ModelB.class, new ValueFilter() {

        @Override
        public Object process(Object object, String name, Object value) {
            return 20001;
        }
    });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{\"id\":30001},{\"id\":20001}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter)

Example 100 with SerializeConfig

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

the class IntArrayFieldTest_primitive method test_codec_null_1.

public void test_codec_null_1() throws Exception {
    V0 v = new V0();
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    Assert.assertTrue(!mapping.isAsmEnable());
    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
    Assert.assertEquals("{\"value\":[]}", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

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