[BUG] JSON.toJSONString 行为不一致
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
*简要描述您碰到的问题。*
在使用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
```java
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"*
Contributor guide
Research direction
Start at com.alibaba.fastjson.JSON.toJSONString in JSON.java around lines 1251-1269 and reproduce the supplied Test961 JUnit case under Java 11 with Fastjson 2.0.49. Confirm the null-pointer failure and the intended serialization behavior, including whether the quoted output is expected; done means the test completes without the reported exception and the output expectation is consistent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100