Skip to content

Commit dc3836a

Browse files
atticoosFacebook Github Bot 6
authored andcommitted
Add toArrayList and toHashMap methods for ReadableArray and ReadableMap. Fixes #4655
Summary:Context #4658 I kept the original commit and author. cc mkonicek Closes facebook/react-native#6639 Differential Revision: D3126336 Pulled By: mkonicek fb-gh-sync-id: 5ae7b37f0eb1db355bb87076d621a405ff9c23c5 fbshipit-source-id: 5ae7b37f0eb1db355bb87076d621a405ff9c23c5
1 parent 31bb85a commit dc3836a

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.facebook.proguard.annotations.DoNotStrip;
1414
import com.facebook.soloader.SoLoader;
1515

16+
import java.util.ArrayList;
17+
1618
/**
1719
* Implementation of a NativeArray that allows read-only access to its members. This will generally
1820
* be constructed and filled in native code so you shouldn't construct one yourself.
@@ -46,4 +48,34 @@ protected ReadableNativeArray(HybridData hybridData) {
4648
public native ReadableNativeMap getMap(int index);
4749
@Override
4850
public native ReadableType getType(int index);
51+
52+
public ArrayList<Object> toArrayList() {
53+
ArrayList<Object> arrayList = new ArrayList<>();
54+
55+
for (int i = 0; i < this.size(); i++) {
56+
switch (getType(i)) {
57+
case Null:
58+
arrayList.add(null);
59+
break;
60+
case Boolean:
61+
arrayList.add(getBoolean(i));
62+
break;
63+
case Number:
64+
arrayList.add(getDouble(i));
65+
break;
66+
case String:
67+
arrayList.add(getString(i));
68+
break;
69+
case Map:
70+
arrayList.add(getMap(i).toHashMap());
71+
break;
72+
case Array:
73+
arrayList.add(getArray(i).toArrayList());
74+
break;
75+
default:
76+
throw new IllegalArgumentException("Could not convert object at index: " + i + ".");
77+
}
78+
}
79+
return arrayList;
80+
}
4981
}

ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import com.facebook.proguard.annotations.DoNotStrip;
1414
import com.facebook.soloader.SoLoader;
1515

16+
import java.util.HashMap;
17+
18+
1619
/**
1720
* Implementation of a read-only map in native memory. This will generally be constructed and filled
1821
* in native code so you shouldn't construct one yourself.
@@ -48,6 +51,35 @@ public ReadableMapKeySetIterator keySetIterator() {
4851
return new ReadableNativeMapKeySetIterator(this);
4952
}
5053

54+
public HashMap<String, Object>toHashMap() {
55+
ReadableMapKeySetIterator iterator = keySetIterator();
56+
HashMap<String, Object> hashMap = new HashMap<>();
57+
58+
while (iterator.hasNextKey()) {
59+
String key = iterator.nextKey();
60+
switch (getType(key)) {
61+
case Null:
62+
hashMap.put(key, null);
63+
break;
64+
case Boolean:
65+
hashMap.put(key, getBoolean(key));
66+
break;
67+
case Number:
68+
hashMap.put(key, getDouble(key));
69+
break;
70+
case Map:
71+
hashMap.put(key, getMap(key).toHashMap());
72+
break;
73+
case Array:
74+
hashMap.put(key, getArray(key).toArrayList());
75+
break;
76+
default:
77+
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
78+
}
79+
}
80+
return hashMap;
81+
}
82+
5183
/**
5284
* Implementation of a {@link ReadableNativeMap} iterator in native memory.
5385
*/

0 commit comments

Comments
 (0)