Skip to content

Commit

Permalink
fix serialize & deserialize org.joda.time.DateTime with jsonb
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 11, 2024
1 parent fd68be8 commit 760cd52
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public Class getObjectClass() {

@Override
public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
return jsonReader.readZonedDateTime();
ZonedDateTime zdt = jsonReader.readZonedDateTime();
if (builder != null && zdt != null) {
return builder.apply(zdt);
}
return zdt;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public ObjectWriterImplZonedDateTime(String format, Locale locale, Function func

@Override
public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
jsonWriter.writeZonedDateTime((ZonedDateTime) object);
ZonedDateTime zdt;
if (function != null) {
zdt = (ZonedDateTime) function.apply(object);
} else {
zdt = (ZonedDateTime) object;
}

jsonWriter.writeZonedDateTime(zdt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.fastjson2.issues_2500;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

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

public class Issue2563 {
DateTime nowD = new DateTime(2018, 7, 14, 12, 13, 14, 0);

@Test
public void mutatedTest0() {
Bean0 entity = new Bean0();
entity.setNow(nowD);

String json = JSON.toJSONString(entity);
Bean0 bean0 = JSON.parseObject(json, Bean0.class);
assertEquals(entity.now, bean0.now);
}

@Test
public void mutatedTest1() {
Bean0 entity = new Bean0();
entity.setNow(nowD);

byte[] jsonbBytes = JSONB.toBytes(entity);
Bean0 bean0 = JSONB.parseObject(jsonbBytes, Bean0.class);
assertEquals(entity.now, bean0.now);
}

@Data
public static class Bean0 {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private DateTime now;
}
}

0 comments on commit 760cd52

Please sign in to comment.