Skip to content

Ability to dynamically unwrap ReadableMap and ReadableArray #4655

Closed

Description

@atticoos

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.

Activity

  1. facebook-github-bot commented on Dec 8, 2015

    @facebook-github-bot
    Contributor

    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.

    • If this is a feature request or a bug that you would like to be fixed by the team, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
    • If you don't know how to do something or not sure whether some behavior is expected or a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
    • We welcome clear issues and PRs that are ready for in-depth discussion; thank you for your contributions!
  2. morenoh149 commented on Dec 15, 2015

    @morenoh149
    Contributor

    I would also find this useful. Honestly I can't even find docs on the ReadableMap type.

  3. atticoos commented on Dec 15, 2015

    @atticoos
    ContributorAuthor

    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.

  4. ghost closed this as completedin dc3836aon Apr 1, 2016
  5. added a commit that references this issue on Jun 18, 2016
    7489aba
  6. hey99xx commented on Sep 23, 2016

    @hey99xx

    The commit should have likely been on ReadableMap, not on ReadableNativeMap (same applies for the array equivalents).

    The way it's written means the user has to cast ReadableMap into ReadableNativeMap before calling toHashMap. 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.

  7. locked as resolved and limited conversation to collaborators on Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions