Skip to content

Commit f80338f

Browse files
committed
[android][internals] rework internals utils to better support JSONObject/Array values
1 parent 631ff70 commit f80338f

File tree

1 file changed

+176
-54
lines changed
  • android/src/main/java/io/invertase/firebase

1 file changed

+176
-54
lines changed

android/src/main/java/io/invertase/firebase/Utils.java

Lines changed: 176 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
import com.facebook.react.bridge.ReactContext;
99
import com.facebook.react.bridge.ReadableArray;
1010
import com.facebook.react.bridge.ReadableMap;
11+
import com.facebook.react.bridge.WritableArray;
1112
import com.facebook.react.bridge.WritableMap;
1213
import com.facebook.react.common.LifecycleState;
1314
import com.facebook.react.modules.core.DeviceEventManagerModule;
1415

16+
import org.json.JSONArray;
17+
import org.json.JSONException;
18+
import org.json.JSONObject;
19+
1520
import java.text.SimpleDateFormat;
1621
import java.util.Calendar;
1722
import java.util.Date;
23+
import java.util.Iterator;
1824
import java.util.List;
1925
import java.util.Locale;
2026
import java.util.Map;
@@ -48,62 +54,178 @@ public static void sendEvent(final ReactContext context, final String eventName,
4854
}
4955
}
5056

