Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.2.5 使用 ISO8601 序列化Calendar类型, 丢失时间数据 #331

Closed
zlphoenix opened this issue May 8, 2015 · 4 comments
Closed
Milestone

Comments

@zlphoenix
Copy link

之前在 #301 中提到过 时区的问题在1.2.5已经解决了,但是又引入了新的问题:
Calendar 类型在只包含日期的场景序列化成 json 会是 yyyy-MM-dd+时区 这样的格式,丢失了 时间部分,不符合8601标准格式了.而且在浏览器生成 Date 对象也会出错,无法识别成日期格式.

请问这个问题应该如何解决?

@wenshao
Copy link
Member

wenshao commented May 9, 2015

请提供testcase

@zlphoenix
Copy link
Author

@wenshao 测试用例:

    @Test
    public void DateWithoutTimeTest() throws Exception
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse("2015-05-23");

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        Model original = new Model();
        original.setDate(date);
        original.setCalendar(c);

        SerializeWriter out = new SerializeWriter();
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.config(SerializerFeature.UseISO8601DateFormat, true);

        serializer.write(original);
        String json = out.toString();

        System.out.println(json); //V1.2.4 输出{"calendar":"2015-05-23","date":"2015-05-23"} , V1.2.6 输出{"calendar":"2015-05-23+08:00","date":"2015-05-23+08:00"}

        Model actual = JSON.parseObject(json, Model.class);

        Assert.assertNotNull(actual);
        Assert.assertNotNull(actual.getDate());
        Assert.assertNotNull(actual.getCalendar());

        Assert.assertEquals("与序列化前比较不相等", original.getDate(), actual.getDate());

        Assert.assertEquals("序列化后的Date 和 Calendar 不相等", actual.getDate(), actual.getCalendar().getTime());
    }

其中 Model 类型只有两个属性分别是Calendar 类型的 calendar 和 Date 类型的 date
这条测试用例在1.2.4能通过,但是缺少时区,但在1.2.6上无法通过测试,异常信息
com.alibaba.fastjson.JSONException: For input string: "2015-05-23+08:00"
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:555)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:250)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:226)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:185)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:303)
at com.inspur.gsp.gsf.service.providers.FastJson_ISO8601DateTest.DateWithoutTimeTest(FastJson_ISO8601DateTest.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NumberFormatException: For input string: "2015-05-23+08:00"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at java.lang.Long.parseLong(Long.java:483)
at com.alibaba.fastjson.parser.deserializer.DateDeserializer.cast(DateDeserializer.java:56)
at com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer.deserialze(AbstractDateDeserializer.java:98)
at com.alibaba.fastjson.serializer.CalendarCodec.deserialze(CalendarCodec.java:96)
at Fastjson_ASM_Model_1.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:551)
... 31 more

@zlphoenix
Copy link
Author

@wenshao 需要我提交一个单元测试的 request 吗?

@wenshao wenshao added this to the 1.2.9 milestone Apr 13, 2016
wenshao added a commit that referenced this issue Apr 13, 2016
@wenshao wenshao closed this as completed Apr 13, 2016
wenshao added a commit that referenced this issue Apr 13, 2016
@wenshao
Copy link
Member

wenshao commented Apr 13, 2016

bug fixed. i will include in 1.2.9 & 1.1.49.android

wenshao added a commit that referenced this issue Apr 13, 2016
wenshao added a commit that referenced this issue Apr 13, 2016
wenshao added a commit that referenced this issue Jul 17, 2019
asa1997 pushed a commit to Be-Secure/fastjson that referenced this issue Feb 8, 2023
asa1997 pushed a commit to Be-Secure/fastjson that referenced this issue Feb 8, 2023
asa1997 pushed a commit to Be-Secure/fastjson that referenced this issue Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants