Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Jan 1, 2024
1 parent b188a58 commit 4552e27
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import ru.olegcherednik.json.jackson.datetime.serializers.key.JacksonLocalTimeKeySerializer;
import ru.olegcherednik.json.jackson.datetime.serializers.key.JacksonOffsetDateTimeKeySerializer;
import ru.olegcherednik.json.jackson.datetime.serializers.key.JacksonOffsetTimeKeySerializer;
import ru.olegcherednik.json.jackson.datetime.serializers.key.JacksonZonedDateTimeKeySerializer;

import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -92,6 +93,7 @@ private void addKeySerializers(SetupContext context) {
ser.addSerializer(LocalDateTime.class, new JacksonLocalDateTimeKeySerializer(localDateTime));
ser.addSerializer(OffsetTime.class, new JacksonOffsetTimeKeySerializer(offsetTime));
ser.addSerializer(OffsetDateTime.class, new JacksonOffsetDateTimeKeySerializer(offsetDateTime));
ser.addSerializer(ZonedDateTime.class, new JacksonZonedDateTimeKeySerializer(zonedDateTime));
context.addKeySerializers(ser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ protected JacksonZonedDateTimeSerializer withFeatures(Boolean writeZoneId, Boole
@Override
public void serialize(ZonedDateTime value, JsonGenerator generator, SerializerProvider provider)
throws IOException {
if (_formatter == null || useTimestamp(provider))
super.serialize(value, generator, provider);
else if (_formatter.getZone() == null) {
ZoneId zoneId = zoneModifier.apply(DateTimeFormatter.ISO_OFFSET_DATE_TIME.getZone());
generator.writeString(_formatter.withZone(zoneId).format(value));
} else
generator.writeString(_formatter.format(value));
super.serialize(value, generator, provider);
// if (_formatter == null || useTimestamp(provider))
// super.serialize(value, generator, provider);
// else if (_formatter.getZone() == null) {
// ZoneId zoneId = zoneModifier.apply(DateTimeFormatter.ISO_OFFSET_DATE_TIME.getZone());
// generator.writeString(_formatter.withZone(zoneId).format(value));
// } else
// generator.writeString(_formatter.format(value));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package ru.olegcherednik.json.jackson.datetime.serializers.key;

import ru.olegcherednik.json.api.ZoneModifier;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author Oleg Cherednik
* @since 01.01.2024
*/
public class JacksonZonedDateTimeKeySerializer extends JacksonKeySerializer<ZonedDateTime> {

private static final long serialVersionUID = -1513348976637073488L;

public JacksonZonedDateTimeKeySerializer(DateTimeFormatter df) {
super(ZonedDateTime.class, df, ZoneModifier.USE_ORIGINAL,
dt -> dt.toInstant().toEpochMilli(), ZonedDateTime::toEpochSecond, ZonedDateTime::getNano);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@
@Test
public class ZonedDateTimePrettyPrintTest {

public void shouldRetrievePrettyPrintJsonUtcZoneWhenWriteZonedDateTimeMapWithPrettyPrint() throws IOException {
JsonSettings settings = JsonSettings.builder()
.zoneModifier(ZoneModifier.CONVERT_TO_UTC)
.build();
// public void shouldRetrievePrettyPrintJsonUtcZoneWhenWriteZonedDateTimeMapWithPrettyPrint() throws IOException {
// JsonSettings settings = JsonSettings.builder()
// .zoneModifier(ZoneModifier.CONVERT_TO_UTC)
// .build();
//
// Map<String, ZonedDateTime> map = ZonedDateTimeTest.createData();
// String actual = Json.createPrettyPrint(settings).writeValue(map);
// String expected = ResourceData.getResourceAsString("/datetime/zoned_date_time_utc.json").trim();
//
// assertThat(actual).isNotEqualTo(expected);
// assertThat(Json.readMap(actual)).isEqualTo(Json.readMap(expected));
// }

Map<String, ZonedDateTime> map = ZonedDateTimeTest.createData();
String actual = Json.createPrettyPrint(settings).writeValue(map);
String expected = ResourceData.getResourceAsString("/datetime/zoned_date_time_utc.json").trim();

assertThat(actual).isNotEqualTo(expected);
assertThat(Json.readMap(actual)).isEqualTo(Json.readMap(expected));
}

public void shouldRetrievePrettyPrintJsonSingaporeZoneWhenWriteZonedDateTimeMapWithPrettyPrint()
throws IOException {
JsonSettings settings = JsonSettings.builder()
.zoneModifier(zone -> LocalZoneId.ASIA_SINGAPORE)
.build();

Map<String, ZonedDateTime> map = ZonedDateTimeTest.createData();
String actual = Json.createPrettyPrint(settings).writeValue(map);
String expected = ResourceData.getResourceAsString("/datetime/zoned_date_time_singapore.json").trim();

assertThat(actual).isNotEqualTo(expected);
assertThat(Json.readMap(actual)).isEqualTo(Json.readMap(expected));
}
// public void shouldRetrievePrettyPrintJsonSingaporeZoneWhenWriteZonedDateTimeMapWithPrettyPrint()
// throws IOException {
// JsonSettings settings = JsonSettings.builder()
// .zoneModifier(zone -> LocalZoneId.ASIA_SINGAPORE)
// .build();
//
// Map<String, ZonedDateTime> map = ZonedDateTimeTest.createData();
// String actual = Json.createPrettyPrint(settings).writeValue(map);
// String expected = ResourceData.getResourceAsString("/datetime/zoned_date_time_singapore.json").trim();
//
// assertThat(actual).isNotEqualTo(expected);
// assertThat(Json.readMap(actual)).isEqualTo(Json.readMap(expected));
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,42 @@ public void shouldRetrieveJsonOriginalZonWhenWriteZonedDateTimeWithUseOriginalZo
+ "\"Australia/Sydney\":\"2017-07-23T13:57:14.225+10:00[Australia/Sydney]\"}");
}

public void shouldRetrieveJsonUtcZonWhenWriteZonedDateTimeWithConvertToUtcZoneModifier() {
JsonSettings settings = JsonSettings.builder()
.zoneModifier(ZoneModifier.CONVERT_TO_UTC)
.build();

String actual = Json.createWriter(settings).writeValue(createData());
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo("{\"UTC\":\"2017-07-23T13:57:14.225Z\","
+ "\"Asia/Singapore\":\"2017-07-23T05:57:14.225Z\","
+ "\"Australia/Sydney\":\"2017-07-23T03:57:14.225Z\"}");
}

public void shouldRetrieveJsonSystemDefaultZoneWhenWriteZonedDateTimeDefaultSettings() {
Map<String, ZonedDateTime> map1 = createData();
Map<String, String> map2 = withDateFormatSystemDefault(map1, JsonSettings.DF_ZONED_DATE_TIME);

String actual = Json.writeValue(map1);
String expected = String.format("{\"UTC\":\"%s\",\"Asia/Singapore\":\"%s\",\"Australia/Sydney\":\"%s\"}",
map2.get("UTC"), map2.get("Asia/Singapore"), map2.get("Australia/Sydney"));

assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
}

public void shouldRetrieveJsonSingaporeZoneWhenWriteZonedDateTimeSingaporeZone() {
JsonSettings settings = JsonSettings.builder()
.zoneModifier(zoneId -> LocalZoneId.ASIA_SINGAPORE)
.build();

String actual = Json.createWriter(settings).writeValue(createData());
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(
"{\"UTC\":\"2017-07-23T21:57:14.225+08:00[Asia/Singapore]\","
+ "\"Asia/Singapore\":\"2017-07-23T13:57:14.225+08:00[Asia/Singapore]\","
+ "\"Australia/Sydney\":\"2017-07-23T11:57:14.225+08:00[Asia/Singapore]\"}");
}
// public void shouldRetrieveJsonUtcZonWhenWriteZonedDateTimeWithConvertToUtcZoneModifier() {
// JsonSettings settings = JsonSettings.builder()
// .zoneModifier(ZoneModifier.CONVERT_TO_UTC)
// .build();
//
// String actual = Json.createWriter(settings).writeValue(createData());
// assertThat(actual).isNotNull();
// assertThat(actual).isEqualTo("{\"UTC\":\"2017-07-23T13:57:14.225Z\","
// + "\"Asia/Singapore\":\"2017-07-23T05:57:14.225Z\","
// + "\"Australia/Sydney\":\"2017-07-23T03:57:14.225Z\"}");
// }

// public void shouldRetrieveJsonSystemDefaultZoneWhenWriteZonedDateTimeDefaultSettings() {
// Map<String, ZonedDateTime> map1 = createData();
// Map<String, String> map2 = withDateFormatSystemDefault(map1, JsonSettings.DF_ZONED_DATE_TIME);
//
// String actual = Json.writeValue(map1);
// String expected = String.format("{\"UTC\":\"%s\",\"Asia/Singapore\":\"%s\",\"Australia/Sydney\":\"%s\"}",
// map2.get("UTC"), map2.get("Asia/Singapore"), map2.get("Australia/Sydney"));
//
// assertThat(actual).isNotNull();
// assertThat(actual).isEqualTo(expected);
// }

// public void shouldRetrieveJsonSingaporeZoneWhenWriteZonedDateTimeSingaporeZone() {
// JsonSettings settings = JsonSettings.builder()
// .zoneModifier(zoneId -> LocalZoneId.ASIA_SINGAPORE)
// .build();
//
// String actual = Json.createWriter(settings).writeValue(createData());
// assertThat(actual).isNotNull();
// assertThat(actual).isEqualTo(
// "{\"UTC\":\"2017-07-23T21:57:14.225+08:00[Asia/Singapore]\","
// + "\"Asia/Singapore\":\"2017-07-23T13:57:14.225+08:00[Asia/Singapore]\","
// + "\"Australia/Sydney\":\"2017-07-23T11:57:14.225+08:00[Asia/Singapore]\"}");
// }

public void shouldRetrieveDeserializedZonedDateTimeMapWhenReadJsonAsMap() {
String json = "{\"UTC\":\"2017-07-23T13:57:14.225Z\","
Expand Down
Loading

0 comments on commit 4552e27

Please sign in to comment.