|
5 | 5 | import com.google.gson.annotations.Expose; |
6 | 6 | import com.google.gson.annotations.SerializedName; |
7 | 7 | import com.jsoniter.extra.GsonCompatibilityMode; |
| 8 | +import com.jsoniter.output.JsonStream; |
8 | 9 | import junit.framework.TestCase; |
9 | 10 |
|
| 11 | +import java.util.Date; |
| 12 | +import java.util.TimeZone; |
| 13 | + |
10 | 14 | public class TestGson extends TestCase { |
11 | 15 |
|
12 | 16 | public static class TestObject1 { |
@@ -40,4 +44,37 @@ public void test_Expose() { |
40 | 44 | "{\"field1\":\"hello\"}", TestObject2.class); |
41 | 45 | assertNull(obj.field1); |
42 | 46 | } |
| 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 | + } |
43 | 80 | } |
0 commit comments