Repository navigation
Backlog
Description
问题描述
在夏令时回拨期间,Europe/Paris 的 2024-10-27 02:30 可以对应两个不同的瞬间。Fastjson2 将带 +01:00 和 +02:00 的两个 ZonedDateTime 序列化为完全相同的 JSON 和 JSONB。读回 +01:00 的值时,偏移变成 +02:00,实际时间提前一小时。即使输入的 JSON 字符串明确包含 +01:00[Europe/Paris],读回结果也会变成 +02:00。
环境信息
- OS:macOS 15.0,Apple Silicon
- JDK:Oracle Java 17.0.13 和 Oracle Java 8u341
- Fastjson2:Maven Central
com.alibaba.fastjson2:fastjson2:2.0.66
重现步骤
将下面的类与 fastjson2-2.0.66.jar 放在同一目录,然后执行 javac -cp fastjson2-2.0.66.jar ZonedOverlap.java 和 java -cp fastjson2-2.0.66.jar:. ZonedOverlap。
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
public class ZonedOverlap {
public static void main(String[] args) {
ZonedDateTime later = ZonedDateTime.ofLocal(
LocalDateTime.of(2024, 10, 27, 2, 30),
ZoneId.of("Europe/Paris"), ZoneOffset.ofHours(1));
ZonedDateTime earlier = later.withEarlierOffsetAtOverlap();
String text = JSON.toJSONString(later);
System.out.println("input: " + later + " / " + later.toInstant());
System.out.println("JSON: " + text);
System.out.println("same JSON: " + text.equals(JSON.toJSONString(earlier)));
System.out.println("JSON readback: " + JSON.parseObject(text, ZonedDateTime.class));
System.out.println("same JSONB: " + Arrays.equals(JSONB.toBytes(earlier), JSONB.toBytes(later)));
System.out.println("JSONB readback: " + JSONB.parseObject(JSONB.toBytes(later), ZonedDateTime.class));
System.out.println("explicit ISO readback: " + JSON.parseObject("\"2024-10-27T02:30:00+01:00[Europe/Paris]\"", ZonedDateTime.class));
}
}期待的正确结果
序列化再读回时保留 Europe/Paris、+01:00 和 2024-10-27T01:30:00Z。解析包含明确偏移和地区时区的 ISO 文本也应保留原偏移。
相关日志输出
input: 2024-10-27T02:30+01:00[Europe/Paris] / 2024-10-27T01:30:00Z
JSON: "2024-10-27T02:30:00[Europe/Paris]"
same JSON: true
JSON readback: 2024-10-27T02:30+02:00[Europe/Paris]
same JSONB: true
JSONB readback: 2024-10-27T02:30+02:00[Europe/Paris]
explicit ISO readback: 2024-10-27T02:30+02:00[Europe/Paris]
附加信息
将 main 的 460d2e4 相对 2.0.66 改动的 4 个核心 Java 源文件编译后覆盖 2.0.66 JAR 中的同名类,在 Java 17 上得到相同结果。复现验证使用了 OpenAI Codex 辅助。
Metadata
Metadata
Assignees
Labels
No labels
Activity
dongyikuan919 commentedon Nov 29, 2026
I tested the JSONB compatibility question separately from the text parser change in #7975. Starting with
JSONB.toBytes(later), I kept theBC_TIMESTAMP_WITH_TIMEZONEmarker and timestamp fields and replaced only the last encoded string. The genericJSONB.parseand typedJSONB.parseObject(..., ZonedDateTime.class)gave the same results in these cases.02:30+01:00[Europe/Paris]Europe/Parisor+01:00[Europe/Paris]02:30+02:00[Europe/Paris]02:30+01:00[Europe/Paris]+01:0002:30+01:00with a fixed offset01:30Z[Europe/London]Europe/LondonorZ[Europe/London]01:30+01:00[Europe/London]01:30Z[Europe/London]Z01:30Zwith a fixed offsetThe composite string keeps the existing layout and was readable by 2.0.66, but those readers still use the earlier offset. An offset-only string preserves the instant at the cost of losing the named zone. This suggests updating both JSONB readers to interpret an offset-plus-region string before emitting it for ambiguous timestamps. The same probe gave identical output using the four changed core classes from main
460d2e4over the 2.0.66 JAR.中文说明
保留
BC_TIMESTAMP_WITH_TIMEZONE标记和时间字段,只替换最后一个时区字符串。2.0.66 的通用和带类型读取都能读取+01:00[Europe/Paris]与Z[Europe/London],但仍选回拨重叠区间中较早的时刻。只写偏移时可以保留时刻,但读回结果没有地区时区。主分支460d2e4相对正式版改动的四个核心类覆盖后,结果一致。后续可先让两种 JSONB 读法识别复合字符串,再考虑在存在歧义的记录上写入偏移和地区。OpenAI Codex assisted with the verification and drafting.