Skip to content

Commit 6c98e50

Browse files
committed
fix serialize OffsetDateTime, for issue #1621
1 parent e981962 commit 6c98e50

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,7 @@ public final void writeOffsetDateTime(OffsetDateTime dateTime) {
23002300
chars[off++] = 'Z';
23012301
} else {
23022302
zoneId.getChars(0, zoneIdLength, chars, off);
2303+
off += zoneIdLength;
23032304
}
23042305
chars[off] = quote;
23052306
this.off = off + 1;

core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,7 @@ public final void writeOffsetDateTime(OffsetDateTime dateTime) {
21932193
bytes[off++] = 'Z';
21942194
} else {
21952195
zoneId.getBytes(0, zoneIdLength, bytes, off);
2196+
off += zoneIdLength;
21962197
}
21972198
bytes[off] = (byte) quote;
21982199
this.off = off + 1;

core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,9 @@ public static int writeLocalDate(byte[] bytes, int off, int year, int month, int
707707
bytes[off + 3] = (byte) v;
708708
off += 4;
709709
} else {
710+
if (year > 9999) {
711+
bytes[off++] = '+';
712+
}
710713
off = IOUtils.writeInt32(bytes, off, year);
711714
}
712715

@@ -747,6 +750,9 @@ public static int writeLocalDate(char[] chars, int off, int year, int month, int
747750
chars[off + 3] = (char) (byte) v;
748751
off += 4;
749752
} else {
753+
if (year > 9999) {
754+
chars[off++] = '+';
755+
}
750756
off = IOUtils.writeInt32(chars, off, year);
751757
}
752758

core/src/test/java/com/alibaba/fastjson2/date/OffsetDateTimeTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,46 @@ public void test_s9() {
155155
OffsetDateTime odt2 = JSON.parseObject(str.toCharArray(), OffsetDateTime.class);
156156
assertEquals(odt, odt2);
157157
}
158+
159+
@Test
160+
public void test_min() {
161+
OffsetDateTime odt = OffsetDateTime.MIN;
162+
{
163+
String str = JSON.toJSONString(odt);
164+
OffsetDateTime odt1 = JSON.parseObject(str, OffsetDateTime.class);
165+
assertEquals(odt, odt1);
166+
167+
OffsetDateTime odt2 = JSON.parseObject(str.toCharArray(), OffsetDateTime.class);
168+
assertEquals(odt, odt2);
169+
170+
OffsetDateTime odt3 = JSON.parseObject(str.getBytes(), OffsetDateTime.class);
171+
assertEquals(odt, odt3);
172+
}
173+
{
174+
byte[] strBytes = JSON.toJSONBytes(odt);
175+
OffsetDateTime odt1 = JSON.parseObject(strBytes, OffsetDateTime.class);
176+
assertEquals(odt, odt1);
177+
}
178+
}
179+
180+
@Test
181+
public void test_max() {
182+
OffsetDateTime odt = OffsetDateTime.MAX;
183+
{
184+
String str = JSON.toJSONString(odt);
185+
OffsetDateTime odt1 = JSON.parseObject(str, OffsetDateTime.class);
186+
assertEquals(odt, odt1);
187+
188+
OffsetDateTime odt2 = JSON.parseObject(str.toCharArray(), OffsetDateTime.class);
189+
assertEquals(odt, odt2);
190+
191+
OffsetDateTime odt3 = JSON.parseObject(str.getBytes(), OffsetDateTime.class);
192+
assertEquals(odt, odt3);
193+
}
194+
{
195+
byte[] strBytes = JSON.toJSONBytes(odt);
196+
OffsetDateTime odt1 = JSON.parseObject(strBytes, OffsetDateTime.class);
197+
assertEquals(odt, odt1);
198+
}
199+
}
158200
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.alibaba.fastjson2.issues_1600;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONObject;
5+
import lombok.*;
6+
import lombok.experimental.SuperBuilder;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.Serializable;
10+
import java.time.OffsetDateTime;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
public class Issue1621 {
17+
@Test
18+
public void test() {
19+
final OffsetDateTime defaultDateTime = OffsetDateTime.MIN;
20+
DemoMetadata demoMetadata = DemoMetadata.builder()
21+
.metadataType("TestType")
22+
.lastMetadataRefreshTime(defaultDateTime)
23+
.build();
24+
Map<String, DemoMetadata> map = new HashMap<>();
25+
map.put(demoMetadata.getMetadataType(), demoMetadata);
26+
DemoRoot demoRoot = DemoRoot.builder()
27+
.rootName("Root")
28+
.lastMetadataRefreshTime(defaultDateTime)
29+
.dmoToDaoTypeMapping(map)
30+
.build();
31+
JSONObject jsonObject = (JSONObject) JSON.toJSON(demoRoot);
32+
DemoRoot updatedRoot = jsonObject.toJavaObject(DemoRoot.class);
33+
assertEquals(defaultDateTime, updatedRoot.getLastMetadataRefreshTime());
34+
}
35+
36+
@Data
37+
@SuperBuilder
38+
@EqualsAndHashCode(callSuper = true)
39+
@NoArgsConstructor
40+
@AllArgsConstructor
41+
@ToString
42+
public static class DemoRoot
43+
extends DemoAbstract {
44+
private static final long serialVersionUID = 1L;
45+
private String rootName;
46+
private Map<String, DemoMetadata> dmoToDaoTypeMapping;
47+
48+
@Override
49+
public void someFunction() {
50+
}
51+
}
52+
53+
@Data
54+
@SuperBuilder
55+
@Generated
56+
@EqualsAndHashCode(callSuper = true)
57+
@NoArgsConstructor
58+
@ToString
59+
public static class DemoMetadata
60+
extends DemoAbstract {
61+
private static final long serialVersionUID = 1L;
62+
private String metadataType;
63+
64+
@Override
65+
public void someFunction() {
66+
}
67+
}
68+
69+
@SuperBuilder
70+
@Data
71+
@NoArgsConstructor
72+
@AllArgsConstructor
73+
@ToString
74+
public abstract static class DemoAbstract
75+
implements Serializable {
76+
private static final long serialVersionUID = 1L;
77+
@EqualsAndHashCode.Exclude
78+
private OffsetDateTime lastMetadataRefreshTime;
79+
80+
public abstract void someFunction();
81+
}
82+
}

0 commit comments

Comments
 (0)