Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions JsonMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@


import com.addapay.common.response.ResponseData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import java.util.Map;


public class JsonMapper {

private final ObjectMapper objectMapper;

public JsonMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public String toJson(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
Comment on lines +19 to +25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance exception message in toJson.

Include the object in the exception message for better debugging.

-      throw new JsonException(e);
+      throw new JsonException("Failed to convert object to JSON: " + object, e);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public String toJson(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
public String toJson(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new JsonException("Failed to convert object to JSON: " + object, e);
}
}


public Map<String, Object> toMap(Object object) {
if (object == null) {
return Maps.newHashMap();
}
return objectMapper.convertValue(object, new TypeReference<>() {
});
}
Comment on lines +27 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Collections.emptyMap() for empty maps.

Replace Maps.newHashMap() with Collections.emptyMap() for better performance and readability.

-      return Maps.newHashMap();
+      return Collections.emptyMap();
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public Map<String, Object> toMap(Object object) {
if (object == null) {
return Maps.newHashMap();
}
return objectMapper.convertValue(object, new TypeReference<>() {
});
}
public Map<String, Object> toMap(Object object) {
if (object == null) {
return Collections.emptyMap();
}
return objectMapper.convertValue(object, new TypeReference<>() {
});
}


public <T> T fromJson(String jsonStr, Class<T> type) {
try {
return objectMapper.readValue(jsonStr, type);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
Comment on lines +35 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance exception message in fromJson.

Include the JSON string in the exception message for better debugging.

-      throw new JsonException(e);
+      throw new JsonException("Failed to deserialize JSON: " + jsonStr, e);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public <T> T fromJson(String jsonStr, Class<T> type) {
try {
return objectMapper.readValue(jsonStr, type);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
public <T> T fromJson(String jsonStr, Class<T> type) {
try {
return objectMapper.readValue(jsonStr, type);
} catch (JsonProcessingException e) {
throw new JsonException("Failed to deserialize JSON: " + jsonStr, e);
}
}


public <T> T fromJson(String jsonStr, TypeReference<T> type) {
try {
return objectMapper.readValue(jsonStr, type);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
Comment on lines +43 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance exception message in fromJson.

Include the JSON string in the exception message for better debugging.

-      throw new JsonException(e);
+      throw new JsonException("Failed to deserialize JSON: " + jsonStr, e);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public <T> T fromJson(String jsonStr, TypeReference<T> type) {
try {
return objectMapper.readValue(jsonStr, type);
} catch (JsonProcessingException e) {
throw new JsonException(e);
}
}
public <T> T fromJson(String jsonStr, TypeReference<T> type) {
try {
return objectMapper.readValue(jsonStr, type);
} catch (JsonProcessingException e) {
throw new JsonException("Failed to deserialize JSON: " + jsonStr, e);
}
}


public <T> T convertValue(Object body, Class<T> type) {
return objectMapper.convertValue(body, type);
}

private static class JsonException extends RuntimeException {

public JsonException(Throwable cause) {
super(cause);
}
}
Comment on lines +55 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding more constructors to JsonException.

Add constructors that accept a message, a message and a cause, etc., for more flexibility.

public JsonException(String message) {
  super(message);
}

public JsonException(String message, Throwable cause) {
  super(message, cause);
}


public static void main(String[] args) {
ResponseData responseData = new ResponseData();
System.out.println(new JsonMapper(new ObjectMapper()).toMap(responseData));
}
Comment on lines +62 to +65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add error handling and logging in main.

Include error handling and logging to make the demonstration more robust.

-    System.out.println(new JsonMapper(new ObjectMapper()).toMap(responseData));
+    try {
+      System.out.println(new JsonMapper(new ObjectMapper()).toMap(responseData));
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static void main(String[] args) {
ResponseData responseData = new ResponseData();
System.out.println(new JsonMapper(new ObjectMapper()).toMap(responseData));
}
public static void main(String[] args) {
ResponseData responseData = new ResponseData();
try {
System.out.println(new JsonMapper(new ObjectMapper()).toMap(responseData));
} catch (Exception e) {
e.printStackTrace();
}
}

}