Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Jan 5, 2024
1 parent e0ab06f commit 6703ee1
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 46 deletions.
10 changes: 10 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://projectlombok.org/features/configuration

# configuration file is in the root of the workspace directory only
config.stopBubbling = true
# If true, lombok will generate a @java.beans.ConstructorProperties annotation when generating constructors.
lombok.anyConstructor.addConstructorProperties=true
# Do not use experimental annotations
lombok.experimental.flagUsage=ERROR
# Lombok marks generated code with @lombok.Generated, so JaCoCo can understand it
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ private static ObjectMapper registerModules(ObjectMapper mapper, JsonSettings se
mapper.registerModule(module);

if ("jackson-datatype-jsr310".equals(module.getModuleName()))
mapper.registerModule(new JacksonJavaTimeModule(settings.getInstantFormatter(),
settings.getLocalDateFormatter(),
settings.getLocalTimeFormatter(),
settings.getLocalDateTimeFormatter(),
settings.getOffsetTimeFormatter(),
settings.getOffsetDateTimeFormatter(),
settings.getZonedDateTimeFormatter()));
mapper.registerModule(JacksonJavaTimeModule.builder()
.instant(settings.getInstantFormatter())
.localDate(settings.getLocalDateFormatter())
.localTime(settings.getLocalTimeFormatter())
.localDateTime(settings.getLocalDateTimeFormatter())
.offsetTime(settings.getOffsetTimeFormatter())
.offsetDateTime(settings.getOffsetDateTimeFormatter())
.zonedDateTime(settings.getZonedDateTimeFormatter())
.build());
});

return mapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import ru.olegcherednik.json.api.JsonEngine;
import ru.olegcherednik.json.api.iterator.AutoCloseableIterator;
import ru.olegcherednik.json.jackson.types.ListMapTypeReference;
import ru.olegcherednik.json.jackson.types.MappingIteratorDecorator;
import ru.olegcherednik.json.jackson.types.MappingAutoCloseableIterator;

import java.io.IOException;
import java.io.Reader;
Expand Down Expand Up @@ -111,13 +111,13 @@ public List<Map<String, Object>> readListOfMap(Reader reader) throws IOException
@Override
public <V> AutoCloseableIterator<V> readListLazy(Reader reader, Class<V> valueClass) throws IOException {
MappingIterator<V> it = mapper.readerFor(valueClass).readValues(reader);
return new MappingIteratorDecorator<>(it);
return new MappingAutoCloseableIterator<>(it);
}

