[ 
https://issues.apache.org/jira/browse/GOBBLIN-2158?focusedWorklogId=935120&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-935120
 ]

ASF GitHub Bot logged work on GOBBLIN-2158:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/Sep/24 05:22
            Start Date: 18/Sep/24 05:22
    Worklog Time Spent: 10m 
      Work Description: pratapaditya04 commented on code in PR #4057:
URL: https://github.com/apache/gobblin/pull/4057#discussion_r1764403116


##########
gobblin-api/src/main/java/org/apache/gobblin/util/io/GsonInterfaceAdapter.java:
##########
@@ -189,7 +192,38 @@ private <S> S readValue(JsonObject jsonObject, 
TypeToken<S> defaultTypeToken) th
   }
 
   public static <T> Gson getGson(Class<T> clazz) {
-    Gson gson = new GsonBuilder().registerTypeAdapterFactory(new 
GsonInterfaceAdapter(clazz)).create();
+    Gson gson = new GsonBuilder()
+        
.setObjectToNumberStrategy(CustomToNumberPolicy.INTEGER_OR_LONG_OR_DOUBLE)
+        .registerTypeAdapterFactory(new GsonInterfaceAdapter(clazz))
+        .create();
     return gson;
   }
+
+  public enum CustomToNumberPolicy implements ToNumberStrategy {
+    INTEGER_OR_LONG_OR_DOUBLE {
+      @Override
+      public Number readNumber(JsonReader in) throws IOException {
+        String value = in.nextString();
+        try {
+          return Integer.parseInt(value);
+        } catch (NumberFormatException var3) {
+          try {
+            return Long.parseLong(value);
+          } catch (NumberFormatException var2) {

Review Comment:
   Nice work on the custom number parsing. But this includes a lot of nesting 
which could imapact readibility and maintainability , I sugges tbreaking down 
the nested try-catch blocks in readNumber into smaller methods for each number 
type (e.g., tryParseInteger, tryParseLong, tryParseDouble). This will improve 
readability and maintainability by reducing nesting.
   Sample code
   ```
   `private Number parseNumber(String value, JsonReader in) throws IOException {
       Number number = tryParseInteger(value);
       if (number != null) return number;
   
       number = tryParseLong(value);
       if (number != null) return number;
   
       return tryParseDouble(value, in);
   }
   
   private Number tryParseInteger(String value) { /* ... */ }
   private Number tryParseLong(String value) { /* ... */ }
   private Number tryParseDouble(String value, JsonReader in) throws 
IOException { /* ... */ }
   ```
   `





Issue Time Tracking
-------------------

    Worklog Id:     (was: 935120)
    Time Spent: 1h 20m  (was: 1h 10m)

> Upgrade GSON version to 2.8.9
> -----------------------------
>
>                 Key: GOBBLIN-2158
>                 URL: https://issues.apache.org/jira/browse/GOBBLIN-2158
>             Project: Apache Gobblin
>          Issue Type: Task
>            Reporter: Vivek Rai
>            Priority: Major
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> Upgrade GSON version to 2.8.9 and add a custom ObjectToIntegerDeserialize 
> policy to avoid getting integer converted to double or long while deserialize.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to