Search in sources :

Example 86 with SerializeConfig

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

the class SerializeConfigTest method test_0.

public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    Method method = SerializeConfig.class.getDeclaredMethod("createJavaBeanSerializer", Class.class);
    method.setAccessible(true);
    Exception error = null;
    try {
        method.invoke(config, int.class);
    } catch (InvocationTargetException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 87 with SerializeConfig

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

the class SerializeConfigTest2 method test_1.

public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setTypeKey("%type");
    Assert.assertEquals("%type", config.getTypeKey());
    Model model = new Model();
    model.value = 1001;
    Assert.assertEquals("{\"%type\":\"com.alibaba.json.bvt.serializer.SerializeConfigTest2$Model\",\"value\":1001}", JSON.toJSONString(model, config, SerializerFeature.WriteClassName));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 88 with SerializeConfig

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

the class SerializeConfigTest2_private method test_1.

public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setTypeKey("%type");
    Assert.assertEquals("%type", config.getTypeKey());
    Model model = new Model();
    model.value = 1001;
    Assert.assertEquals("{\"%type\":\"com.alibaba.json.bvt.serializer.SerializeConfigTest2_private$Model\",\"value\":1001}", JSON.toJSONString(model, config, SerializerFeature.WriteClassName));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 89 with SerializeConfig

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

the class BeforeFilterClassLevelTest_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 BeforeFilter() {

        @Override
        public void writeBefore(Object object) {
            this.writeKeyValue("type", "A");
        }
    });
    //
    config.addFilter(//
    ModelB.class, new BeforeFilter() {

        @Override
        public void writeBefore(Object object) {
            this.writeKeyValue("type", "B");
        }
    });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{\"type\":\"A\",\"id\":1001},{\"type\":\"B\",\"id\":1002}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) BeforeFilter(com.alibaba.fastjson.serializer.BeforeFilter)

Example 90 with SerializeConfig

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

the class ClassLevelFeatureConfigTest 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));
    config.config(Model.class, SerializerFeature.BeanToArray, true);
    Assert.assertEquals("[1001]", JSON.toJSONString(model, config));
}
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