-
Notifications
You must be signed in to change notification settings - Fork 3
Enhance: Object -> JSONObject 변환 기능 추가 #29
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
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 @@ | ||
| package com.innohotsource.hotjson.Json; | ||
|
|
||
| import org.json.simple.*; | ||
|
|
||
| import java.lang.reflect.Field; | ||
|
|
||
| public class ObjectToJson { | ||
|
|
||
| public static JSONObject toJson(Object obj) throws IllegalAccessException { | ||
| JSONObject jsonObject = new JSONObject(); | ||
| return toJson(obj, jsonObject); | ||
| } | ||
|
|
||
| private static JSONObject toJson(Object obj, JSONObject map) throws IllegalAccessException { | ||
| Class<?> clazz = obj.getClass(); | ||
| Field[] fields = clazz.getDeclaredFields(); | ||
| for (Field field : fields) { | ||
| field.setAccessible(true); | ||
| String key = field.getName(); | ||
| // Object 인 경우 || String, Wrapper Type 인 경우 || Array 인 경우??? 여튼 조건이 필요하다 | ||
| Object value = field.get(obj); | ||
|
|
||
| if(value instanceof String){ | ||
| map.put(key, value.toString()); | ||
| continue; | ||
| } | ||
|
|
||
| if(value instanceof Double){ | ||
| if(((Double)value).isInfinite() || ((Double)value).isNaN()) | ||
| map.put(key, "null"); | ||
| else | ||
| map.put(key, value); | ||
| continue; | ||
| } | ||
|
|
||
| if(value instanceof Float){ | ||
| if(((Float)value).isInfinite() || ((Float)value).isNaN()) | ||
| map.put(key, "null"); | ||
| else | ||
| map.put(key, value); | ||
| continue; | ||
| } | ||
|
|
||
| if(value instanceof Number){ | ||
| map.put(key, value); | ||
| continue; | ||
| } | ||
|
|
||
| if(value instanceof Boolean){ | ||
| map.put(key, value); | ||
| continue; | ||
| } | ||
|
Comment on lines
+23
to
+52
Contributor
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. primitive 타입을 찾는 로직을 확인했습니다. |
||
|
|
||
| // Arr 혹은 List 인지 확인하고 처리하는 로직? 아마도 필요할 것 같음... | ||
|
|
||
| if(value instanceof Object){ | ||
| JSONObject newMap = toJson(value); | ||
| map.put(key, newMap); | ||
| } | ||
|
Comment on lines
+56
to
+59
Contributor
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. JSON의 value의 object가 다양한 wrapper를 포함할때 로직을 어떻게 구현해야할지는 |
||
|
|
||
| } | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| } | ||
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.
추가 구현 부분 확인했습니다!