add java.util.Date ISO-8601 GSON type adapter (FINERACT-926) (#816)
* minor clean-up in existing JsonSerializer implementations (FINERACT-926) * centralize registration of GSON type adapters (FINERACT-926) * add java.util.Date ISO-8601 GSON type adapter (FINERACT-926)
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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 org.apache.fineract.infrastructure.core.api;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* GSON Serializer for JUL Dates (java.util.Date) in ISO-8601 format using {@link DateTimeFormatter#ISO_INSTANT} (see FINERACT-926).
|
||||
*
|
||||
* @author Michael Vorburger.ch
|
||||
*/
|
||||
public class DateAdapter implements JsonSerializer<Date> {
|
||||
|
||||
private final static DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unused")
|
||||
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
if (src == null) {
|
||||
return null;
|
||||
}
|
||||
return new JsonPrimitive(formatter.format(src.toInstant()));
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -26,15 +26,14 @@ import java.lang.reflect.Type;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* Serializer for joda time {@link DateTime} that returns date as long to match
|
||||
* previous functionality.
|
||||
* Serializer for Joda Time {@link DateTime} that returns the date as long to
|
||||
* match previous (Jackson) functionality.
|
||||
*/
|
||||
public class JodaDateTimeAdapter implements JsonSerializer<DateTime> {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
@SuppressWarnings("unused")
|
||||
public JsonElement serialize(final DateTime src, final Type typeOfSrc, final JsonSerializationContext context) {
|
||||
|
||||
JsonElement element = null;
|
||||
if (src != null) {
|
||||
element = new JsonPrimitive(src.getMillis());
|
||||
|
||||
+3
-5
@@ -27,15 +27,14 @@ import java.lang.reflect.Type;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
/**
|
||||
* Serializer for joda time {@link LocalDate} that returns date in array format
|
||||
* to match previous jackson functionality.
|
||||
* Serializer for Joda Time {@link LocalDate} that returns the date in array format
|
||||
* to match previous Jackson functionality.
|
||||
*/
|
||||
public class JodaLocalDateAdapter implements JsonSerializer<LocalDate> {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
@SuppressWarnings("unused")
|
||||
public JsonElement serialize(final LocalDate src, final Type typeOfSrc, final JsonSerializationContext context) {
|
||||
|
||||
JsonArray array = null;
|
||||
if (src != null) {
|
||||
array = new JsonArray();
|
||||
@@ -43,7 +42,6 @@ public class JodaLocalDateAdapter implements JsonSerializer<LocalDate> {
|
||||
array.add(new JsonPrimitive(src.getMonthOfYear()));
|
||||
array.add(new JsonPrimitive(src.getDayOfMonth()));
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -27,22 +27,20 @@ import java.lang.reflect.Type;
|
||||
import org.joda.time.MonthDay;
|
||||
|
||||
/**
|
||||
* Serializer for joda time {@link MonthDay} that returns date in array format
|
||||
* to match previous jackson functionality.
|
||||
* Serializer for Joda Time {@link MonthDay} that returns the date in array format
|
||||
* to match previous Jackson functionality.
|
||||
*/
|
||||
public class JodaMonthDayAdapter implements JsonSerializer<MonthDay> {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
@SuppressWarnings("unused")
|
||||
public JsonElement serialize(final MonthDay src, final Type typeOfSrc, final JsonSerializationContext context) {
|
||||
|
||||
JsonArray array = null;
|
||||
if (src != null) {
|
||||
array = new JsonArray();
|
||||
array.add(new JsonPrimitive(src.getMonthOfYear()));
|
||||
array.add(new JsonPrimitive(src.getDayOfMonth()));
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
+2
-16
@@ -20,23 +20,12 @@ package org.apache.fineract.infrastructure.core.serialization;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaDateTimeAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaLocalDateAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaMonthDayAdapter;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.MonthDay;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A google gson implementation of
|
||||
* contract.
|
||||
* </p>
|
||||
* A Google GSON implementation of contract.
|
||||
*
|
||||
* <p>
|
||||
* It serializes all fields of any Java {@link Object} passed to it.
|
||||
* </p>
|
||||
*/
|
||||
@Component
|
||||
public final class CommandProcessingResultJsonSerializer {
|
||||
@@ -45,11 +34,8 @@ public final class CommandProcessingResultJsonSerializer {
|
||||
|
||||
public CommandProcessingResultJsonSerializer() {
|
||||
final GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
GoogleGsonSerializerHelper.registerTypeAdapters(builder);
|
||||
builder.serializeNulls();
|
||||
|
||||
this.gson = builder.create();
|
||||
}
|
||||
|
||||
|
||||
+2
-15
@@ -20,23 +20,12 @@ package org.apache.fineract.infrastructure.core.serialization;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaDateTimeAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaLocalDateAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaMonthDayAdapter;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.MonthDay;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A google gson implementation of
|
||||
* contract.
|
||||
* </p>
|
||||
* A Google GSON implementation of contract.
|
||||
*
|
||||
* <p>
|
||||
* It serializes all fields of any Java {@link Object} passed to it.
|
||||
* </p>
|
||||
*/
|
||||
@Component
|
||||
public final class ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson {
|
||||
@@ -45,9 +34,7 @@ public final class ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson {
|
||||
|
||||
public ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson() {
|
||||
final GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
GoogleGsonSerializerHelper.registerTypeAdapters(builder);
|
||||
|
||||
this.gson = builder.create();
|
||||
}
|
||||
|
||||
+2
-15
@@ -20,23 +20,12 @@ package org.apache.fineract.infrastructure.core.serialization;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaDateTimeAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaLocalDateAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaMonthDayAdapter;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.MonthDay;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A google gson implementation of
|
||||
* contract.
|
||||
* </p>
|
||||
* A Google GSON implementation of contract.
|
||||
*
|
||||
* <p>
|
||||
* It serializes all fields of any Java {@link Object} passed to it.
|
||||
* </p>
|
||||
*/
|
||||
@Component
|
||||
public final class ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson {
|
||||
@@ -45,9 +34,7 @@ public final class ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson {
|
||||
|
||||
public ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson() {
|
||||
final GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
GoogleGsonSerializerHelper.registerTypeAdapters(builder);
|
||||
builder.setPrettyPrinting();
|
||||
|
||||
this.gson = builder.create();
|
||||
|
||||
+12
-12
@@ -24,6 +24,7 @@ import com.google.gson.GsonBuilder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.apache.fineract.infrastructure.core.api.DateAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaDateTimeAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaLocalDateAdapter;
|
||||
import org.apache.fineract.infrastructure.core.api.JodaMonthDayAdapter;
|
||||
@@ -36,16 +37,14 @@ import org.joda.time.MonthDay;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Helper class for serialization of java objects into JSON using google-gson.
|
||||
* Helper class for serialization of Java objects into JSON using Google's GSON.
|
||||
*/
|
||||
@Service
|
||||
public final class GoogleGsonSerializerHelper {
|
||||
|
||||
public Gson createGsonBuilder(final boolean prettyPrint) {
|
||||
final GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
registerTypeAdapters(builder);
|
||||
if (prettyPrint) {
|
||||
builder.setPrettyPrinting();
|
||||
}
|
||||
@@ -53,13 +52,10 @@ public final class GoogleGsonSerializerHelper {
|
||||
}
|
||||
|
||||
public Gson createGsonBuilderForPartialResponseFiltering(final boolean prettyPrint, final Set<String> responseParameters) {
|
||||
|
||||
final ExclusionStrategy strategy = new ParameterListInclusionStrategy(responseParameters);
|
||||
|
||||
final GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
registerTypeAdapters(builder);
|
||||
if (prettyPrint) {
|
||||
builder.setPrettyPrinting();
|
||||
}
|
||||
@@ -72,7 +68,6 @@ public final class GoogleGsonSerializerHelper {
|
||||
final Set<String> parameterNamesToSkip = new HashSet<>();
|
||||
|
||||
if (!responseParameters.isEmpty()) {
|
||||
|
||||
// strip out all known support parameters from expected response to
|
||||
// see if unsupported parameters requested for response.
|
||||
final Set<String> differentParametersDetectedSet = new HashSet<>(responseParameters);
|
||||
@@ -88,9 +83,7 @@ public final class GoogleGsonSerializerHelper {
|
||||
final ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
|
||||
|
||||
final GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
registerTypeAdapters(builder);
|
||||
if (prettyPrint) {
|
||||
builder.setPrettyPrinting();
|
||||
}
|
||||
@@ -104,4 +97,11 @@ public final class GoogleGsonSerializerHelper {
|
||||
public String serializedJsonFrom(final Gson serializer, final Object singleDataObject) {
|
||||
return serializer.toJson(singleDataObject);
|
||||
}
|
||||
|
||||
public static void registerTypeAdapters(final GsonBuilder builder) {
|
||||
builder.registerTypeAdapter(java.util.Date.class, new DateAdapter());
|
||||
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
|
||||
builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
|
||||
builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user