@Override
public AutoCloseableIterator<Map<String, Object>> readListOfMapLazy(Reader reader) throws IOException {
MappingIterator<Map<String, Object>> it = mapper.readerFor(Map.class).readValues(reader);
return new MappingIteratorDecorator<>(it);
return new MappingAutoCloseableIterator<>(it);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import ru.olegcherednik.json.jackson.datetime.deserializers.JacksonInstantDeserializer;
import ru.olegcherednik.json.jackson.datetime.deserializers.JacksonJsr310KeyDeserializer;
Expand Down Expand Up @@ -60,7 +62,8 @@
* @author Oleg Cherednik
* @since 14.12.2023
*/
@RequiredArgsConstructor
@Builder
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class JacksonJavaTimeModule extends SimpleModule {

private static final long serialVersionUID = 7443855953089866124L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @since 03.01.2024
*/
@RequiredArgsConstructor
public class MappingIteratorDecorator<T> implements AutoCloseableIterator<T> {
public class MappingAutoCloseableIterator<T> implements AutoCloseableIterator<T> {

protected final MappingIterator<T> delegate;

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/ru/olegcherednik/json/jackson/WriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -87,6 +88,13 @@ public void shouldRetrieveJsonWhenWriteListObject() {
assertThat(actual).isEqualTo("[{\"intVal\":555,\"strVal\":\"victory\"},{\"intVal\":666,\"strVal\":\"omen\"}]");
}

public void shouldRetrieveJsonWhenWriteIterator() {
List<Data> data = new ArrayList<>(ListUtils.of(Data.VICTORY, Data.OMEN));
String actual = Json.writeValue(data.iterator());
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo("[{\"intVal\":555,\"strVal\":\"victory\"},{\"intVal\":666,\"strVal\":\"omen\"}]");
}

public void shouldRetrieveEmptyJsonWhenWriteEmptyCollection() {
assertThat(Json.writeValue(Collections.emptyList())).isEqualTo("[]");
assertThat(Json.writeValue(Collections.emptyMap())).isEqualTo("{}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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;

import org.testng.annotations.Test;
import ru.olegcherednik.json.api.Json;
import ru.olegcherednik.json.api.JsonSettings;
import ru.olegcherednik.json.jackson.LocalZoneId;
import ru.olegcherednik.json.jackson.MapUtils;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Oleg Cherednik
* @since 03.01.2024
*/
@Test
public class OffsetDateTimeTest {

public void shouldRetrieveJsonOriginalWhenWriteDefaultSettings() {
String actual = Json.writeValue(createData());
assertThat(actual).isNotNull().isEqualTo(
"{\"UTC\":\"2017-07-23T13:57:14.225Z\","
+ "\"Asia/Singapore\":\"2017-07-23T13:57:14.225+08:00\","
+ "\"Australia/Sydney\":\"2017-07-23T13:57:14.225+10:00\"}");
}

public void shouldRetrieveJsonUtcWhenWriteOffsetDateTimeWithUtcZoneId() {
JsonSettings settings = JsonSettings.builder().zoneId(ZoneOffset.UTC).build();
String actual = Json.createWriter(settings).writeValue(createData());
assertThat(actual).isNotNull().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 shouldRetrieveJsonSingaporeWhenWriteOffsetDateTimeWithSingaporeZoneId() {
JsonSettings settings = JsonSettings.builder().zoneId(LocalZoneId.ASIA_SINGAPORE).build();
String actual = Json.createWriter(settings).writeValue(createData());
assertThat(actual).isNotNull().isEqualTo(
"{\"UTC\":\"2017-07-23T21:57:14.225+08:00\","
+ "\"Asia/Singapore\":\"2017-07-23T13:57:14.225+08:00\","
+ "\"Australia/Sydney\":\"2017-07-23T11:57:14.225+08:00\"}");
}

public void shouldRetrieveDeserializedOffsetDateTimeMapWhenReadJsonAsMap() {
String json = "{\"UTC\":\"2017-07-23T13:57:14.225Z\","
+ "\"Asia/Singapore\":\"2017-07-23T13:57:14.225+08:00\","
+ "\"Australia/Sydney\":\"2017-07-23T13:57:14.225+10:00\"}";

Map<String, OffsetDateTime> actual = Json.readMap(json, String.class, OffsetDateTime.class);
Map<String, OffsetDateTime> expected = createData();
assertThat(actual).isNotNull().isEqualTo(expected);
}

private static Map<String, OffsetDateTime> createData() {
return MapUtils.of("UTC", OffsetDateTime.parse("2017-07-23T13:57:14.225Z"),
"Asia/Singapore", OffsetDateTime.parse("2017-07-23T13:57:14.225+08:00"),
"Australia/Sydney", OffsetDateTime.parse("2017-07-23T13:57:14.225+10:00"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ru.olegcherednik.json.api.Json;
import ru.olegcherednik.json.api.JsonSettings;
import ru.olegcherednik.json.jackson.LocalZoneId;
import ru.olegcherednik.json.jackson.MapUtils;
import ru.olegcherednik.json.jackson.ResourceData;

import java.io.IOException;
Expand All @@ -40,12 +41,8 @@
public class ZonedDateTimePrettyPrintTest {

public void shouldRetrievePrettyPrintJsonUtcZoneWhenWriteZonedDateTimeMapWithPrettyPrint() throws IOException {
JsonSettings settings = JsonSettings.builder()
.zoneId(ZoneOffset.UTC)
.build();

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

assertThat(actual).isNotEqualTo(expected);
Expand All @@ -54,16 +51,18 @@ public void shouldRetrievePrettyPrintJsonUtcZoneWhenWriteZonedDateTimeMapWithPre

public void shouldRetrievePrettyPrintJsonSingaporeZoneWhenWriteZonedDateTimeMapWithPrettyPrint()
throws IOException {
JsonSettings settings = JsonSettings.builder()
.zoneId(LocalZoneId.ASIA_SINGAPORE)
.build();

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

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

private static Map<String, ZonedDateTime> createData() {
return MapUtils.of("UTC", ZonedDateTime.parse("2017-07-23T13:57:14.225Z"),
"Asia/Singapore", ZonedDateTime.parse("2017-07-23T13:57:14.225+08:00[Asia/Singapore]"),
"Australia/Sydney", ZonedDateTime.parse("2017-07-23T13:57:14.225+10:00[Australia/Sydney]"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -40,24 +39,20 @@
public class ZonedDateTimeTest {

public void shouldRetrieveJsonOriginalZoneWhenWriteDefaultSettings() {
String actual = Json.createWriter().writeValue(createData());
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(
String actual = Json.writeValue(createData());
assertThat(actual).isNotNull().isEqualTo(
"{\"UTC\":\"2017-07-23T13:57:14.225Z\","
+ "\"Asia/Singapore\":\"2017-07-23T13:57:14.225+08:00[Asia/Singapore]\","
+ "\"Australia/Sydney\":\"2017-07-23T13:57:14.225+10:00[Australia/Sydney]\"}");
}

public void shouldRetrieveJsonUtcZoneWhenWriteZonedDateTimeWithUtcZoneId() {
JsonSettings settings = JsonSettings.builder()
.zoneId(ZoneOffset.UTC)
.build();
JsonSettings settings = JsonSettings.builder().zoneId(ZoneOffset.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\"}");
assertThat(actual).isNotNull().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 shouldRetrieveJsonSingaporeWhenWriteZonedDateTimeWithSingaporeZoneId() {
Expand All @@ -66,8 +61,7 @@ public void shouldRetrieveJsonSingaporeWhenWriteZonedDateTimeWithSingaporeZoneId
.build();

String actual = Json.createWriter(settings).writeValue(createData());
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(
assertThat(actual).isNotNull().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]\"}");
Expand All @@ -80,17 +74,13 @@ public void shouldRetrieveDeserializedZonedDateTimeMapWhenReadJsonAsMap() {

Map<String, ZonedDateTime> actual = Json.readMap(json, String.class, ZonedDateTime.class);
Map<String, ZonedDateTime> expected = createData();
assertThat(actual).isNotNull();
assertThat(actual).isEqualTo(expected);
assertThat(actual).isNotNull().isEqualTo(expected);
}

static Map<String, ZonedDateTime> createData() {
String str = "2017-07-23T13:57:14.225";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

return MapUtils.of("UTC", ZonedDateTime.parse(str, df.withZone(ZoneOffset.UTC)),
"Asia/Singapore", ZonedDateTime.parse(str, df.withZone(LocalZoneId.ASIA_SINGAPORE)),
"Australia/Sydney", ZonedDateTime.parse(str, df.withZone(LocalZoneId.AUSTRALIA_SYDNEY)));
private static Map<String, ZonedDateTime> createData() {
return MapUtils.of("UTC", ZonedDateTime.parse("2017-07-23T13:57:14.225Z"),
"Asia/Singapore", ZonedDateTime.parse("2017-07-23T13:57:14.225+08:00[Asia/Singapore]"),
"Australia/Sydney", ZonedDateTime.parse("2017-07-23T13:57:14.225+10:00[Australia/Sydney]"));
}

}

0 comments on commit 6703ee1

Please sign in to comment.