Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Jan 2, 2024
1 parent 5bb7066 commit 8851720
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

package ru.olegcherednik.json.jackson.datetime.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import ru.olegcherednik.json.api.JsonSettings;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,10 @@ protected JacksonLocalDateSerializer withFormat(Boolean useTimestamp,

@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (useTimestamp(provider)) {
if (_shape == JsonFormat.Shape.ARRAY) {
gen.writeStartArray();
_serializeAsArrayContents(value, gen, provider);
gen.writeEndArray();
} else
gen.writeNumber(value.toEpochDay());
} else if (_formatter == null)
gen.writeString(value.toString());
if (useTimestamp(provider) || _formatter != null)
super.serialize(value, gen, provider);
else
gen.writeString(_formatter.format(value));
gen.writeString(value.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.testng.annotations.Test;
import ru.olegcherednik.json.api.Json;
import ru.olegcherednik.json.api.JsonSettings;
Expand All @@ -38,7 +39,6 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -59,7 +59,6 @@ public class DateTimeCombinationTest {

public void shouldWriteDatesWithDefaultJsonSettings() throws IOException {
DataOne data = new DataOne(ZONED_DATE_TIME);

String actual = Json.writeValue(data);
String expected = ResourceData.getResourceAsString("/datetime/def_date_one.json").trim();
assertThat(actual).isEqualTo(expected);
Expand All @@ -68,39 +67,31 @@ public void shouldWriteDatesWithDefaultJsonSettings() throws IOException {
public void shouldReadDatesWithDefaultJsonSettings() throws IOException {
String json = ResourceData.getResourceAsString("/datetime/def_date_one.json").trim();
DataOne actual = Json.readValue(json, DataOne.class);
DataOne expected = new DataOne(ZONED_DATE_TIME.toInstant(),
ZONED_DATE_TIME.toLocalDate(),
ZONED_DATE_TIME.toLocalTime(),
ZONED_DATE_TIME.toLocalDateTime(),
ZONED_DATE_TIME.toOffsetDateTime().toOffsetTime(),
ZONED_DATE_TIME.toOffsetDateTime(),
ZONED_DATE_TIME.withZoneSameInstant(ZoneId.systemDefault()),
Date.from(ZONED_DATE_TIME.toInstant()));
DataOne expected = new DataOne(ZONED_DATE_TIME);
assertThat(actual).isEqualTo(expected);
}

public void shouldWriteDatesWithAnnotationSettingsPreferable() throws IOException {
DataTwo data = new DataTwo(ZonedDateTime.parse("2023-12-03T10:39:20.187+03:00"));
DataTwo data = new DataTwo(ZONED_DATE_TIME);
String actual = Json.createWriter(getSettings()).writeValue(data);
String expected = ResourceData.getResourceAsString("/datetime/custom_date_two.json").trim();
assertThat(actual).isEqualTo(expected);
}

public void shouldReadDatesWithAnnotationSettingsPreferable() throws IOException {
ZonedDateTime zdtLocal = ZonedDateTime.parse("2023-12-03T10:39:20.187+03:00");
ZonedDateTime zdtSingapore = zdtLocal.withZoneSameInstant(LocalZoneId.ASIA_SINGAPORE);
ZonedDateTime zdtSingapore = ZONED_DATE_TIME.withZoneSameInstant(LocalZoneId.ASIA_SINGAPORE);

String json = ResourceData.getResourceAsString("/datetime/custom_date_two.json").trim();

DataTwo actual = Json.createReader(getSettings()).readValue(json, DataTwo.class);
DataTwo expected = new DataTwo(zdtLocal.toInstant(),
zdtLocal.toLocalDate(),
zdtLocal.toLocalTime(),
zdtLocal.toLocalDateTime(),
DataTwo expected = new DataTwo(ZONED_DATE_TIME.toInstant(),
ZONED_DATE_TIME.toLocalDate(),
ZONED_DATE_TIME.toLocalTime(),
ZONED_DATE_TIME.toLocalDateTime(),
zdtSingapore.toOffsetDateTime().toOffsetTime(),
zdtSingapore.toOffsetDateTime(),
zdtSingapore,
Date.from(zdtLocal.toInstant()));
Date.from(ZONED_DATE_TIME.toInstant()));


assertThat(actual).isEqualTo(expected);
Expand All @@ -125,41 +116,18 @@ public void shouldWriteDatesAsNumbersWhenAnnotationSettingsNumberInt() throws IO
assertThat(actual).isEqualTo(expected);
}

@EqualsAndHashCode
static class Data1 {

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private Instant instant = ZONED_DATE_TIME.toInstant();
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private LocalDate localDate = ZONED_DATE_TIME.toLocalDate();
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private LocalTime localTime = ZONED_DATE_TIME.toLocalTime();
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private LocalDateTime localDateTime = ZONED_DATE_TIME.toLocalDateTime();
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private OffsetTime offsetTime = ZONED_DATE_TIME.toOffsetDateTime().toOffsetTime();
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private OffsetDateTime offsetDateTime = ZONED_DATE_TIME.toOffsetDateTime();
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private ZonedDateTime zonedDateTime = ZONED_DATE_TIME.withZoneSameInstant(ZoneOffset.UTC);
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private Date date = Date.from(ZONED_DATE_TIME.toInstant());

}

public void shouldReadDatesAsNumber() throws IOException {
Data1 expected = new Data1();
String json = Json.writeValue(expected);
Data1 actual = Json.readValue(json, Data1.class);

// String json = ResourceData.getResourceAsString("/datetime/date_one_num.json").trim();

// DataThree actual = Json.readValue(json, DataThree.class);
// DataThree expected = new DataThree();

// assertThat(actual).isEqualTo(expected);
int a = 0;
a++;
public void shouldReadDatesAsNumbersWhenAnnotationSettingsNumberInt() throws IOException {
String json = ResourceData.getResourceAsString("/datetime/date_one_num.json").trim();
DataThree actual = Json.readValue(json, DataThree.class);
DataThree expected = new DataThree(ZONED_DATE_TIME.toInstant(),
ZONED_DATE_TIME.toLocalDate(),
ZONED_DATE_TIME.toLocalTime(),
ZONED_DATE_TIME.toLocalDateTime(),
ZONED_DATE_TIME.toOffsetDateTime().toOffsetTime(),
ZONED_DATE_TIME.toOffsetDateTime(),
ZONED_DATE_TIME.withZoneSameInstant(ZoneOffset.UTC),
Date.from(ZONED_DATE_TIME.toInstant()));
assertThat(actual).isEqualTo(expected);
}

@Getter
Expand Down Expand Up @@ -265,6 +233,7 @@ private DataTwo(@JsonProperty("instant") Instant instant,

@Getter
@EqualsAndHashCode
@ToString
private static final class DataThree {

@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.testng.annotations.Test;
import ru.olegcherednik.json.api.Json;
import ru.olegcherednik.json.api.JsonSettings;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Expand All @@ -54,19 +55,27 @@ public void shouldUseToStringWhenDateFormatIsNull() throws JsonProcessingExcepti
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(module);

String json = mapper.writeValueAsString(new Data(LOCAL_DATE));
Data expected = new Data(LOCAL_DATE);
String json = mapper.writeValueAsString(expected);
assertThat(json).isEqualTo("{\"map\":{\"localDate\":\"2023-12-10\"}}");

Data actual = Json.readValue(json, Data.class);
assertThat(actual).isEqualTo(expected);
}

public void shouldUseEpochDayWhenWriteDateAsTimestamps() throws JsonProcessingException {
public void shouldSerializeArrayWhenWriteDateAsTimestamps() throws JsonProcessingException {
SimpleModule module = createModule(null);

ObjectMapper mapper = new ObjectMapper()
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(module);

String json = mapper.writeValueAsString(new Data(LOCAL_DATE));
assertThat(json).isEqualTo("{\"map\":{\"localDate\":19701}}");
Data expected = new Data(LOCAL_DATE);
String json = mapper.writeValueAsString(expected);
assertThat(json).isEqualTo("{\"map\":{\"localDate\":[2023,12,10]}}");

Data actual = Json.readValue(json, Data.class);
assertThat(actual).isEqualTo(expected);
}

public void shouldUseDateFormatWhenDateFormatNotNull() throws JsonProcessingException {
Expand All @@ -75,50 +84,52 @@ public void shouldUseDateFormatWhenDateFormatNotNull() throws JsonProcessingExce

ObjectMapper mapper = new ObjectMapper().registerModule(module);

String json = mapper.writeValueAsString(new Data(LOCAL_DATE));
Data expected = new Data(LOCAL_DATE);
String json = mapper.writeValueAsString(expected);
assertThat(json).isEqualTo("{\"map\":{\"localDate\":\"10-12-2023\"}}");
}

public void shouldSerializeTimestampWhenShapeNumberInt() throws JsonProcessingException {
@Getter
@RequiredArgsConstructor
class Data {

@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
private final LocalDate localDate;

}
JsonSettings settings = JsonSettings.builder().localDateFormatter(df).build();
Data actual = Json.createReader(settings).readValue(json, Data.class);
assertThat(actual).isEqualTo(expected);
}

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
SimpleModule module = createModule(df);
public void shouldSerializeEpochDayWhenWriteDateAsTimestampsAndShapeNumberInt() throws JsonProcessingException {
SimpleModule module = createModule(DateTimeFormatter.ISO_LOCAL_DATE);

ObjectMapper mapper = new ObjectMapper()
.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(module);

String json = mapper.writeValueAsString(new Data(LOCAL_DATE));
DataInt expected = new DataInt();
String json = mapper.writeValueAsString(expected);
assertThat(json).isEqualTo("{\"localDate\":19701}");
}

public void shouldSerializeArrayWhenShapeNotNumberInt() throws JsonProcessingException {
@Getter
@RequiredArgsConstructor
class Data {
DataInt actual = Json.readValue(json, DataInt.class);
assertThat(actual).isEqualTo(expected);
}

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private final LocalDate localDate;
public void shouldSerializeArrayWhenWriteDateAsTimestampsAndShapeNumberFloat() throws JsonProcessingException {
SimpleModule module = createModule(DateTimeFormatter.ISO_LOCAL_DATE);
ObjectMapper mapper = new ObjectMapper().registerModule(module);

}
DataFloat expected = new DataFloat();
String json = mapper.writeValueAsString(expected);
assertThat(json).isEqualTo("{\"localDate\":[2023,12,10]}");

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
SimpleModule module = createModule(df);
DataFloat actual = Json.readValue(json, DataFloat.class);
assertThat(actual).isEqualTo(expected);
}

ObjectMapper mapper = new ObjectMapper()
.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.registerModule(module);
public void shouldSerializeArrayWhenShapeNumberArray() throws JsonProcessingException {
SimpleModule module = createModule(DateTimeFormatter.ISO_LOCAL_DATE);
ObjectMapper mapper = new ObjectMapper().registerModule(module);

String json = mapper.writeValueAsString(new Data(LOCAL_DATE));
DataArray expected = new DataArray();
String json = mapper.writeValueAsString(expected);
assertThat(json).isEqualTo("{\"localDate\":[2023,12,10]}");

DataArray actual = Json.readValue(json, DataArray.class);
assertThat(actual).isEqualTo(expected);
}

private static SimpleModule createModule(DateTimeFormatter df) {
Expand All @@ -145,4 +156,28 @@ private Data(LocalDate value) {

}

@EqualsAndHashCode
private static final class DataInt {

@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
private final LocalDate localDate = LOCAL_DATE;

}

@EqualsAndHashCode
private static final class DataFloat {

@JsonFormat(shape = JsonFormat.Shape.NUMBER_FLOAT)
private final LocalDate localDate = LOCAL_DATE;

}

@EqualsAndHashCode
private static final class DataArray {

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
private final LocalDate localDate = LOCAL_DATE;

}

}
Loading

0 comments on commit 8851720

Please sign in to comment.