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

日期格式设置为 "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS" 会导致反序列化报错 #3361

Closed
vdiskg opened this issue Jul 24, 2020 · 0 comments

Comments

@vdiskg
Copy link

vdiskg commented Jul 24, 2020

我看了一下DateCodec的源代码,好像只是简单的判断了一下 strVal.length() == parser.getDateFomartPattern().length(),不符合就直接跳过使用DateFormat,从而导致这个问题。

这是我的测试代码

package test.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class FastJsonTest {

    public static void main(String[] args) {
        Model model = new Model();
        model.setOldDate(new Date());
        log.info("{}", model);

        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
        config.setWriteContentLength(false);
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS";
        config.setDateFormat(JSON.DEFFAULT_DATE_FORMAT);
        String string = JSON.toJSONString(model,
                config.getSerializeConfig(),
                config.getSerializeFilters(),
                config.getDateFormat(),
                JSON.DEFAULT_GENERATE_FEATURE,
                config.getSerializerFeatures());
        log.info("{}", string);

        Model model2 = JSON.parseObject(string, Model.class);
        log.info("{}", model2);

        Model model3 = JSON.parseObject(string, new TypeReference<Model>() {
        }.getType());
        log.info("{}", model3);
    }

    @Getter
    @Setter
    @ToString
    public static class Model {

        private Date oldDate;
    }
}

这个是错误日志

2020-07-24 16:54:30.173 GMT+08:00  INFO 14700 --- [main] test.fastjson.FastJsonTest : FastJsonTest.Model(oldDate=Fri Jul 24 16:54:30 CST 2020)
2020-07-24 16:54:30.275 GMT+08:00  INFO 14700 --- [main] test.fastjson.FastJsonTest : {"oldDate":"2020-07-24T16:54:30.000000170"}
Exception in thread "main" com.alibaba.fastjson.JSONException: For input string: "2020-07-24T16:54:30.000000170"
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:693)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:395)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:299)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:572)
	at test.fastjson.FastJsonTest.main(FastJsonTest.java:42)
Caused by: java.lang.NumberFormatException: For input string: "2020-07-24T16:54:30.000000170"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Long.parseLong(Long.java:692)
	at java.base/java.lang.Long.parseLong(Long.java:817)
	at com.alibaba.fastjson.serializer.DateCodec.cast(DateCodec.java:335)
	at com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer.deserialze(AbstractDateDeserializer.java:184)
	at com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer.deserialze(AbstractDateDeserializer.java:16)
	at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1277)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:892)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1610)
	at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_Model.deserialze(Unknown Source)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:296)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:688)
	... 4 more

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

1 participant