This is an automated email from the ASF dual-hosted git repository.

awasum pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new bee3fb7  add java.util.Date ISO-8601 GSON type adapter (FINERACT-926) 
(#816)
bee3fb7 is described below

commit bee3fb7ce52dc1d39f7e7adc1bf299786a7323ad
Author: Michael Vorburger ⛑️ <[email protected]>
AuthorDate: Fri May 8 20:56:19 2020 +0200

    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)
---
 .../{JodaDateTimeAdapter.java => DateAdapter.java} | 25 +++++++++++-----------
 .../core/api/JodaDateTimeAdapter.java              |  7 +++---
 .../core/api/JodaLocalDateAdapter.java             |  8 +++----
 .../core/api/JodaMonthDayAdapter.java              |  8 +++----
 .../CommandProcessingResultJsonSerializer.java     | 18 ++--------------
 ...hPrettyPrintingOffJsonSerializerGoogleGson.java | 17 ++-------------
 ...thPrettyPrintingOnJsonSerializerGoogleGson.java | 17 ++-------------
 .../serialization/GoogleGsonSerializerHelper.java  | 24 ++++++++++-----------
 8 files changed, 40 insertions(+), 84 deletions(-)

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/DateAdapter.java
similarity index 65%
copy from 
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JodaDateTimeAdapter.java
copy to 
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/DateAdapter.java
index 4db6f30..e5bd9e9 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/DateAdapter.java
@@ -23,23 +23,24 @@ import com.google.gson.JsonPrimitive;
 import com.google.gson.JsonSerializationContext;
 import com.google.gson.JsonSerializer;
 import java.lang.reflect.Type;
-import org.joda.time.DateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
 
 /**
- * Serializer for joda time {@link DateTime} that returns date as long to match
- * previous functionality.
+ * 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 JodaDateTimeAdapter implements JsonSerializer<DateTime> {
+public class DateAdapter implements JsonSerializer<Date> {
 
-    @SuppressWarnings("unused")
-    @Override
-    public JsonElement serialize(final DateTime src, final Type typeOfSrc, 
final JsonSerializationContext context) {
+    private final static DateTimeFormatter formatter = 
DateTimeFormatter.ISO_INSTANT;
 
-        JsonElement element = null;
-        if (src != null) {
-            element = new JsonPrimitive(src.getMillis());
+    @Override
+    @SuppressWarnings("unused")
+    public JsonElement serialize(Date src, Type typeOfSrc, 
JsonSerializationContext context) {
+        if (src == null) {
+            return null;
         }
-
-        return element;
+        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 4db6f30..d9d611b 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<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());
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 ee9c573..85acb8d 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<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;
     }
 }
\ 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 058bc3b..3ff290b 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<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;
     }
 }
\ 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 7d8d21d..36c7449 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;
 
 /**
- * <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();
     }
 
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 685d435..a5d1344 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;
 
 /**
- * <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();
     }
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 d806c01..93bf9d2 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;
 
 /**
- * <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();
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 4cec345..1a4834f 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<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());
+    }
 }
\ No newline at end of file

Reply via email to