Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Dec 23, 2023
1 parent 795c9e0 commit 5786ebb
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ else if (_formatter.getZone() == null) {

@Override
protected String formatValue(Instant value, SerializerProvider provider) {
assert _formatter == null;
assert defaultFormat != null;
assert defaultFormat.getZone() != null;

ZoneId zoneId = zoneModifier.apply(defaultFormat.getZone());
ZoneId zoneId = JsonSettings.SYSTEM_DEFAULT_ZONE_ID;

if (provider.getConfig().hasExplicitTimeZone() && provider.isEnabled(WRITE_DATES_WITH_CONTEXT_TIME_ZONE))
zoneId = provider.getTimeZone().toZoneId();
else
zoneId = zoneModifier.apply(zoneId);

return defaultFormat.withZone(zoneId).format(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.testng.annotations.Test;
import ru.olegcherednik.json.jackson.LocalZoneId;

import java.time.Instant;
import java.util.TimeZone;

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

Expand All @@ -40,8 +45,62 @@ public void shouldUserSuperClassLogicWhenDateFormatIsNull() throws JsonProcessin
module.addSerializer(Instant.class, JacksonInstantSerializer.INSTANCE);

ObjectMapper mapper = new ObjectMapper().registerModule(module);
String json = mapper.writeValueAsString(Instant.parse("2023-12-23T19:22:40.758927Z"));
assertThat(json).isEqualTo("1703359360.758927000");
String json = mapper.writeValueAsString(Instant.parse("2023-12-10T19:22:40.758927Z"));
assertThat(json).isEqualTo("1702236160.758927000");
}

public void shouldUseObjectMapperGlobalZoneWhenSerializeWithGlobalZoneSetAndFormatNull()
throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addSerializer(Instant.class, new JacksonInstantSerializer(null, zoneId -> LocalZoneId.AUSTRALIA_SYDNEY));

ObjectMapper mapper = new ObjectMapper()
.setTimeZone(TimeZone.getTimeZone(LocalZoneId.ASIA_SINGAPORE))
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(module);

Instant instant = Instant.parse("2023-12-23T19:22:40.758927Z");
String json = mapper.writeValueAsString(new Data(instant));
assertThat(json).isEqualTo("{\"one\":\"2023-12-24T03:22:40.758927+08:00\"}");
}

public void shouldIgnoreObjectMapperGlobalZoneWhenFeatureDisabled()
throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addSerializer(Instant.class, new JacksonInstantSerializer(null, zoneId -> LocalZoneId.AUSTRALIA_SYDNEY));

ObjectMapper mapper = new ObjectMapper()
.setTimeZone(TimeZone.getTimeZone(LocalZoneId.ASIA_SINGAPORE))
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
.registerModule(module);

Instant instant = Instant.parse("2023-12-23T19:22:40.758927Z");
String json = mapper.writeValueAsString(new Data(instant));
assertThat(json).isEqualTo("{\"one\":\"2023-12-24T06:22:40.758927+11:00\"}");
}

public void shouldIgnoreFeatureContextTimeZoneWhenTimeZoneNotSet()
throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addSerializer(Instant.class, new JacksonInstantSerializer(null, zoneId -> LocalZoneId.AUSTRALIA_SYDNEY));

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

Instant instant = Instant.parse("2023-12-23T19:22:40.758927Z");
String json = mapper.writeValueAsString(new Data(instant));
assertThat(json).isEqualTo("{\"one\":\"2023-12-24T06:22:40.758927+11:00\"}");
}

@Getter
@RequiredArgsConstructor
private static class Data {

private final Instant one;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@

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

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.testng.annotations.Test;

import java.time.OffsetDateTime;
Expand All @@ -41,8 +45,32 @@ public void shouldUserSuperClassLogicWhenDateFormatIsNull() throws JsonProcessin
module.addSerializer(OffsetDateTime.class, JacksonOffsetDateTimeSerializer.INSTANCE);

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

String json = mapper.writeValueAsString(OffsetDateTime.parse("2023-12-23T22:16:19.989648300+03:00"));
assertThat(json).isEqualTo("1703358979.989648300");
}

public void shouldUseFeatureWhenSerializeWithFeature() throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addSerializer(OffsetDateTime.class, JacksonOffsetDateTimeSerializer.INSTANCE);

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

OffsetDateTime offsetDateTime = OffsetDateTime.parse("2023-12-23T22:16:19.989648300+03:00");
String json = mapper.writeValueAsString(new Data(offsetDateTime, offsetDateTime));
assertThat(json).isEqualTo("{\"one\":1703358979.989648300,\"two\":1703358979989}");
}

@Getter
@RequiredArgsConstructor
private static class Data {

@JsonFormat(with = JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
private final OffsetDateTime one;
private final OffsetDateTime two;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@

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

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.testng.annotations.Test;

import java.time.OffsetTime;
Expand All @@ -44,4 +48,27 @@ public void shouldUserSuperClassLogicWhenDateFormatIsNull() throws JsonProcessin
assertThat(json).isEqualTo("[22,16,19,989648300,\"+03:00\"]");
}

public void shouldUseFeatureWhenSerializeWithFeature() throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addSerializer(OffsetTime.class, JacksonOffsetTimeSerializer.INSTANCE);

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

OffsetTime offsetDateTime = OffsetTime.parse("22:16:19.989648300+03:00");
String json = mapper.writeValueAsString(new Data(offsetDateTime, offsetDateTime));
assertThat(json).isEqualTo("{\"one\":[22,16,19,989648300,\"+03:00\"],\"two\":[22,16,19,989,\"+03:00\"]}");
}

@Getter
@RequiredArgsConstructor
private static class Data {

@JsonFormat(with = JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
private final OffsetTime one;
private final OffsetTime two;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@

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

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.testng.annotations.Test;

import java.time.ZonedDateTime;
Expand All @@ -44,4 +48,27 @@ public void shouldUserSuperClassLogicWhenDateFormatIsNull() throws JsonProcessin
assertThat(json).isEqualTo("1703359236.855992000");
}

public void shouldUseFeatureWhenSerializeWithFeature() throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addSerializer(ZonedDateTime.class, JacksonZonedDateTimeSerializer.INSTANCE);

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

ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-12-23T22:20:36.855992+03:00[Europe/Moscow]");
String json = mapper.writeValueAsString(new Data(zonedDateTime, zonedDateTime));
assertThat(json).isEqualTo("{\"one\":1703359236.855992000,\"two\":1703359236855}");
}

@Getter
@RequiredArgsConstructor
private static class Data {

@JsonFormat(with = JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
private final ZonedDateTime one;
private final ZonedDateTime two;

}

}

0 comments on commit 5786ebb

Please sign in to comment.