diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/DateAdapter.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/DateAdapter.java new file mode 100644 index 000000000..e5bd9e9ef --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/DateAdapter.java @@ -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 { + + 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())); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaDateTimeAdapter.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaDateTimeAdapter.java index 4db6f30c3..d9d611b10 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaDateTimeAdapter.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaDateTimeAdapter.java @@ -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 { - @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()); diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaLocalDateAdapter.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaLocalDateAdapter.java index ee9c573b9..85acb8d99 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaLocalDateAdapter.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaLocalDateAdapter.java @@ -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 { - @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 { array.add(new JsonPrimitive(src.getMonthOfYear())); array.add(new JsonPrimitive(src.getDayOfMonth())); } - return array; } } \ No newline at end of file diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaMonthDayAdapter.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaMonthDayAdapter.java index 058bc3b46..3ff290b86 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaMonthDayAdapter.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaMonthDayAdapter.java @@ -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 { - @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; } } \ No newline at end of file diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/CommandProcessingResultJsonSerializer.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/CommandProcessingResultJsonSerializer.java index 7d8d21d9c..36c744927 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/CommandProcessingResultJsonSerializer.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/CommandProcessingResultJsonSerializer.java @@ -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; /** - *

- * A google gson implementation of - * contract. - *

+ * A Google GSON implementation of contract. * - *

* It serializes all fields of any Java {@link Object} passed to it. - *

*/ @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(); } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson.java index 685d4356e..a5d13440d 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson.java @@ -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; /** - *

- * A google gson implementation of - * contract. - *

+ * A Google GSON implementation of contract. * - *

* It serializes all fields of any Java {@link Object} passed to it. - *

*/ @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(); } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson.java index d806c0140..93bf9d26b 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson.java @@ -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; /** - *

- * A google gson implementation of - * contract. - *

+ * A Google GSON implementation of contract. * - *

* It serializes all fields of any Java {@link Object} passed to it. - *

*/ @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(); diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/GoogleGsonSerializerHelper.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/GoogleGsonSerializerHelper.java index 4cec34582..1a4834f5f 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/GoogleGsonSerializerHelper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/serialization/GoogleGsonSerializerHelper.java @@ -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 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 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 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()); + } } \ No newline at end of file