51-
/**
52-
* Takes a value and calls the appropriate setter for its type on the target map + key
53-
*
54-
* @param key String key to set on target map
55-
* @param value Object value to set on target map
56-
* @param map WritableMap target map to write the value to
57-
*/
57+
public static WritableMap jsonObjectToWritableMap(JSONObject jsonObject) throws JSONException {
58+
Iterator<String> iterator = jsonObject.keys();
59+
WritableMap writableMap = Arguments.createMap();
60+
61+
while (iterator.hasNext()) {
62+
String key = iterator.next();
63+
Object value = jsonObject.get(key);
64+
if (value instanceof Float || value instanceof Double) {
65+
writableMap.putDouble(key, jsonObject.getDouble(key));
66+
} else if (value instanceof Number) {
67+
writableMap.putInt(key, jsonObject.getInt(key));
68+
} else if (value instanceof String) {
69+
writableMap.putString(key, jsonObject.getString(key));
70+
} else if (value instanceof JSONObject) {
71+
writableMap.putMap(key, jsonObjectToWritableMap(jsonObject.getJSONObject(key)));
72+
} else if (value instanceof JSONArray) {
73+
writableMap.putArray(key, jsonArrayToWritableArray(jsonObject.getJSONArray(key)));
74+
} else if (value == JSONObject.NULL) {
75+
writableMap.putNull(key);
76+
}
77+
}
78+
79+
return writableMap;
80+
}
81+
82+
public static WritableArray jsonArrayToWritableArray(JSONArray jsonArray) throws JSONException {
83+
WritableArray writableArray = Arguments.createArray();
84+
85+
for (int i = 0; i < jsonArray.length(); i++) {
86+
Object value = jsonArray.get(i);
87+
if (value instanceof Float || value instanceof Double) {
88+
writableArray.pushDouble(jsonArray.getDouble(i));
89+
} else if (value instanceof Number) {
90+
writableArray.pushInt(jsonArray.getInt(i));
91+
} else if (value instanceof String) {
92+
writableArray.pushString(jsonArray.getString(i));
93+
} else if (value instanceof JSONObject) {
94+
writableArray.pushMap(jsonObjectToWritableMap(jsonArray.getJSONObject(i)));
95+
} else if (value instanceof JSONArray) {
96+
writableArray.pushArray(jsonArrayToWritableArray(jsonArray.getJSONArray(i)));
97+
} else if (value == JSONObject.NULL) {
98+
writableArray.pushNull();
99+
}
100+
}
101+
return writableArray;
102+
}
103+
104+
public static WritableMap mapToWritableMap(Map<String, Object> value) {
105+
WritableMap writableMap = Arguments.createMap();
106+
107+
for (Map.Entry<String, Object> entry : value.entrySet()) {
108+
mapPutValue(entry.getKey(), entry.getValue(), writableMap);
109+
}
110+
111+
return writableMap;
112+
}
113+
114+
private static WritableArray listToWritableArray(List<Object> objects) {
115+
WritableArray writableArray = Arguments.createArray();
116+
for (Object object : objects) {
117+
arrayPushValue(object, writableArray);
118+
}
119+
return writableArray;
120+
}
121+
122+
@SuppressWarnings("unchecked")
123+
public static void arrayPushValue(@Nullable Object value, WritableArray array) {
124+
if (value == null || value == JSONObject.NULL) {
125+
array.pushNull();
126+
return;
127+
}
128+
129+
String type = value.getClass().getName();
130+
switch (type) {
131+
case "java.lang.Boolean":
132+
array.pushBoolean((Boolean) value);
133+
break;
134+
case "java.lang.Long":
135+
Long longVal = (Long) value;
136+
array.pushDouble((double) longVal);
137+
break;
138+
case "java.lang.Float":
139+
float floatVal = (float) value;
140+
array.pushDouble((double) floatVal);
141+
break;
142+
case "java.lang.Double":
143+
array.pushDouble((double) value);
144+
break;
145+
case "java.lang.Integer":
146+
array.pushInt((int) value);
147+
break;
148+
case "java.lang.String":
149+
array.pushString((String) value);
150+
break;
151+
case "org.json.JSONObject$1":
152+
try {
153+
array.pushMap(jsonObjectToWritableMap((JSONObject) value));
154+
} catch (JSONException e) {
155+
array.pushNull();
156+
}
157+
break;
158+
case "org.json.JSONArray$1":
159+
try {
160+
array.pushArray(jsonArrayToWritableArray((JSONArray) value));
161+
} catch (JSONException e) {
162+
array.pushNull();
163+
}
164+
break;
165+
default:
166+
if (List.class.isAssignableFrom(value.getClass())) {
167+
array.pushArray(listToWritableArray((List<Object>) value));
168+
} else if (Map.class.isAssignableFrom(value.getClass())) {
169+
array.pushMap(mapToWritableMap((Map<String, Object>) value));
170+
} else {
171+
Log.d(TAG, "utils:arrayPushValue:unknownType:" + type);
172+
array.pushNull();
173+
}
174+
}
175+
}
176+
58177
@SuppressWarnings("unchecked")
59178
public static void mapPutValue(String key, @Nullable Object value, WritableMap map) {
60-
if (value == null) {
179+
if (value == null || value == JSONObject.NULL) {
61180
map.putNull(key);
62-
} else {
63-
String type = value
64-
.getClass()
65-
.getName();
66-
switch (type) {
67-
case "java.lang.Boolean":
68-
map.putBoolean(key, (Boolean) value);
69-
break;
70-
case "java.lang.Long":
71-
Long longVal = (Long) value;
72-
map.putDouble(key, (double) longVal);
73-
break;
74-
case "java.lang.Float":
75-
float floatVal = (float) value;
76-
map.putDouble(key, (double) floatVal);
77-
break;
78-
case "java.lang.Double":
79-
map.putDouble(key, (Double) value);
80-
break;
81-
case "java.lang.Integer":
82-
map.putInt(key, (int) value);
83-
break;
84-
case "java.lang.String":
85-
map.putString(key, (String) value);
86-
break;
87-
case "org.json.JSONObject$1":
88-
map.putString(key, value.toString());
89-
break;
90-
default:
91-
if (List.class.isAssignableFrom(value.getClass())) {
92-
map.putArray(key, Arguments.makeNativeArray((List<Object>) value));
93-
} else if (Map.class.isAssignableFrom(value.getClass())) {
94-
WritableMap childMap = Arguments.createMap();
95-
Map<String, Object> valueMap = (Map<String, Object>) value;
96-
97-
for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
98-
mapPutValue(entry.getKey(), entry.getValue(), childMap);
99-
}
100-
101-
map.putMap(key, childMap);
102-
} else {
103-
Log.d(TAG, "utils:mapPutValue:unknownType:" + type);
104-
map.putNull(key);
105-
}
106-
}
181+
return;
182+
}
183+
184+
String type = value.getClass().getName();
185+
switch (type) {
186+
case "java.lang.Boolean":
187+
map.putBoolean(key, (Boolean) value);
188+
break;
189+
case "java.lang.Long":
190+
Long longVal = (Long) value;
191+
map.putDouble(key, (double) longVal);
192+
break;
193+
case "java.lang.Float":
194+
float floatVal = (float) value;
195+
map.putDouble(key, (double) floatVal);
196+
break;
197+
case "java.lang.Double":
198+
map.putDouble(key, (double) value);
199+
break;
200+
case "java.lang.Integer":
201+
map.putInt(key, (int) value);
202+
break;
203+
case "java.lang.String":
204+
map.putString(key, (String) value);
205+
break;
206+
case "org.json.JSONObject$1":
207+
try {
208+
map.putMap(key, jsonObjectToWritableMap((JSONObject) value));
209+
} catch (JSONException e) {
210+
map.putNull(key);
211+
}
212+
break;
213+
case "org.json.JSONArray$1":
214+
try {
215+
map.putArray(key, jsonArrayToWritableArray((JSONArray) value));
216+
} catch (JSONException e) {
217+
map.putNull(key);
218+
}
219+
break;
220+
default:
221+
if (List.class.isAssignableFrom(value.getClass())) {
222+
map.putArray(key, listToWritableArray((List<Object>) value));
223+
} else if (Map.class.isAssignableFrom(value.getClass())) {
224+
map.putMap(key, mapToWritableMap((Map<String, Object>) value));
225+
} else {
226+
Log.d(TAG, "utils:mapPutValue:unknownType:" + type);
227+
map.putNull(key);
228+
}
107229
}
108230
}
109231

@@ -164,7 +286,7 @@ public static boolean isAppInForeground(Context context) {
164286
if (
165287
appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
166288
&& appProcess.processName.equals(packageName)
167-
) {
289+
) {
168290
ReactContext reactContext;
169291

170292
try {

0 commit comments

Comments
 (0)