mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-28 13:50:47 +08:00
improved java.time.Instan & org.joda.time.Instant support. fix #3539
This commit is contained in:
@@ -20,11 +20,13 @@ import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.DefaultJSONParser;
|
||||
import com.alibaba.fastjson.parser.JSONLexer;
|
||||
import com.alibaba.fastjson.parser.JSONScanner;
|
||||
import com.alibaba.fastjson.parser.JSONToken;
|
||||
import com.alibaba.fastjson.serializer.*;
|
||||
import com.alibaba.fastjson.util.TypeUtils;
|
||||
|
||||
public class Jdk8DateCodec extends ContextObjectDeserializer implements ObjectSerializer, ContextObjectSerializer, ObjectDeserializer {
|
||||
|
||||
@@ -231,6 +233,23 @@ public class Jdk8DateCodec extends ContextObjectDeserializer implements ObjectSe
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException();
|
||||
} else if (lexer.token() == JSONToken.LBRACE) {
|
||||
JSONObject object = parser.parseObject();
|
||||
|
||||
if (type == Instant.class) {
|
||||
Object epochSecond = object.get("epochSecond");
|
||||
Object nano = object.get("nano");
|
||||
if (epochSecond instanceof Number && nano instanceof Number) {
|
||||
return (T) Instant.ofEpochSecond(
|
||||
TypeUtils.longExtractValue((Number) epochSecond)
|
||||
, TypeUtils.longExtractValue((Number) nano));
|
||||
}
|
||||
|
||||
if (epochSecond instanceof Number) {
|
||||
return (T) Instant.ofEpochSecond(
|
||||
TypeUtils.longExtractValue((Number) epochSecond));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.alibaba.fastjson.serializer;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.DefaultJSONParser;
|
||||
import com.alibaba.fastjson.parser.JSONLexer;
|
||||
import com.alibaba.fastjson.parser.JSONToken;
|
||||
@@ -11,6 +12,7 @@ import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
|
||||
import com.alibaba.fastjson.util.TypeUtils;
|
||||
import org.joda.time.*;
|
||||
import org.joda.time.format.*;
|
||||
|
||||
@@ -156,7 +158,7 @@ public class JodaCodec implements ObjectSerializer, ContextObjectSerializer, Obj
|
||||
return (T) new DateTime(millis, DateTimeZone.forTimeZone(timeZone));
|
||||
}
|
||||
|
||||
LocalDateTime localDateTime = new LocalDateTime(millis, DateTimeZone.forTimeZone(timeZone));
|
||||
LocalDateTime localDateTime = new LocalDateTime(millis, DateTimeZone.forTimeZone(timeZone));
|
||||
if (type == LocalDateTime.class) {
|
||||
return (T) localDateTime;
|
||||
}
|
||||
@@ -176,6 +178,23 @@ public class JodaCodec implements ObjectSerializer, ContextObjectSerializer, Obj
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException();
|
||||
} else if (lexer.token() == JSONToken.LBRACE) {
|
||||
JSONObject object = parser.parseObject();
|
||||
|
||||
if (type == Instant.class) {
|
||||
Object epochSecond = object.get("epochSecond");
|
||||
|
||||
if (epochSecond instanceof Number) {
|
||||
return (T) Instant.ofEpochSecond(
|
||||
TypeUtils.longExtractValue((Number) epochSecond));
|
||||
}
|
||||
|
||||
Object millis = object.get("millis");
|
||||
if (millis instanceof Number) {
|
||||
return (T) Instant.ofEpochMilli(
|
||||
TypeUtils.longExtractValue((Number) millis));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.alibaba.json.bvt.issue_3500;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class Issue3539 extends TestCase {
|
||||
public void test_for_issue() throws Exception {
|
||||
String str = "{\"date\":{\"nano\":140000000,\"epochSecond\":1605106869}}";
|
||||
Bean bean = JSON.parseObject(str, Bean.class);
|
||||
assertNotNull(bean.date);
|
||||
JSON.toJSONString(bean);
|
||||
|
||||
JSON.parseObject(str)
|
||||
.toJavaObject(Bean.class);
|
||||
}
|
||||
|
||||
public void test_for_issue_joda() throws Exception {
|
||||
String str = "{\"date\":{\"epochSecond\":1605106869}}";
|
||||
JodaBean bean = JSON.parseObject(str, JodaBean.class);
|
||||
assertNotNull(bean.date);
|
||||
JSON.toJSONString(bean);
|
||||
|
||||
JSON.parseObject(str)
|
||||
.toJavaObject(JodaBean.class);
|
||||
}
|
||||
|
||||
public void test_for_issue_joda2() throws Exception {
|
||||
String str = "{\"date\":{\"millis\":1605364826724}}";
|
||||
JodaBean bean = JSON.parseObject(str, JodaBean.class);
|
||||
assertNotNull(bean.date);
|
||||
JSON.toJSONString(bean);
|
||||
|
||||
JSON.parseObject(str)
|
||||
.toJavaObject(JodaBean.class);
|
||||
}
|
||||
|
||||
public static class Bean {
|
||||
public Instant date;
|
||||
}
|
||||
|
||||
public static class JodaBean {
|
||||
public org.joda.time.Instant date;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user