Search in sources :

Example 6 with SimpleDateFormatSerializer

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

the class DateFieldTest7 method test_0.

public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    config.setAsmEnable(false);
    Entity object = new Entity();
    object.setValue(new Date());
    String text = JSON.toJSONString(object, config);
    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)

Example 7 with SimpleDateFormatSerializer

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

the class DateFieldTest3 method test_codec_null_no_asm.

public void test_codec_null_no_asm() throws Exception {
    V0 v = new V0();
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    mapping.setAsmEnable(false);
    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    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 8 with SimpleDateFormatSerializer

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

the class Bug_for_loveflying method test_for_loveflying.

public void test_for_loveflying() throws Exception {
    User user = new User();
    user.setId(1l);
    user.setName("loveflying");
    user.setCreateTime(new java.sql.Timestamp(new Date().getTime()));
    UserLog userLog = new UserLog();
    userLog.setId(1l);
    userLog.setUser(user);
    user.getUserLogs().add(userLog);
    userLog = new UserLog();
    userLog.setId(2l);
    userLog.setUser(user);
    user.getUserLogs().add(userLog);
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    mapping.put(java.sql.Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    // mapping.put(User.class, new JavaBeanSerializer(User.class,
    // Collections.singletonMap("id", "uid")));
    JSONObject jsonObject = (JSONObject) JSON.toJSON(user);
    jsonObject.put("ext", "新加的属性");
    System.out.println(jsonObject.toJSONString(jsonObject, mapping));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) 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