33import java .time .Instant ;
44import java .time .ZoneId ;
55import java .time .ZonedDateTime ;
6+ import java .util .HashMap ;
67
78import com .fasterxml .jackson .annotation .JsonFormat ;
89import com .fasterxml .jackson .databind .ObjectMapper ;
10+ import com .fasterxml .jackson .databind .SerializationFeature ;
11+ import com .fasterxml .jackson .databind .json .JsonMapper ;
12+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
13+ import com .fasterxml .jackson .datatype .jsr310 .MockObjectConfiguration ;
914import com .fasterxml .jackson .datatype .jsr310 .ModuleTestBase ;
1015
1116import org .junit .Assert ;
1217import org .junit .Test ;
1318
19+ import static org .junit .Assert .assertEquals ;
20+
1421public class WriteZoneIdTest extends ModuleTestBase
1522{
1623 static class DummyClassWithDate {
@@ -26,18 +33,47 @@ public DummyClassWithDate(ZonedDateTime date) {
2633 }
2734 }
2835
36+ private static ObjectMapper MAPPER = newMapper ();
37+
38+ @ Test
39+ public void testSerialization01 () throws Exception
40+ {
41+ ZoneId id = ZoneId .of ("America/Chicago" );
42+ String value = MAPPER .writeValueAsString (id );
43+ assertEquals ("The value is not correct." , "\" America/Chicago\" " , value );
44+ }
45+
46+ @ Test
47+ public void testSerialization02 () throws Exception
48+ {
49+ ZoneId id = ZoneId .of ("America/Anchorage" );
50+ String value = MAPPER .writeValueAsString (id );
51+ assertEquals ("The value is not correct." , "\" America/Anchorage\" " , value );
52+ }
53+
54+ @ Test
55+ public void testSerializationWithTypeInfo01 () throws Exception
56+ {
57+ ZoneId id = ZoneId .of ("America/Denver" );
58+ ObjectMapper mapper = mapperBuilder ()
59+ .addMixIn (ZoneId .class , MockObjectConfiguration .class )
60+ .addModule (new JavaTimeModule ())
61+ .build ();
62+ String value = mapper .writeValueAsString (id );
63+ assertEquals ("The value is not correct." , "[\" java.time.ZoneRegion\" ,\" America/Denver\" ]" , value );
64+ }
65+
2966 @ Test
3067 public void testJacksonAnnotatedPOJOWithDateWithTimezoneToJson () throws Exception
3168 {
32- ObjectMapper mapper = newMapper ();
3369 String ZONE_ID_STR = "Asia/Krasnoyarsk" ;
3470 final ZoneId ZONE_ID = ZoneId .of (ZONE_ID_STR );
3571
3672 DummyClassWithDate input = new DummyClassWithDate (ZonedDateTime .ofInstant (Instant .ofEpochSecond (0L ), ZONE_ID ));
3773
3874 // 30-Jun-2016, tatu: Exact time seems to vary a bit based on DST, so let's actually
3975 // just verify appending of timezone id itself:
40- String json = mapper .writeValueAsString (input );
76+ String json = MAPPER .writeValueAsString (input );
4177 if (!json .contains ("\" 1970-01-01T" )) {
4278 Assert .fail ("Should contain time prefix, did not: " +json );
4379 }
@@ -46,4 +82,16 @@ public void testJacksonAnnotatedPOJOWithDateWithTimezoneToJson() throws Exceptio
4682 Assert .fail ("Should contain zone id " +match +", does not: " +json );
4783 }
4884 }
85+
86+ @ Test
87+ public void testMapSerialization () throws Exception {
88+ final ZonedDateTime datetime = ZonedDateTime .parse ("2007-12-03T10:15:30+01:00[Europe/Warsaw]" );
89+ final HashMap <ZonedDateTime , String > map = new HashMap <>();
90+ map .put (datetime , "" );
91+
92+ String json = MAPPER .writer ()
93+ .with (SerializationFeature .WRITE_DATES_WITH_ZONE_ID )
94+ .writeValueAsString (map );
95+ Assert .assertEquals ("{\" 2007-12-03T10:15:30+01:00[Europe/Warsaw]\" :\" \" }" , json );
96+ }
4997}
0 commit comments