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

[BUG] JSON.toJSONString 行为不一致 #2545

Open
stc-W opened this issue May 8, 2024 · 1 comment
Open

[BUG] JSON.toJSONString 行为不一致 #2545

stc-W opened this issue May 8, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@stc-W
Copy link

stc-W commented May 8, 2024

问题描述

简要描述您碰到的问题。
在使用fastjson 1.2.83可以正常序列化,用2.0.49则抛出空指针异常

环境信息

请填写以下信息:

  • OS信息: [Ubuntu 22.04.3 LTS]
  • JDK信息: [openjdk 11.0.22 2024-01-16]
  • 版本信息:[Fastjson 2.0.49 兼容1.x.x]

重现步骤

如何操作可以重现该问题:

Test case

import java.util.Date;
import java.util.TimeZone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class Test961 {

    @Test
    public void testTimezoneConversionDuringDaylightSavingTime() {
        // Set the system time zone to Los Angeles (PST/PDT)
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));

        // Create a Date object within the daylight saving time period in Los Angeles
        Date dateWithinDST = new Date(1597848000000L); // Aug 19, 2020 12:00:00 AM PDT
        // Create a SerializeConfig instance
        SerializeConfig config = new SerializeConfig();

        // Serialize the Date object using the toJSONString method with UseISO8601DateFormat feature and config
        String jsonString = JSON.toJSONString(dateWithinDST, config, null, null, 0, SerializerFeature.UseISO8601DateFormat);

        // Expected outcome: The serialized output should reflect the correct timezone offset including daylight saving time (PDT)
        assertEquals("2020-08-19T07:40:00-07:00", jsonString); // PDT offset: -07:00
    }
}

期待的正确结果

能够正确序列化

相关日志输出

*com.alibaba.fastjson.JSONException: toJSONString error

at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1269)
at Test961.testTimezoneConversionDuringDaylightSavingTime(Test961.java:25)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

Caused by: java.lang.NullPointerException: Cannot read the array length because "" is null
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1251)
... 26 more*

附加信息

在fastjson 1.2.83版本可以正常序列化但是输出貌似有点奇怪:org.junit.ComparisonFailure:
Expected :2020-08-19T07:40:00-07:00
Actual :"2020-08-19T07:40:00-07:00"

@stc-W stc-W added the bug Something isn't working label May 8, 2024
@mek1986
Copy link
Contributor

mek1986 commented May 9, 2024

fastjson2可以用下面这种写法

import com.alibaba.fastjson2.JSON;

import java.util.Date;
import java.util.TimeZone;

public class Issue2545 {
    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));

        Date dateWithinDST = new Date(1597848000000L);
        String jsonString = JSON.toJSONString(dateWithinDST, "yyyy-MM-dd'T'HH:mm:ssXXX");
        System.out.println(jsonString);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants