Search in sources :

Example 1 with SimpleDateFormatSerializer

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

the class DateFieldTest3 method test_codec_no_asm.

public void test_codec_no_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new Date());
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with SimpleDateFormatSerializer

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

the class DateFieldTest3 method test_codec.

public void test_codec() throws Exception {
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    V0 v = new V0();
    v.setValue(new Date());
    String text = JSON.toJSONString(v, mapping);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 3 with SimpleDateFormatSerializer

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

the class DateFieldTest3 method test_codec_asm.

public void test_codec_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new Date());
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    mapping.setAsmEnable(true);
    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with SimpleDateFormatSerializer

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

the class DateFieldTest3 method test_codec_null_asm.

public void test_codec_null_asm() throws Exception {
    V0 v = new V0();
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(true);
    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    Assert.assertEquals("{\"value\":null}", text);
    V0 v1 = JSON.parseObject(text, V0.class);
    Assert.assertEquals(v1.getValue(), v.getValue());
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)

Example 5 with SimpleDateFormatSerializer

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

the class DateFieldTest6 method test_0.

public void test_0() throws Exception {
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    Entity object = new Entity();
    object.setValue(new Date());
    String text = JSON.toJSONString(object, mapping);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":\"" + format.format(object.getValue()) + "\"}", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)8 SimpleDateFormatSerializer (com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)8 Date (java.util.Date)6 SimpleDateFormat (java.text.SimpleDateFormat)5 JSONObject (com.alibaba.fastjson.JSONObject)1