@@ -46,7 +46,19 @@ public static Object[] arrayToObjects(JSONArray array) {
4646 }
4747 Object [] res = new Object [array .size ()];
4848 for (int i =0 ; i < array .size (); i ++) {
49- res [i ] = array .get (i );
49+ res [i ] = resolveValue (array .get (i ));
50+ }
51+
52+ return res ;
53+ }
54+
55+ public static Object [] arrayToObjects (Object [] array ) {
56+ if (array == null ) {
57+ return null ;
58+ }
59+ Object [] res = new Object [array .length ];
60+ for (int i =0 ; i < array .length ; i ++) {
61+ res [i ] = resolveValue (array [i ]);
5062 }
5163
5264 return res ;
@@ -63,7 +75,19 @@ public static Map<String, Object> objectToMap(JSONObject object) {
6375 }
6476 Map <String , Object > res = new HashMap <>();
6577 for (String key : object .keySet ()) {
66- res .put (key , object .get (key ));
78+ res .put (key , resolveValue (object .get (key )));
79+ }
80+
81+ return res ;
82+ }
83+
84+ public static Map <String , Object > objectToMap (Map <String , Object > object ) {
85+ if (object == null ) {
86+ return null ;
87+ }
88+ Map <String , Object > res = new HashMap <>();
89+ for (String key : object .keySet ()) {
90+ res .put (key , resolveValue (object .get (key )));
6791 }
6892
6993 return res ;
@@ -75,11 +99,34 @@ public static Map<String, Object> objectToMap(JSONObject object) {
7599 * @return pure java object.
76100 */
77101 public static Object resolveValue (Object value ) {
78- if (value instanceof JSONArray ) {
79- return Utils .arrayToObjects ((JSONArray ) value );
80- } else if (value instanceof JSONObject ) {
81- return Utils .objectToMap ((JSONObject ) value );
102+ return resolveValue (value , getType (value ));
103+ }
104+
105+ public static Object resolveValue (Object value , JSONType t ) {
106+ if (t == null ) {
107+ return null ;
82108 }
83- return value ;
109+
110+ switch (t ) {
111+ case Object :
112+ if (value instanceof JSONObject ) {
113+ return Utils .objectToMap ((JSONObject ) value );
114+ } else if (value instanceof Map ) {
115+ return Utils .objectToMap ((Map <String , Object >) value );
116+ }
117+ break ;
118+ case Array :
119+ if (value instanceof JSONArray ) {
120+ return Utils .arrayToObjects ((JSONArray ) value );
121+ } else if (value instanceof Object []) {
122+ return Utils .arrayToObjects ((Object []) value );
123+ }
124+ break ;
125+ case Null :
126+ return null ;
127+ default :
128+ return value ;
129+ }
130+ return null ;
84131 }
85132}
0 commit comments