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