Search in sources :

Example 91 with SerializeConfig

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

the class ClassLevelFeatureConfigTest2 method test_0.

public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.config(Model.class, SerializerFeature.BeanToArray, true);
    Model model = new Model();
    model.id = 1001;
    Assert.assertEquals("[1001]", JSON.toJSONString(model, config));
    config.config(Model.class, SerializerFeature.BeanToArray, false);
    Assert.assertEquals("{\"id\":1001}", JSON.toJSONString(model, config));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 92 with SerializeConfig

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

the class ClassLevelFeatureConfigTest3 method test_0.

public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.config(Model.class, SerializerFeature.BeanToArray, false);
    Model model = new Model();
    model.id = 1001;
    Assert.assertEquals("{\"id\":1001}", JSON.toJSONString(model, config));
    config.config(Model.class, SerializerFeature.BeanToArray, true);
    Assert.assertEquals("[1001]", JSON.toJSONString(model, config));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 93 with SerializeConfig

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

the class ClassLevelFeatureConfigTest_private method test_0.

public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    Model model = new Model();
    model.id = 1001;
    Assert.assertEquals("{\"id\":1001}", JSON.toJSONString(model, config));
    config.config(Model.class, SerializerFeature.BeanToArray, true);
    Assert.assertEquals("[1001]", JSON.toJSONString(model, config));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 94 with SerializeConfig

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

the class PropertyFilterClassLevelTest method test_0.

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

        @Override
        public boolean apply(Object object, String name, Object value) {
            return false;
        }
    });
    //
    config.addFilter(//
    ModelB.class, new PropertyFilter() {

        @Override
        public boolean apply(Object object, String name, Object value) {
            return true;
        }
    });
    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) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter)

Example 95 with SerializeConfig

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

the class NameFilterClassLevelTest_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 PascalNameFilter());
    //
    config.addFilter(//
    ModelB.class, new NameFilter() {

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

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