Skip to content

Commit 9f72f02

Browse files
committed
support gson dateformat decode
1 parent c89725a commit 9f72f02

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/main/java/com/jsoniter/extra/GsonCompatibilityMode.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.annotations.Expose;
44
import com.google.gson.annotations.SerializedName;
5+
import com.jsoniter.JsonIterator;
56
import com.jsoniter.annotation.JsonIgnore;
67
import com.jsoniter.annotation.JsonProperty;
78
import com.jsoniter.any.Any;
@@ -13,6 +14,7 @@
1314
import java.lang.annotation.Annotation;
1415
import java.lang.reflect.Type;
1516
import java.text.DateFormat;
17+
import java.text.ParseException;
1618
import java.text.SimpleDateFormat;
1719
import java.util.Date;
1820
import java.util.Locale;
@@ -118,6 +120,24 @@ public void encode(Object obj, JsonStream stream) throws IOException {
118120
return super.createEncoder(cacheKey, type);
119121
}
120122

123+
@Override
124+
public Decoder createDecoder(String cacheKey, Type type) {
125+
if (Date.class == type) {
126+
return new Decoder() {
127+
@Override
128+
public Object decode(JsonIterator iter) throws IOException {
129+
DateFormat dateFormat = builder().dateFormat.get();
130+
try {
131+
return dateFormat.parse(iter.readString());
132+
} catch (ParseException e) {
133+
throw new JsonException(e);
134+
}
135+
}
136+
};
137+
}
138+
return super.createDecoder(cacheKey, type);
139+
}
140+
121141
@Override
122142
public void updateClassDescriptor(ClassDescriptor desc) {
123143
removeGetterAndSetter(desc);

src/main/java/com/jsoniter/spi/ClassDescriptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static ClassDescriptor getDecodingClassDescriptor(ClassInfo classInfo, bo
3232
desc.ctor = getCtor(clazz);
3333
desc.fields = getFields(lookup, classInfo, includingPrivate);
3434
desc.setters = getSetters(lookup, classInfo, includingPrivate);
35+
desc.getters = new ArrayList<Binding>();
3536
desc.bindingTypeWrappers = new ArrayList<WrapperDescriptor>();
3637
desc.keyValueTypeWrappers = new ArrayList<Method>();
3738
desc.unwrappers = new ArrayList<UnwrapperDescriptor>();

src/test/java/com/jsoniter/TestGson.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import com.google.gson.annotations.Expose;
66
import com.google.gson.annotations.SerializedName;
77
import com.jsoniter.extra.GsonCompatibilityMode;
8+
import com.jsoniter.output.JsonStream;
89
import junit.framework.TestCase;
910

11+
import java.util.Date;
12+
import java.util.TimeZone;
13+
1014
public class TestGson extends TestCase {
1115

1216
public static class TestObject1 {
@@ -40,4 +44,37 @@ public void test_Expose() {
4044
"{\"field1\":\"hello\"}", TestObject2.class);
4145
assertNull(obj.field1);
4246
}
47+
48+
public void test_setDateFormat_no_op() {
49+
TimeZone orig = TimeZone.getDefault();
50+
try {
51+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
52+
Gson gson = new GsonBuilder().create();
53+
Date obj = gson.fromJson("\"Jan 1, 1970, 12:00:00 AM\"", Date.class);
54+
assertEquals(0, obj.getTime());
55+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
56+
.build();
57+
obj = JsonIterator.deserialize(config, "\"Jan 1, 1970, 12:00:00 AM\"", Date.class);
58+
assertEquals(0, obj.getTime());
59+
} finally {
60+
TimeZone.setDefault(orig);
61+
}
62+
}
63+
64+
public void test_setDateFormat_format() {
65+
TimeZone orig = TimeZone.getDefault();
66+
try {
67+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
68+
Gson gson = new GsonBuilder().setDateFormat("EEE, MMM d, yyyy hh:mm:ss a z").create();
69+
Date obj = gson.fromJson("\"Thu, Jan 1, 1970 12:00:00 AM UTC\"", Date.class);
70+
assertEquals(0, obj.getTime());
71+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
72+
.setDateFormat("EEE, MMM d, yyyy hh:mm:ss a z")
73+
.build();
74+
obj = JsonIterator.deserialize(config, "\"Thu, Jan 1, 1970 12:00:00 AM UTC\"", Date.class);
75+
assertEquals(0, obj.getTime());
76+
} finally {
77+
TimeZone.setDefault(orig);
78+
}
79+
}
4380
}

0 commit comments

Comments
 (0)