Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

使用SerializeFilter的JSON.toJSONString的时候会强制设置SerializerFeature.WriteDateUseDateFormat,导致Date类型转换了yyyy-MM-dd HH:mm:ss #476

Closed
myoss opened this issue Jan 3, 2016 · 4 comments
Milestone

Comments

@myoss
Copy link
Contributor

myoss commented Jan 3, 2016

serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

为什么要强制设置这个?如果我features设置了false都不会起作用,我正常的使用JSON.toJSONString(object)输出的Date类型的字段是long类型的数字,而用了下面这2个方法,Date类型都转换成了这种格式"2016-01-02 17:46:41",我看源码,发现了这个强制性的设置

方法1:
public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out);
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }

        serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

        setFilter(serializer, filters);

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}

方法2:
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out);
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }

        serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

        setFilter(serializer, filter);

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}
@myoss
Copy link
Contributor Author

myoss commented Jan 3, 2016

fastjson.version:1.2.7

@arlen43
Copy link

arlen43 commented Mar 9, 2016

不光序列化时是这样,反序列化时也是,注解format完全不起作用,因为代码里的JSONDefaultParser默认的格式化都是写死的,压根没有去拿注解里的东西。醉了。。。自己重写反序列化,只能是全局设置,不同的format完全没法搞,看了半天源码,还是解决不了,烦死

@wenshao wenshao added this to the 1.2.9 milestone Apr 11, 2016
@axeon
Copy link

axeon commented Apr 12, 2016

很早之前就提过这个bug。。。 @wenshao
#314

@wenshao
Copy link
Member

wenshao commented Apr 13, 2016

已经修正,谢谢反馈。

@wenshao wenshao closed this as completed Apr 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants