Repository navigation
Closed
Labels
Resolution: LockedThis issue was locked by the bot.This issue was locked by the bot.
Description
I was working on the android integration of react-native-intercom today and found that I had to forward an arbitrary ReadableMap to Intercom as a normal Map fully unboxed. I quickly realized there was no way to transform the ReadableMap into a usable object without recursively deconstructing it by hand.
Ideally, it'd be nice to have the ability to say unwrap() or getObject().
My current approach is to use the ReadableMapKeySetIterator and go through each node recursively
private Map<String, Object> recursivelyDeconstructReadableMap(ReadableMap readableMap) {
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
Map<String, Object> deconstructedMap = new HashMap<>();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = readableMap.getType(key);
switch (type) {
case Null:
deconstructedMap.put(key, null);
break;
case Boolean:
deconstructedMap.put(key, readableMap.getBoolean(key));
break;
case Number:
deconstructedMap.put(key, readableMap.getDouble(key));
break;
case String:
deconstructedMap.put(key, readableMap.getString(key));
break;
case Map:
deconstructedMap.put(key, recursivelyDeconstructReadableMap(readableMap.getMap(key)));
break;
case Array:
deconstructedMap.put(key, recursivelyDeconstructReadableArray(readableMap.getArray(key)));
break;
default:
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
}
}
return deconstructedMap;
}
private List<Object> recursivelyDeconstructReadableArray(ReadableArray readableArray) {
List<Object> deconstructedList = new ArrayList<>(readableArray.size());
for (int i = 0; i < readableArray.size(); i++) {
ReadableType indexType = readableArray.getType(i);
switch(indexType) {
case Null:
deconstructedList.add(i, null);
break;
case Boolean:
deconstructedList.add(i, readableArray.getBoolean(i));
break;
case Number:
deconstructedList.add(i, readableArray.getDouble(i));
break;
case String:
deconstructedList.add(i, readableArray.getString(i));
break;
case Map:
deconstructedList.add(i, recursivelyDeconstructReadableMap(readableArray.getMap(i)));
break;
case Array:
deconstructedList.add(i, recursivelyDeconstructReadableArray(readableArray.getArray(i)));
break;
default:
throw new IllegalArgumentException("Could not convert object at index " + i + ".");
}
}
return deconstructedList;
}This seems like a handy method that could exist on the ReadableMap and ReadableArray itself.
Reacted by Fadi Antoine Kfoury, Charlie Schwabacher, Evgheni Lisita, @odemolliens, Stanimir, Henning Hall and Arian Flores
Metadata
Metadata
Assignees
Labels
Resolution: LockedThis issue was locked by the bot.This issue was locked by the bot.
Activity
facebook-github-bot commentedon Dec 8, 2015
Hey ajwhite, thanks for reporting this issue!
React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.
react-nativeor for more real time interactions, ask on Discord in the #react-native channel.morenoh149 commentedon Dec 15, 2015
I would also find this useful. Honestly I can't even find docs on the
ReadableMaptype.morenoh149 commentedon Dec 15, 2015
Nvm found it's a custom interface. https://github.com/facebook/react-native/blob/a5c693dd53cd4b406a0de841cdb20dac104012e0/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableMap.java#L16
atticoos commentedon Dec 15, 2015
Yeah, takes a little looking around to locate it. Glad to see that there is some interest in this, I'll be getting to #4658 this week.
Add toArrayList and toHashMap methods for ReadableArray and ReadableM…
hey99xx commentedon Sep 23, 2016
The commit should have likely been on
ReadableMap, not onReadableNativeMap(same applies for the array equivalents).The way it's written means the user has to cast
ReadableMapintoReadableNativeMapbefore callingtoHashMap. Although this would normally be ok, for anyone using JavaOnlyMap in their unit tests to mock their native modules (like me) this is a huge annoyance.