Skip to content

Commit 5bd2ee3

Browse files
committed
support gson dateformat
1 parent aa3db15 commit 5bd2ee3

3 files changed

Lines changed: 116 additions & 1 deletion

File tree

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
import com.google.gson.annotations.SerializedName;
55
import com.jsoniter.annotation.JsonIgnore;
66
import com.jsoniter.annotation.JsonProperty;
7+
import com.jsoniter.any.Any;
8+
import com.jsoniter.output.JsonStream;
79
import com.jsoniter.spi.Config;
810
import com.jsoniter.spi.*;
911

12+
import java.io.IOException;
1013
import java.lang.annotation.Annotation;
14+
import java.lang.reflect.Type;
15+
import java.text.DateFormat;
16+
import java.text.SimpleDateFormat;
17+
import java.util.Date;
18+
import java.util.Locale;
1119

1220
public class GsonCompatibilityMode extends Config {
1321

@@ -22,6 +30,12 @@ protected Builder builder() {
2230
public static class Builder extends Config.Builder {
2331
private boolean excludeFieldsWithoutExposeAnnotation = false;
2432
private boolean serializeNulls = false;
33+
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
34+
@Override
35+
protected DateFormat initialValue() {
36+
return DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US);
37+
}
38+
};
2539

2640
public Builder excludeFieldsWithoutExposeAnnotation() {
2741
excludeFieldsWithoutExposeAnnotation = true;
@@ -33,6 +47,31 @@ public Builder serializeNulls() {
3347
return this;
3448
}
3549

50+
public Builder setDateFormat(int dateStyle) {
51+
// no op, same as gson
52+
return this;
53+
}
54+
55+
public Builder setDateFormat(final int dateStyle, final int timeStyle) {
56+
dateFormat = new ThreadLocal<DateFormat>() {
57+
@Override
58+
protected DateFormat initialValue() {
59+
return DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US);
60+
}
61+
};
62+
return this;
63+
}
64+
65+
public Builder setDateFormat(final String pattern) {
66+
dateFormat = new ThreadLocal<DateFormat>() {
67+
@Override
68+
protected DateFormat initialValue() {
69+
return new SimpleDateFormat(pattern, Locale.US);
70+
}
71+
};
72+
return this;
73+
}
74+
3675
public GsonCompatibilityMode build() {
3776
return (GsonCompatibilityMode) super.build();
3877
}
@@ -51,18 +90,34 @@ public boolean equals(Object o) {
5190
Builder builder = (Builder) o;
5291

5392
if (excludeFieldsWithoutExposeAnnotation != builder.excludeFieldsWithoutExposeAnnotation) return false;
54-
return serializeNulls == builder.serializeNulls;
93+
if (serializeNulls != builder.serializeNulls) return false;
94+
return dateFormat.get().equals(builder.dateFormat.get());
5595
}
5696

5797
@Override
5898
public int hashCode() {
5999
int result = super.hashCode();
60100
result = 31 * result + (excludeFieldsWithoutExposeAnnotation ? 1 : 0);
61101
result = 31 * result + (serializeNulls ? 1 : 0);
102+
result = 31 * result + dateFormat.get().hashCode();
62103
return result;
63104
}
64105
}
65106

107+
@Override
108+
public Encoder createEncoder(String cacheKey, Type type) {
109+
if (Date.class == type) {
110+
return new EmptyEncoder() {
111+
@Override
112+
public void encode(Object obj, JsonStream stream) throws IOException {
113+
DateFormat dateFormat = builder().dateFormat.get();
114+
stream.writeVal(dateFormat.format(obj));
115+
}
116+
};
117+
}
118+
return super.createEncoder(cacheKey, type);
119+
}
120+
66121
@Override
67122
public void updateClassDescriptor(ClassDescriptor desc) {
68123
removeGetterAndSetter(desc);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface Encoder {
99

1010
void encode(Object obj, JsonStream stream) throws IOException;
1111

12+
// TODO: remove this from encoder interface
1213
Any wrap(Object obj);
1314

1415
abstract class BooleanEncoder implements Encoder {

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import com.jsoniter.extra.GsonCompatibilityMode;
88
import junit.framework.TestCase;
99

10+
import java.text.DateFormat;
11+
import java.util.Calendar;
12+
import java.util.Date;
13+
import java.util.TimeZone;
14+
1015
public class TestGson extends TestCase {
1116

1217
public static class TestObject1 {
@@ -85,4 +90,58 @@ public void test_serializeNulls() {
8590
output = JsonStream.serialize(config, obj);
8691
assertEquals("{\"field1\":null}", output);
8792
}
93+
94+
public void test_setDateFormat_no_op() {
95+
TimeZone orig = TimeZone.getDefault();
96+
try {
97+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
98+
Gson gson = new GsonBuilder().create();
99+
String output = gson.toJson(new Date(0));
100+
assertEquals("\"Jan 1, 1970, 12:00:00 AM\"", output);
101+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
102+
.build();
103+
output = JsonStream.serialize(config, new Date(0));
104+
assertEquals("\"Jan 1, 1970, 12:00:00 AM\"", output);
105+
} finally {
106+
TimeZone.setDefault(orig);
107+
}
108+
}
109+
110+
public void test_setDateFormat_with_style() {
111+
TimeZone orig = TimeZone.getDefault();
112+
try {
113+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
114+
Gson gson = new GsonBuilder()
115+
.setDateFormat(DateFormat.LONG, DateFormat.LONG)
116+
.create();
117+
String output = gson.toJson(new Date(0));
118+
assertEquals("\"January 1, 1970 at 12:00:00 AM UTC\"", output);
119+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
120+
.setDateFormat(DateFormat.LONG, DateFormat.LONG)
121+
.build();
122+
output = JsonStream.serialize(config, new Date(0));
123+
assertEquals("\"January 1, 1970 at 12:00:00 AM UTC\"", output);
124+
} finally {
125+
TimeZone.setDefault(orig);
126+
}
127+
}
128+
129+
public void test_setDateFormat_with_format() {
130+
TimeZone orig = TimeZone.getDefault();
131+
try {
132+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
133+
Gson gson = new GsonBuilder()
134+
.setDateFormat("EEE, MMM d, yyyy hh:mm:ss a z")
135+
.create();
136+
String output = gson.toJson(new Date(0));
137+
assertEquals("\"Thu, Jan 1, 1970 12:00:00 AM UTC\"", output);
138+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
139+
.setDateFormat("EEE, MMM d, yyyy hh:mm:ss a z")
140+
.build();
141+
output = JsonStream.serialize(config, new Date(0));
142+
assertEquals("\"Thu, Jan 1, 1970 12:00:00 AM UTC\"", output);
143+
} finally {
144+
TimeZone.setDefault(orig);
145+
}
146+
}
88147
}

0 commit comments

Comments
 (0)