Skip to content

Commit

Permalink
deserialize support ZoneOffset, for issue #1789
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 26, 2023
1 parent 64409f0 commit 54656fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,10 @@ public ObjectReader getObjectReader(ObjectReaderProvider provider, Type type) {
return ObjectReaderImplOffsetTime.INSTANCE;
}

if (type == ZoneOffset.class) {
return new ObjectReaderImplFromString<>(ZoneOffset.class, ZoneOffset::of);
}

if (type == Instant.class) {
return ObjectReaderImplInstant.INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.fastjson2.issues_1700;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.time.*;
import java.util.Date;

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

public class Issue1789 {
@Test
public void test() {
Person person = new Person();
person.setZoneOffset(ZoneOffset.ofHours(8));
String str = JSON.toJSONString(person); // {"zoneOffset":"+08:00"}
Person person1 = JSON.parseObject(str, Person.class);
assertNotNull(person1);
assertEquals(person.zoneOffset, person1.zoneOffset);
}

@Data
public static class Person {
private String name;
private Integer age;
private Object object;
private Date date;
private Duration duration;
private LocalTime localTime;
private LocalDate localDate;
private LocalDateTime localDateTime;
private ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
}
}

0 comments on commit 54656fc

Please sign in to comment.