Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jdubois committed Oct 16, 2015
1 parent 31c373a commit c87f363
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mongodb.Mongo;<% if (authenticationType == 'oauth2') { %>
import <%=packageName%>.config.oauth2.OAuth2AuthenticationReadConverter;<% } %>
import <%=packageName%>.domain.util.JSR310DateConverters.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package <%=packageName%>.config;

import <%=packageName%>.domain.util.*;<% if (databaseType != 'cassandra') { %>
import <%=packageName%>.domain.util.*;
<% if (databaseType != 'cassandra') { %>
import com.fasterxml.jackson.databind.SerializationFeature;<% } %>
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.*;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfiguration {

@Bean
@Bean
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
Expand All @@ -28,6 +25,4 @@ Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
.findModulesViaServiceLoader(true)
.modulesToInstall(module);
}

}

Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package <%=packageName%>.domain.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.*;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;
Expand All @@ -13,7 +10,9 @@ public final class JSR310DateConverters {
private JSR310DateConverters() {}

public static class LocalDateToDateConverter implements Converter<LocalDate, Date> {

public static final LocalDateToDateConverter INSTANCE = new LocalDateToDateConverter();

private LocalDateToDateConverter() {}

@Override
Expand Down Expand Up @@ -72,4 +71,3 @@ public LocalDateTime convert(Date source) {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

public final class JSR310DateTimeSerializer extends JsonSerializer<TemporalAccessor> {

private static final DateTimeFormatter ISOFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.of("Z"));
private static final DateTimeFormatter ISOFormatter =
DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.of("Z"));

public static final JSR310DateTimeSerializer INSTANCE = new JSR310DateTimeSerializer();

private JSR310DateTimeSerializer() {}

@Override
public void serialize(TemporalAccessor value, JsonGenerator generator, SerializerProvider serializerProvider) throws IOException {
generator.writeString(ISOFormatter.format(value));
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,47 @@
public class JSR310LocalDateDeserializer extends JsonDeserializer<LocalDate> {

public static final JSR310LocalDateDeserializer INSTANCE = new JSR310LocalDateDeserializer();

private JSR310LocalDateDeserializer() {}

private static final DateTimeFormatter ISO_DATE_OPTIONAL_TIME;

static {
ISO_DATE_OPTIONAL_TIME = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.optionalStart()
.appendLiteral('T')
.append(DateTimeFormatter.ISO_OFFSET_TIME)
.toFormatter();
}
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.optionalStart()
.appendLiteral('T')
.append(DateTimeFormatter.ISO_OFFSET_TIME)
.toFormatter();
}

@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
switch(parser.getCurrentToken())
{
case START_ARRAY:
if(parser.nextToken() == JsonToken.END_ARRAY)
return null;
int year = parser.getIntValue();

parser.nextToken();
int month = parser.getIntValue();

parser.nextToken();
int day = parser.getIntValue();

if(parser.nextToken() != JsonToken.END_ARRAY)
throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end.");
return LocalDate.of(year, month, day);

case VALUE_STRING:
String string = parser.getText().trim();
if(string.length() == 0)
return null;
return LocalDate.parse(string, ISO_DATE_OPTIONAL_TIME);
switch(parser.getCurrentToken()) {
case START_ARRAY:
if(parser.nextToken() == JsonToken.END_ARRAY) {
return null;
}
int year = parser.getIntValue();

parser.nextToken();
int month = parser.getIntValue();

parser.nextToken();
int day = parser.getIntValue();

if(parser.nextToken() != JsonToken.END_ARRAY) {
throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end.");
}
return LocalDate.of(year, month, day);

case VALUE_STRING:
String string = parser.getText().trim();
if(string.length() == 0) {
return null;
}
return LocalDate.parse(string, ISO_DATE_OPTIONAL_TIME);
}
throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}
}


Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package <%=packageName%>.domain.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import <%=packageName%>.domain.util.JSR310DateConverters.*;

import java.time.*;
import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

import <%=packageName%>.domain.util.JSR310DateConverters.*;

public final class JSR310PersistenceConverters {

private JSR310PersistenceConverters() {}
Expand Down Expand Up @@ -55,6 +53,4 @@ public LocalDateTime convertToEntityAttribute(Date date) {
return DateToLocalDateTimeConverter.INSTANCE.convert(date);
}
}

}

0 comments on commit c87f363

Please sign in to comment.