Skip to content

Instantly share code, notes, and snippets.

@tranquan
Forked from mfmendiola/ArrayUtil.java
Created February 18, 2020 12:05
Show Gist options
  • Save tranquan/51ef53257459805b70c6926742137301 to your computer and use it in GitHub Desktop.
Save tranquan/51ef53257459805b70c6926742137301 to your computer and use it in GitHub Desktop.
ReadableArray and ReadableMap serialization helpers for the React Native—Android bridge.
/*
ArrayUtil exposes a set of helper methods for working with
ReadableArray (by React Native), Object[], and JSONArray.
*/
package com.iodine.start;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableArray;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
public class ArrayUtil {
public static JSONArray toJSONArray(ReadableArray readableArray) throws JSONException {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < readableArray.size(); i++) {
ReadableType type = readableArray.getType(i);
switch (type) {
case Null:
jsonArray.put(i, null);
break;
case Boolean:
jsonArray.put(i, readableArray.getBoolean(i));
break;
case Number:
jsonArray.put(i, readableArray.getDouble(i));
break;
case String:
jsonArray.put(i, readableArray.getString(i));
break;
case Map:
jsonArray.put(i, MapUtil.toJSONObject(readableArray.getMap(i)));
break;
case Array:
jsonArray.put(i, ArrayUtil.toJSONArray(readableArray.getArray(i)));
break;
}
}
return jsonArray;
}
public static Object[] toArray(JSONArray jsonArray) throws JSONException {
Object[] array = new Object[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
Object value = jsonArray.get(i);
if (value instanceof JSONObject) {
value = MapUtil.toMap((JSONObject) value);
}
if (value instanceof JSONArray) {
value = ArrayUtil.toArray((JSONArray) value);
}
array[i] = value;
}
return array;
}
public static Object[] toArray(ReadableArray readableArray) {
Object[] array = new Object[readableArray.size()];
for (int i = 0; i < readableArray.size(); i++) {
ReadableType type = readableArray.getType(i);
switch (type) {
case Null:
array[i] = null;
break;
case Boolean:
array[i] = readableArray.getBoolean(i);
break;
case Number:
array[i] = readableArray.getDouble(i);
break;
case String:
array[i] = readableArray.getString(i);
break;
case Map:
array[i] = MapUtil.toMap(readableArray.getMap(i));
break;
case Array:
array[i] = ArrayUtil.toArray(readableArray.getArray(i));
break;
}
}
return array;
}
public static WritableArray toWritableArray(Object[] array) {
WritableArray writableArray = Arguments.createArray();
for (int i = 0; i < array.length; i++) {
Object value = array[i];
if (value == null) {
writableArray.pushNull();
}
if (value instanceof Boolean) {
writableArray.pushBoolean((Boolean) value);
}
if (value instanceof Double) {
writableArray.pushDouble((Double) value);
}
if (value instanceof Integer) {
writableArray.pushInt((Integer) value);
}
if (value instanceof String) {
writableArray.pushString((String) value);
}
if (value instanceof Map) {
writableArray.pushMap(MapUtil.toWritableMap((Map<String, Object>) value));
}
if (value.getClass().isArray()) {
writableArray.pushArray(ArrayUtil.toWritableArray((Object[]) value));
}
}
return writableArray;
}
}
/*
MapUtil exposes a set of helper methods for working with
ReadableMap (by React Native), Map<String, Object>, and JSONObject.
*/
package com.iodine.start;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableMap;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
public class MapUtil {
public static JSONObject toJSONObject(ReadableMap readableMap) throws JSONException {
JSONObject jsonObject = new JSONObject();
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = readableMap.getType(key);
switch (type) {
case Null:
jsonObject.put(key, null);
break;
case Boolean:
jsonObject.put(key, readableMap.getBoolean(key));
break;
case Number:
jsonObject.put(key, readableMap.getDouble(key));
break;
case String:
jsonObject.put(key, readableMap.getString(key));
break;
case Map:
jsonObject.put(key, MapUtil.toJSONObject(readableMap.getMap(key)));
break;
case Array:
jsonObject.put(key, ArrayUtil.toJSONArray(readableMap.getArray(key)));
break;
}
}
return jsonObject;
}
public static Map<String, Object> toMap(JSONObject jsonObject) throws JSONException {
Map<String, Object> map = new HashMap<>();
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
value = MapUtil.toMap((JSONObject) value);
}
if (value instanceof JSONArray) {
value = ArrayUtil.toArray((JSONArray) value);
}
map.put(key, value);
}
return map;
}
public static Map<String, Object> toMap(ReadableMap readableMap) {
Map<String, Object> map = new HashMap<>();
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = readableMap.getType(key);
switch (type) {
case Null:
map.put(key, null);
break;
case Boolean:
map.put(key, readableMap.getBoolean(key));
break;
case Number:
map.put(key, readableMap.getDouble(key));
break;
case String:
map.put(key, readableMap.getString(key));
break;
case Map:
map.put(key, MapUtil.toMap(readableMap.getMap(key)));
break;
case Array:
map.put(key, ArrayUtil.toArray(readableMap.getArray(key)));
break;
}
}
return map;
}
public static WritableMap toWritableMap(Map<String, Object> map) {
WritableMap writableMap = Arguments.createMap();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
Object value = pair.getValue();
if (value == null) {
writableMap.putNull((String) pair.getKey());
} else if (value instanceof Boolean) {
writableMap.putBoolean((String) pair.getKey(), (Boolean) value);
} else if (value instanceof Double) {
writableMap.putDouble((String) pair.getKey(), (Double) value);
} else if (value instanceof Integer) {
writableMap.putInt((String) pair.getKey(), (Integer) value);
} else if (value instanceof String) {
writableMap.putString((String) pair.getKey(), (String) value);
} else if (value instanceof Map) {
writableMap.putMap((String) pair.getKey(), MapUtil.toWritableMap((Map<String, Object>) value));
} else if (value.getClass() != null && value.getClass().isArray()) {
writableMap.putArray((String) pair.getKey(), ArrayUtil.toWritableArray((Object[]) value));
}
iterator.remove();
}
return writableMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment