|
@@ -0,0 +1,298 @@
|
|
1
|
+package com.syan.agora;
|
|
2
|
+
|
|
3
|
+import android.support.annotation.Nullable;
|
|
4
|
+
|
|
5
|
+import com.facebook.react.bridge.Arguments;
|
|
6
|
+import com.facebook.react.bridge.ReadableArray;
|
|
7
|
+import com.facebook.react.bridge.ReadableMap;
|
|
8
|
+import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
9
|
+import com.facebook.react.bridge.ReadableType;
|
|
10
|
+import com.facebook.react.bridge.WritableArray;
|
|
11
|
+import com.facebook.react.bridge.WritableMap;
|
|
12
|
+import com.facebook.react.bridge.WritableNativeArray;
|
|
13
|
+import com.facebook.react.bridge.WritableNativeMap;
|
|
14
|
+
|
|
15
|
+import org.json.JSONArray;
|
|
16
|
+import org.json.JSONException;
|
|
17
|
+import org.json.JSONObject;
|
|
18
|
+
|
|
19
|
+import java.util.ArrayList;
|
|
20
|
+import java.util.HashMap;
|
|
21
|
+import java.util.Iterator;
|
|
22
|
+import java.util.List;
|
|
23
|
+import java.util.Map;
|
|
24
|
+
|
|
25
|
+public class ConvertUtils {
|
|
26
|
+ public static Map<String, Object> readableMapToMap(final @Nullable ReadableMap readableMap) {
|
|
27
|
+ if (readableMap == null) {
|
|
28
|
+ return new HashMap<>();
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ final ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
|
|
32
|
+ if (!iterator.hasNextKey()) {
|
|
33
|
+ return new HashMap<>();
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ final Map<String, Object> result = new HashMap<>();
|
|
37
|
+ while (iterator.hasNextKey()) {
|
|
38
|
+ final String key = iterator.nextKey();
|
|
39
|
+ result.put(key, toObject(readableMap, key));
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ return result;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ public static Object toObject(@Nullable ReadableMap readableMap, String key) {
|
|
46
|
+ if (readableMap == null) {
|
|
47
|
+ return null;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ Object result;
|
|
51
|
+
|
|
52
|
+ final ReadableType readableType = readableMap.getType(key);
|
|
53
|
+ switch (readableType) {
|
|
54
|
+ case Null:
|
|
55
|
+ result = key;
|
|
56
|
+ break;
|
|
57
|
+ case Boolean:
|
|
58
|
+ result = readableMap.getBoolean(key);
|
|
59
|
+ break;
|
|
60
|
+ case Number:
|
|
61
|
+ // Can be int or double.
|
|
62
|
+ double tmp = readableMap.getDouble(key);
|
|
63
|
+ if (tmp == (int) tmp) {
|
|
64
|
+ result = (int) tmp;
|
|
65
|
+ } else {
|
|
66
|
+ result = tmp;
|
|
67
|
+ }
|
|
68
|
+ break;
|
|
69
|
+ case String:
|
|
70
|
+ result = readableMap.getString(key);
|
|
71
|
+ break;
|
|
72
|
+ case Map:
|
|
73
|
+ result = readableMapToMap(readableMap.getMap(key));
|
|
74
|
+ break;
|
|
75
|
+ case Array:
|
|
76
|
+ result = readableArrayToList(readableMap.getArray(key));
|
|
77
|
+ break;
|
|
78
|
+ default:
|
|
79
|
+ throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ return result;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ /**
|
|
86
|
+ * toList converts a {@link ReadableArray} into an ArrayList.
|
|
87
|
+ *
|
|
88
|
+ * @param readableArray The ReadableArray to be conveted.
|
|
89
|
+ * @return An ArrayList containing the data that was in the ReadableArray.
|
|
90
|
+ */
|
|
91
|
+ public static List<Object> readableArrayToList(final @Nullable ReadableArray readableArray) {
|
|
92
|
+ if (readableArray == null) {
|
|
93
|
+ return null;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ List<Object> result = new ArrayList<>(readableArray.size());
|
|
97
|
+ for (int index = 0; index < readableArray.size(); index++) {
|
|
98
|
+ final ReadableType readableType = readableArray.getType(index);
|
|
99
|
+ switch (readableType) {
|
|
100
|
+ case Null:
|
|
101
|
+ result.add(String.valueOf(index));
|
|
102
|
+ break;
|
|
103
|
+ case Boolean:
|
|
104
|
+ result.add(readableArray.getBoolean(index));
|
|
105
|
+ break;
|
|
106
|
+ case Number:
|
|
107
|
+ // Can be int or double.
|
|
108
|
+ double tmp = readableArray.getDouble(index);
|
|
109
|
+ if (tmp == (int) tmp) {
|
|
110
|
+ result.add((int) tmp);
|
|
111
|
+ } else {
|
|
112
|
+ result.add(tmp);
|
|
113
|
+ }
|
|
114
|
+ break;
|
|
115
|
+ case String:
|
|
116
|
+ result.add(readableArray.getString(index));
|
|
117
|
+ break;
|
|
118
|
+ case Map:
|
|
119
|
+ result.add(readableMapToMap(readableArray.getMap(index)));
|
|
120
|
+ break;
|
|
121
|
+ case Array:
|
|
122
|
+ result = readableArrayToList(readableArray.getArray(index));
|
|
123
|
+ break;
|
|
124
|
+ default:
|
|
125
|
+ throw new IllegalArgumentException("Could not convert object with index: " + index + ".");
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ return result;
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ /**
|
|
133
|
+ * better
|
|
134
|
+ *
|
|
135
|
+ * @param jsonArray
|
|
136
|
+ * @return
|
|
137
|
+ * @throws JSONException
|
|
138
|
+ */
|
|
139
|
+ public static WritableArray jsonToReact(final JSONArray jsonArray) throws JSONException {
|
|
140
|
+ final WritableArray writableArray = Arguments.createArray();
|
|
141
|
+ for (int i = 0; i < jsonArray.length(); i++) {
|
|
142
|
+ final Object value = jsonArray.get(i);
|
|
143
|
+ if (value instanceof Float || value instanceof Double) {
|
|
144
|
+ writableArray.pushDouble(jsonArray.getDouble(i));
|
|
145
|
+ } else if (value instanceof Number) {
|
|
146
|
+ writableArray.pushInt(jsonArray.getInt(i));
|
|
147
|
+ } else if (value instanceof String) {
|
|
148
|
+ writableArray.pushString(jsonArray.getString(i));
|
|
149
|
+ } else if (value instanceof Boolean) {
|
|
150
|
+ writableArray.pushBoolean(jsonArray.getBoolean(i));
|
|
151
|
+ } else if (value instanceof JSONObject) {
|
|
152
|
+ writableArray.pushMap(jsonToReact(jsonArray.getJSONObject(i)));
|
|
153
|
+ } else if (value instanceof JSONArray) {
|
|
154
|
+ writableArray.pushArray(jsonToReact(jsonArray.getJSONArray(i)));
|
|
155
|
+ } else if (value == JSONObject.NULL) {
|
|
156
|
+ writableArray.pushNull();
|
|
157
|
+ }
|
|
158
|
+ }
|
|
159
|
+ return writableArray;
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ /**
|
|
163
|
+ * better
|
|
164
|
+ *
|
|
165
|
+ * @param jsonObject
|
|
166
|
+ * @return
|
|
167
|
+ * @throws JSONException
|
|
168
|
+ */
|
|
169
|
+ public static WritableMap jsonToReact(final JSONObject jsonObject) throws JSONException {
|
|
170
|
+ final WritableMap writableMap = Arguments.createMap();
|
|
171
|
+ final Iterator iterator = jsonObject.keys();
|
|
172
|
+ while (iterator.hasNext()) {
|
|
173
|
+ final String key = (String) iterator.next();
|
|
174
|
+ final Object value = jsonObject.get(key);
|
|
175
|
+ if (value instanceof Float || value instanceof Double) {
|
|
176
|
+ writableMap.putDouble(key, jsonObject.getDouble(key));
|
|
177
|
+ } else if (value instanceof Number) {
|
|
178
|
+ writableMap.putInt(key, jsonObject.getInt(key));
|
|
179
|
+ } else if (value instanceof String) {
|
|
180
|
+ writableMap.putString(key, jsonObject.getString(key));
|
|
181
|
+ } else if (value instanceof JSONObject) {
|
|
182
|
+ writableMap.putMap(key, jsonToReact(jsonObject.getJSONObject(key)));
|
|
183
|
+ } else if (value instanceof JSONArray) {
|
|
184
|
+ writableMap.putArray(key, jsonToReact(jsonObject.getJSONArray(key)));
|
|
185
|
+ } else if (value instanceof Boolean) {
|
|
186
|
+ writableMap.putBoolean(key, jsonObject.getBoolean(key));
|
|
187
|
+ } else if (value == JSONObject.NULL) {
|
|
188
|
+ writableMap.putNull(key);
|
|
189
|
+ }
|
|
190
|
+ }
|
|
191
|
+ return writableMap;
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ public static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
|
|
195
|
+ WritableMap map = new WritableNativeMap();
|
|
196
|
+
|
|
197
|
+ Iterator<String> iterator = jsonObject.keys();
|
|
198
|
+ while (iterator.hasNext()) {
|
|
199
|
+ String key = iterator.next();
|
|
200
|
+ Object value = jsonObject.get(key);
|
|
201
|
+ if (value instanceof JSONObject) {
|
|
202
|
+ map.putMap(key, convertJsonToMap((JSONObject) value));
|
|
203
|
+ } else if (value instanceof JSONArray) {
|
|
204
|
+ map.putArray(key, convertJsonToArray((JSONArray) value));
|
|
205
|
+ } else if (value instanceof Boolean) {
|
|
206
|
+ map.putBoolean(key, (Boolean) value);
|
|
207
|
+ } else if (value instanceof Integer) {
|
|
208
|
+ map.putInt(key, (Integer) value);
|
|
209
|
+ } else if (value instanceof Double) {
|
|
210
|
+ map.putDouble(key, (Double) value);
|
|
211
|
+ } else if (value instanceof String) {
|
|
212
|
+ map.putString(key, (String) value);
|
|
213
|
+ } else {
|
|
214
|
+ map.putString(key, value.toString());
|
|
215
|
+ }
|
|
216
|
+ }
|
|
217
|
+ return map;
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ public static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException {
|
|
221
|
+ WritableArray array = new WritableNativeArray();
|
|
222
|
+
|
|
223
|
+ for (int i = 0; i < jsonArray.length(); i++) {
|
|
224
|
+ Object value = jsonArray.get(i);
|
|
225
|
+ if (value instanceof JSONObject) {
|
|
226
|
+ array.pushMap(convertJsonToMap((JSONObject) value));
|
|
227
|
+ } else if (value instanceof JSONArray) {
|
|
228
|
+ array.pushArray(convertJsonToArray((JSONArray) value));
|
|
229
|
+ } else if (value instanceof Boolean) {
|
|
230
|
+ array.pushBoolean((Boolean) value);
|
|
231
|
+ } else if (value instanceof Integer) {
|
|
232
|
+ array.pushInt((Integer) value);
|
|
233
|
+ } else if (value instanceof Double) {
|
|
234
|
+ array.pushDouble((Double) value);
|
|
235
|
+ } else if (value instanceof String) {
|
|
236
|
+ array.pushString((String) value);
|
|
237
|
+ } else {
|
|
238
|
+ array.pushString(value.toString());
|
|
239
|
+ }
|
|
240
|
+ }
|
|
241
|
+ return array;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ public static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException {
|
|
245
|
+ JSONObject object = new JSONObject();
|
|
246
|
+ ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
|
|
247
|
+ while (iterator.hasNextKey()) {
|
|
248
|
+ String key = iterator.nextKey();
|
|
249
|
+ switch (readableMap.getType(key)) {
|
|
250
|
+ case Null:
|
|
251
|
+ object.put(key, JSONObject.NULL);
|
|
252
|
+ break;
|
|
253
|
+ case Boolean:
|
|
254
|
+ object.put(key, readableMap.getBoolean(key));
|
|
255
|
+ break;
|
|
256
|
+ case Number:
|
|
257
|
+ object.put(key, readableMap.getDouble(key));
|
|
258
|
+ break;
|
|
259
|
+ case String:
|
|
260
|
+ object.put(key, readableMap.getString(key));
|
|
261
|
+ break;
|
|
262
|
+ case Map:
|
|
263
|
+ object.put(key, convertMapToJson(readableMap.getMap(key)));
|
|
264
|
+ break;
|
|
265
|
+ case Array:
|
|
266
|
+ object.put(key, convertArrayToJson(readableMap.getArray(key)));
|
|
267
|
+ break;
|
|
268
|
+ }
|
|
269
|
+ }
|
|
270
|
+ return object;
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ public static JSONArray convertArrayToJson(ReadableArray readableArray) throws JSONException {
|
|
274
|
+ JSONArray array = new JSONArray();
|
|
275
|
+ for (int i = 0; i < readableArray.size(); i++) {
|
|
276
|
+ switch (readableArray.getType(i)) {
|
|
277
|
+ case Null:
|
|
278
|
+ break;
|
|
279
|
+ case Boolean:
|
|
280
|
+ array.put(readableArray.getBoolean(i));
|
|
281
|
+ break;
|
|
282
|
+ case Number:
|
|
283
|
+ array.put(readableArray.getDouble(i));
|
|
284
|
+ break;
|
|
285
|
+ case String:
|
|
286
|
+ array.put(readableArray.getString(i));
|
|
287
|
+ break;
|
|
288
|
+ case Map:
|
|
289
|
+ array.put(convertMapToJson(readableArray.getMap(i)));
|
|
290
|
+ break;
|
|
291
|
+ case Array:
|
|
292
|
+ array.put(convertArrayToJson(readableArray.getArray(i)));
|
|
293
|
+ break;
|
|
294
|
+ }
|
|
295
|
+ }
|
|
296
|
+ return array;
|
|
297
|
+ }
|
|
298
|
+}
|