-
Notifications
You must be signed in to change notification settings - Fork 0
Create JsonMapper.java #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Replace - return Maps.newHashMap();
+ return Collections.emptyMap();Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance exception message in 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
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance exception message in 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
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding more constructors to 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling and logging in 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
Suggested change
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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.
Committable suggestion