/** * Converts a readable map of numbers into a native map of numbers. * * @param readableMap * @return */ @Nullable private Map<String, Number> readableMapToNumberMap(ReadableMap readableMap) { Map<String, Number> map = new HashMap<>(); if (readableMap == null) { return null; } ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); if (!iterator.hasNextKey()) { return null; } while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType readableType = readableMap.getType(key); switch (readableType) { case Number: // Can be int or double. map.put(key, readableMap.getDouble(key)); break; default: // Do nothing and fail silently } } return map; }
/** * Converts a react native readable map into a JSON object. * * @param readableMap * @return */ @Nullable private JSONObject readableMapToJson(ReadableMap readableMap) { JSONObject jsonObject = new JSONObject(); if (readableMap == null) { return null; } ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); if (!iterator.hasNextKey()) { return null; } while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType readableType = readableMap.getType(key); try { switch (readableType) { case Null: jsonObject.put(key, null); break; case Boolean: jsonObject.put(key, readableMap.getBoolean(key)); break; case Number: // Can be int or double. jsonObject.put(key, readableMap.getDouble(key)); break; case String: jsonObject.put(key, readableMap.getString(key)); break; case Map: jsonObject.put(key, this.readableMapToJson(readableMap.getMap(key))); break; case Array: jsonObject.put(key, readableMap.getArray(key)); default: // Do nothing and fail silently } } catch (JSONException ex) { // Do nothing and fail silently } } return jsonObject; }