Skip to content

Commit df46051

Browse files
committed
LocalDateTime & OffsetDateTime & ZonedDateTime support JSONField#locale, for issue#2328
1 parent a0fe523 commit df46051

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

core/src/main/java/com/alibaba/fastjson2/writer/FieldWriterObjectFunc.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.lang.reflect.Field;
44
import java.lang.reflect.Method;
55
import java.lang.reflect.Type;
6+
import java.util.Locale;
67
import java.util.concurrent.atomic.AtomicIntegerArray;
78
import java.util.concurrent.atomic.AtomicLongArray;
89
import java.util.concurrent.atomic.AtomicReferenceArray;
@@ -18,14 +19,15 @@ final class FieldWriterObjectFunc<T>
1819
int ordinal,
1920
long features,
2021
String format,
22+
Locale locale,
2123
String label,
2224
Type fieldType,
2325
Class fieldClass,
2426
Field field,
2527
Method method,
2628
Function function
2729
) {
28-
super(name, ordinal, features, format, null, label, fieldType, fieldClass, field, method);
30+
super(name, ordinal, features, format, locale, label, fieldType, fieldClass, field, method);
2931
this.function = function;
3032
isArray = fieldClass == AtomicIntegerArray.class
3133
|| fieldClass == AtomicLongArray.class

core/src/main/java/com/alibaba/fastjson2/writer/FieldWriterObjectMethod.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.lang.reflect.InvocationTargetException;
77
import java.lang.reflect.Method;
88
import java.lang.reflect.Type;
9+
import java.util.Locale;
910

1011
class FieldWriterObjectMethod<T>
1112
extends FieldWriterObject<T> {
@@ -14,13 +15,14 @@ protected FieldWriterObjectMethod(
1415
int ordinal,
1516
long features,
1617
String format,
18+
Locale locale,
1719
String label,
1820
Type fieldType,
1921
Class fieldClass,
2022
Field field,
2123
Method method
2224
) {
23-
super(name, ordinal, features, format, null, label, fieldType, fieldClass, field, method);
25+
super(name, ordinal, features, format, locale, label, fieldType, fieldClass, field, method);
2426
}
2527

2628
@Override

core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java

+62-4
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,31 @@ public <T> FieldWriter<T> createFieldWriter(
876876
String label,
877877
Method method,
878878
ObjectWriter initObjectWriter
879+
) {
880+
return createFieldWriter(
881+
provider,
882+
objectType,
883+
fieldName,
884+
ordinal,
885+
features,
886+
format,
887+
null,
888+
label,
889+
method, initObjectWriter
890+
);
891+
}
892+
893+
public <T> FieldWriter<T> createFieldWriter(
894+
ObjectWriterProvider provider,
895+
Class<T> objectType,
896+
String fieldName,
897+
int ordinal,
898+
long features,
899+
String format,
900+
Locale locale,
901+
String label,
902+
Method method,
903+
ObjectWriter initObjectWriter
879904
) {
880905
method.setAccessible(true);
881906
Class<?> fieldClass = method.getReturnType();
@@ -886,7 +911,7 @@ public <T> FieldWriter<T> createFieldWriter(
886911
}
887912

888913
if (initObjectWriter != null) {
889-
FieldWriterObjectMethod objMethod = new FieldWriterObjectMethod(fieldName, ordinal, features, format, label, fieldType, fieldClass, null, method);
914+
FieldWriterObjectMethod objMethod = new FieldWriterObjectMethod(fieldName, ordinal, features, format, locale, label, fieldType, fieldClass, null, method);
890915
objMethod.initValueClass = fieldClass;
891916
if (initObjectWriter != ObjectWriterBaseModule.VoidObjectWriter.INSTANCE) {
892917
objMethod.initObjectWriter = initObjectWriter;
@@ -980,7 +1005,7 @@ public <T> FieldWriter<T> createFieldWriter(
9801005
return new FieldWriterObjectArrayMethod(fieldName, fieldClass.getComponentType(), ordinal, features, format, label, fieldType, fieldClass, field, method);
9811006
}
9821007

983-
return new FieldWriterObjectMethod(fieldName, ordinal, features, format, label, fieldType, fieldClass, null, method);
1008+
return new FieldWriterObjectMethod(fieldName, ordinal, features, format, locale, label, fieldType, fieldClass, null, method);
9841009
}
9851010

9861011
public <T> FieldWriter createFieldWriter(String fieldName, ToLongFunction<T> function) {
@@ -1065,7 +1090,39 @@ public <T, V> FieldWriter<T> createFieldWriter(
10651090
Method method,
10661091
Function<T, V> function
10671092
) {
1068-
return createFieldWriter(provider, objectClass, fieldName, ordinal, features, format, label, fieldType, fieldClass, null, method, function);
1093+
return createFieldWriter(
1094+
provider, objectClass, fieldName, ordinal, features, format, null, label, fieldType, fieldClass, null, method, function);
1095+
}
1096+
1097+
public <T, V> FieldWriter<T> createFieldWriter(
1098+
ObjectWriterProvider provider,
1099+
Class<T> objectClass,
1100+
String fieldName,
1101+
int ordinal,
1102+
long features,
1103+
String format,
1104+
String label,
1105+
Type fieldType,
1106+
Class<V> fieldClass,
1107+
Field field,
1108+
Method method,
1109+
Function<T, V> function
1110+
) {
1111+
return createFieldWriter(
1112+
provider,
1113+
objectClass,
1114+
fieldName,
1115+
ordinal,
1116+
features,
1117+
format,
1118+
null,
1119+
label,
1120+
fieldType,
1121+
fieldClass,
1122+
field,
1123+
method,
1124+
function
1125+
);
10691126
}
10701127

10711128
public <T, V> FieldWriter<T> createFieldWriter(
@@ -1075,6 +1132,7 @@ public <T, V> FieldWriter<T> createFieldWriter(
10751132
int ordinal,
10761133
long features,
10771134
String format,
1135+
Locale locale,
10781136
String label,
10791137
Type fieldType,
10801138
Class<V> fieldClass,
@@ -1173,7 +1231,7 @@ public <T, V> FieldWriter<T> createFieldWriter(
11731231
return new FieldWriterObjectFuncFinal(fieldName, ordinal, features, format, label, fieldType, fieldClass, field, method, function);
11741232
}
11751233

1176-
return new FieldWriterObjectFunc(fieldName, ordinal, features, format, label, fieldType, fieldClass, field, method, function);
1234+
return new FieldWriterObjectFunc(fieldName, ordinal, features, format, locale, label, fieldType, fieldClass, field, method, function);
11771235
}
11781236

11791237
static class LambdaInfo {

core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ boolean record = BeanUtils.isRecord(objectClass);
331331
fieldInfo.ordinal,
332332
fieldInfo.features,
333333
fieldInfo.format,
334+
fieldInfo.locale,
334335
fieldInfo.label,
335336
method,
336337
writeUsingWriter

core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2328.java

+26
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,30 @@ public static class Bean3 {
8181
@JSONField(format = "E MMM dd yyyy", locale = "zh_CN")
8282
public LocalDate createdAt;
8383
}
84+
85+
@Test
86+
public void test4() throws Exception {
87+
LocalDateTime ldt = LocalDateTime.of(2018, 2, 3, 12, 13, 14);
88+
Bean4 bean = new Bean4();
89+
bean.createdAt = ZonedDateTime.of(ldt, ZoneId.of("Asia/Shanghai"));
90+
String str = JSON.toJSONString(bean);
91+
assertTrue(str.equals("{\"createdAt\":\"星期六 二月 03 12:13:14 +0800 2018\"}")
92+
|| str.equals("{\"createdAt\":\"周六 2月 03 12:13:14 +0800 2018\"}"));
93+
94+
Bean4 bean1 = JSON.parseObject(str, Bean4.class);
95+
assertEquals(bean.createdAt, bean1.createdAt);
96+
}
97+
98+
public static class Bean4 {
99+
@JSONField(format = "E MMM dd HH:mm:ss Z yyyy", locale = "zh_CN")
100+
private ZonedDateTime createdAt;
101+
102+
public ZonedDateTime getCreatedAt() {
103+
return createdAt;
104+
}
105+
106+
public void setCreatedAt(ZonedDateTime createdAt) {
107+
this.createdAt = createdAt;
108+
}
109+
}
84110
}

0 commit comments

Comments
 (0)