• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package io.flutter.plugin.common;
2 
3 import java.lang.reflect.Array;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.HashMap;
9 import java.util.Map;
10 import org.json.JSONArray;
11 import org.json.JSONObject;
12 
13 public class JSONUtil {
JSONUtil()14     private JSONUtil() {
15     }
16 
17     /**
18      * Convert the Json java representation to Java objects. Particularly used for converting
19      * JSONArray and JSONObject to Lists and Maps.
20      */
unwrap(Object o)21     public static Object unwrap(Object o) {
22         if (JSONObject.NULL.equals(o) || o == null) {
23             return null;
24         }
25         if (o instanceof Boolean
26             || o instanceof Byte
27             || o instanceof Character
28             || o instanceof Double
29             || o instanceof Float
30             || o instanceof Integer
31             || o instanceof Long
32             || o instanceof Short
33             || o instanceof String) {
34             return o;
35         }
36         try {
37             if (o instanceof JSONArray) {
38                 List<Object> list = new ArrayList<>();
39                 JSONArray array = (JSONArray) o;
40                 for (int i = 0; i < array.length(); i++) {
41                     list.add(unwrap(array.get(i)));
42                 }
43                 return list;
44             }
45             if (o instanceof JSONObject) {
46                 Map<String, Object> map = new HashMap<>();
47                 JSONObject jsonObject = (JSONObject) o;
48                 Iterator<String> keyIterator = jsonObject.keys();
49                 while (keyIterator.hasNext()) {
50                     String key = keyIterator.next();
51                     map.put(key, unwrap(jsonObject.get(key)));
52                 }
53                 return map;
54             }
55         } catch (Exception ignored) {
56         }
57         return null;
58     }
59 
60     /**
61      * Backport of {@link JSONObject#wrap(Object)} for use on pre-KitKat
62      * systems.
63      */
wrap(Object o)64     public static Object wrap(Object o) {
65         if (o == null) {
66             return JSONObject.NULL;
67         }
68         if (o instanceof JSONArray || o instanceof JSONObject) {
69             return o;
70         }
71         if (o.equals(JSONObject.NULL)) {
72             return o;
73         }
74         try {
75             if (o instanceof Collection) {
76                 JSONArray result = new JSONArray();
77                 for (Object e : (Collection) o)
78                     result.put(wrap(e));
79                 return result;
80             } else if (o.getClass().isArray()) {
81                 JSONArray result = new JSONArray();
82                 int length = Array.getLength(o);
83                 for (int i = 0; i < length; i++)
84                     result.put(wrap(Array.get(o, i)));
85                 return result;
86             }
87             if (o instanceof Map) {
88                 JSONObject result = new JSONObject();
89                 for (Map.Entry<?, ?> entry: ((Map<?, ?>) o).entrySet())
90                     result.put((String) entry.getKey(), wrap(entry.getValue()));
91                 return result;
92             }
93             if (o instanceof Boolean ||
94                 o instanceof Byte ||
95                 o instanceof Character ||
96                 o instanceof Double ||
97                 o instanceof Float ||
98                 o instanceof Integer ||
99                 o instanceof Long ||
100                 o instanceof Short ||
101                 o instanceof String) {
102                 return o;
103             }
104             if (o.getClass().getPackage().getName().startsWith("java.")) {
105                 return o.toString();
106             }
107         } catch (Exception ignored) {
108         }
109         return null;
110     }
111 }
112