Skip to content

Commit

Permalink
fix serialize OffsetDateTime, for issue #1621
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 8, 2023
1 parent e981962 commit 6c98e50
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,7 @@ public final void writeOffsetDateTime(OffsetDateTime dateTime) {
chars[off++] = 'Z';
} else {
zoneId.getChars(0, zoneIdLength, chars, off);
off += zoneIdLength;
}
chars[off] = quote;
this.off = off + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,7 @@ public final void writeOffsetDateTime(OffsetDateTime dateTime) {
bytes[off++] = 'Z';
} else {
zoneId.getBytes(0, zoneIdLength, bytes, off);
off += zoneIdLength;
}
bytes[off] = (byte) quote;
this.off = off + 1;
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,9 @@ public static int writeLocalDate(byte[] bytes, int off, int year, int month, int
bytes[off + 3] = (byte) v;
off += 4;
} else {
if (year > 9999) {
bytes[off++] = '+';
}
off = IOUtils.writeInt32(bytes, off, year);
}

Expand Down Expand Up @@ -747,6 +750,9 @@ public static int writeLocalDate(char[] chars, int off, int year, int month, int
chars[off + 3] = (char) (byte) v;
off += 4;
} else {
if (year > 9999) {
chars[off++] = '+';
}
off = IOUtils.writeInt32(chars, off, year);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,46 @@ public void test_s9() {
OffsetDateTime odt2 = JSON.parseObject(str.toCharArray(), OffsetDateTime.class);
assertEquals(odt, odt2);
}

@Test
public void test_min() {
OffsetDateTime odt = OffsetDateTime.MIN;
{
String str = JSON.toJSONString(odt);
OffsetDateTime odt1 = JSON.parseObject(str, OffsetDateTime.class);
assertEquals(odt, odt1);

OffsetDateTime odt2 = JSON.parseObject(str.toCharArray(), OffsetDateTime.class);
assertEquals(odt, odt2);

OffsetDateTime odt3 = JSON.parseObject(str.getBytes(), OffsetDateTime.class);
assertEquals(odt, odt3);
}
{
byte[] strBytes = JSON.toJSONBytes(odt);
OffsetDateTime odt1 = JSON.parseObject(strBytes, OffsetDateTime.class);
assertEquals(odt, odt1);
}
}

@Test
public void test_max() {
OffsetDateTime odt = OffsetDateTime.MAX;
{
String str = JSON.toJSONString(odt);
OffsetDateTime odt1 = JSON.parseObject(str, OffsetDateTime.class);
assertEquals(odt, odt1);

OffsetDateTime odt2 = JSON.parseObject(str.toCharArray(), OffsetDateTime.class);
assertEquals(odt, odt2);

OffsetDateTime odt3 = JSON.parseObject(str.getBytes(), OffsetDateTime.class);
assertEquals(odt, odt3);
}
{
byte[] strBytes = JSON.toJSONBytes(odt);
OffsetDateTime odt1 = JSON.parseObject(strBytes, OffsetDateTime.class);
assertEquals(odt, odt1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.alibaba.fastjson2.issues_1600;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1621 {
@Test
public void test() {
final OffsetDateTime defaultDateTime = OffsetDateTime.MIN;
DemoMetadata demoMetadata = DemoMetadata.builder()
.metadataType("TestType")
.lastMetadataRefreshTime(defaultDateTime)
.build();
Map<String, DemoMetadata> map = new HashMap<>();
map.put(demoMetadata.getMetadataType(), demoMetadata);
DemoRoot demoRoot = DemoRoot.builder()
.rootName("Root")
.lastMetadataRefreshTime(defaultDateTime)
.dmoToDaoTypeMapping(map)
.build();
JSONObject jsonObject = (JSONObject) JSON.toJSON(demoRoot);
DemoRoot updatedRoot = jsonObject.toJavaObject(DemoRoot.class);
assertEquals(defaultDateTime, updatedRoot.getLastMetadataRefreshTime());
}

@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
public static class DemoRoot
extends DemoAbstract {
private static final long serialVersionUID = 1L;
private String rootName;
private Map<String, DemoMetadata> dmoToDaoTypeMapping;

@Override
public void someFunction() {
}
}

@Data
@SuperBuilder
@Generated
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@ToString
public static class DemoMetadata
extends DemoAbstract {
private static final long serialVersionUID = 1L;
private String metadataType;

@Override
public void someFunction() {
}
}

@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public abstract static class DemoAbstract
implements Serializable {
private static final long serialVersionUID = 1L;
@EqualsAndHashCode.Exclude
private OffsetDateTime lastMetadataRefreshTime;

public abstract void someFunction();
}
}

0 comments on commit 6c98e50

Please sign in to comment.