Java Code Examples for com.facebook.react.bridge.ReadableMap#getType()

The following examples show how to use com.facebook.react.bridge.ReadableMap#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactNativeSmooch.java    From react-native-smooch with MIT License 6 votes vote down vote up
private Map<String, Object> getUserProperties(ReadableMap properties) {
    ReadableMapKeySetIterator iterator = properties.keySetIterator();
    Map<String, Object> userProperties = new HashMap<>();

    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType type = properties.getType(key);
        if (type == ReadableType.Boolean) {
            userProperties.put(key, properties.getBoolean(key));
        } else if (type == ReadableType.Number) {
            userProperties.put(key, properties.getDouble(key));
        } else if (type == ReadableType.String) {
            userProperties.put(key, properties.getString(key));
        }
    }

    return userProperties;
}
 
Example 2
Source File: RCTConvert.java    From react-native-twilio-chat with MIT License 6 votes vote down vote up
public static HashMap<String, Object> readableMapToHashMap(ReadableMap readableMap) {
    if (readableMap == null) {
        return null;
    }
    HashMap map = new HashMap<String, Object>();
    ReadableMapKeySetIterator keySetIterator = readableMap.keySetIterator();
    while (keySetIterator.hasNextKey()) {
        String key = keySetIterator.nextKey();
        ReadableType type = readableMap.getType(key);
        switch(type) {
            case String:
                map.put(key, readableMap.getString(key));
                break;
            case Map:
                HashMap<String, Object> attributes = new RCTConvert().readableMapToHashMap(readableMap.getMap(key));
                map.put(key, attributes);
                break;
            default:
                // do nothing
        }
    }
    return map;
}
 
Example 3
Source File: OAuthManagerProviders.java    From react-native-oauth with MIT License 6 votes vote down vote up
static private OAuthRequest addParametersToRequest(
  OAuthRequest request,
  final String access_token,
  @Nullable final ReadableMap params
) {
  if (params != null && params.hasKey("params")) {
    ReadableMapKeySetIterator iterator = params.keySetIterator();
    while (iterator.hasNextKey()) {
      String key = iterator.nextKey();
      ReadableType readableType = params.getType(key);
      switch(readableType) {
        case String:
          String val = params.getString(key);
          // String escapedVal = Uri.encode(val);
          if (val.equals("access_token")) {
            val = access_token;
          }
          request.addParameter(key, val);
          break;
        default:
          throw new IllegalArgumentException("Could not read object with key: " + key);
      }
    }
  }
  return request;
}
 
Example 4
Source File: DefaultNavigationImplementation.java    From native-navigation with MIT License 5 votes vote down vote up
private static boolean mapEqual(
    ReadableMap a,
    ReadableMap b
) {
  ReadableMapKeySetIterator iterator = b.keySetIterator();
  while (iterator.hasNextKey()) {
    String key = iterator.nextKey();
    if (!a.hasKey(key)) return false;
    ReadableType type = b.getType(key);
    if (type != a.getType(key)) return false;
    switch (type) {
      case Null:
        break;
      case Boolean:
        if (a.getBoolean(key) != b.getBoolean(key)) return false;
        break;
      case Number:
        if (a.getDouble(key) != b.getDouble(key)) return false;
        break;
      case String:
        if (!a.getString(key).equals(b.getString(key))) return false;
        break;
      case Map:
        if (!mapEqual(a.getMap(key), b.getMap(key))) return false;
        break;
      case Array:
        if (!arrayEqual(a.getArray(key), b.getArray(key))) return false;
        break;
      default:
        Log.e(TAG, "Could not convert object with key: " + key + ".");
    }
  }
  return true;
}
 
Example 5
Source File: OAuthManagerModule.java    From react-native-oauth with MIT License 5 votes vote down vote up
public static Map<String, Object> recursivelyDeconstructReadableMap(ReadableMap readableMap) {
  Map<String, Object> deconstructedMap = new HashMap<>();
  if (readableMap == null) {
    return deconstructedMap;
  }

  ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
  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, OAuthManagerModule.recursivelyDeconstructReadableMap(readableMap.getMap(key)));
        break;
      case Array:
        deconstructedMap.put(key, OAuthManagerModule.recursivelyDeconstructReadableArray(readableMap.getArray(key)));
        break;
      default:
        throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
    }

  }
  return deconstructedMap;
}
 
Example 6
Source File: SQLitePluginConverter.java    From react-native-sqlite-storage with MIT License 5 votes vote down vote up
/**
 * Returns the value at {@code key} if it exists, coercing it if
 * necessary.
 */
static String getString(ReadableMap map, String key, String defaultValue) {
    if (map == null){
        return defaultValue;
    }
    try {
        ReadableType type = map.getType(key);
        switch (type) {
            case Number:
                double value = map.getDouble(key);
                if (value == (long) value) {
                    return String.valueOf((long) value);
                } else {
                    return String.valueOf(value);
                }
            case Boolean:
                return String.valueOf(map.getBoolean(key));
            case String:
                return map.getString(key);
            case Null:
                return null;
            default:
                return defaultValue;
        }
    } catch(NoSuchKeyException ex){
        return defaultValue;
    }
}
 
Example 7
Source File: RNMailComposeModule.java    From react-native-mail-compose with MIT License 5 votes vote down vote up
private byte[] getBlobFromUri(ReadableMap map, String key) {
    if (map.hasKey(key) && map.getType(key) == ReadableType.String) {
        String uri = map.getString(key);
        if (uri != null && !uri.isEmpty()) {
            return byteArrayFromUrl(uri);
        }
    }
    return null;
}
 
Example 8
Source File: ConversionUtil.java    From react-native-eval with MIT License 5 votes vote down vote up
/**
 * toObject extracts a value from a {@link ReadableMap} by its key,
 * and returns a POJO representing that object.
 *
 * @param readableMap The Map to containing the value to be converted
 * @param key The key for the value to be converted
 * @return The converted POJO
 */
public static Object toObject(@Nullable ReadableMap readableMap, String key) {
    if (readableMap == null) {
        return null;
    }

    Object result;

    ReadableType readableType = readableMap.getType(key);
    switch (readableType) {
        case Null:
            result = null;
            break;
        case Boolean:
            result = readableMap.getBoolean(key);
            break;
        case Number:
            // Can be int or double.
            double tmp = readableMap.getDouble(key);
            if (tmp == (int) tmp) {
                result = (int) tmp;
            } else {
                result = tmp;
            }
            break;
        case String:
            result = readableMap.getString(key);
            break;
        case Map:
            result = toMap(readableMap.getMap(key));
            break;
        case Array:
            result = toList(readableMap.getArray(key));
            break;
        default:
            throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
    }

    return result;
}
 
Example 9
Source File: Utils.java    From react-native-update with MIT License 5 votes vote down vote up
public static JSONObject convertReadableToJsonObject(ReadableMap map) {
    JSONObject jsonObj = new JSONObject();
    ReadableMapKeySetIterator it = map.keySetIterator();
    while (it.hasNextKey()) {
        String key = it.nextKey();
        ReadableType type = map.getType(key);
        try {
            switch (type) {
                case Map:
                    jsonObj.put(key, convertReadableToJsonObject(map.getMap(key)));
                    break;
                case Array:
                    jsonObj.put(key, convertReadableToJsonArray(map.getArray(key)));
                    break;
                case String:
                    jsonObj.put(key, map.getString(key));
                    break;
                case Number:
                    jsonObj.put(key, map.getDouble(key));
                    break;
                case Boolean:
                    jsonObj.put(key, map.getBoolean(key));
                    break;
                case Null:
                    jsonObj.put(key, null);
                    break;
                default:
                    throw new RuntimeException("Unrecognized type: " + type + " of key: " + key);
            }
        } catch (JSONException jsonException) {
            throw new RuntimeException("Error setting key: " + key + " in JSONObject", jsonException);
        }
    }

    return jsonObj;
}
 
Example 10
Source File: MapUtil.java    From Instabug-React-Native with MIT License 5 votes vote down vote up
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;
}
 
Example 11
Source File: JSStackTrace.java    From react-native-GPay with MIT License 5 votes vote down vote up
private static String stackFrameToModuleId(ReadableMap frame) {
  if (frame.hasKey("file") &&
      !frame.isNull("file") &&
      frame.getType("file") == ReadableType.String) {
    final Matcher matcher = mJsModuleIdPattern.matcher(frame.getString("file"));
    if (matcher.find()) {
      return matcher.group(1) + ":";
    }
  }
  return "";
}
 
Example 12
Source File: MapUtil.java    From react-native-background-geolocation with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: JsonConvert.java    From react-native-cordova with MIT License 5 votes vote down vote up
public static JSONObject reactToJSON(ReadableMap readableMap) throws JSONException {
    JSONObject jsonObject = new JSONObject();
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    while(iterator.hasNextKey()){
        String key = iterator.nextKey();
        ReadableType valueType = readableMap.getType(key);
        switch (valueType){
            case Null:
                jsonObject.put(key,JSONObject.NULL);
                break;
            case Boolean:
                jsonObject.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                try {
                    jsonObject.put(key, readableMap.getInt(key));
                } catch(Exception e) {
                    jsonObject.put(key, readableMap.getDouble(key));
                }
                break;
            case String:
                jsonObject.put(key, readableMap.getString(key));
                break;
            case Map:
                jsonObject.put(key, reactToJSON(readableMap.getMap(key)));
                break;
            case Array:
                jsonObject.put(key, reactToJSON(readableMap.getArray(key)));
                break;
        }
    }

    return jsonObject;
}
 
Example 14
Source File: JSONParser.java    From react-native-navigation with MIT License 5 votes vote down vote up
public JSONObject parse(ReadableMap map) {
    try {
        ReadableMapKeySetIterator it = map.keySetIterator();
        JSONObject result = new JSONObject();
        while (it.hasNextKey()) {
            String key = it.nextKey();
            switch (map.getType(key)) {
                case String:
                    result.put(key, map.getString(key));
                    break;
                case Number:
                    result.put(key, parseNumber(map, key));
                    break;
                case Boolean:
                    result.put(key, map.getBoolean(key));
                    break;
                case Array:
                    result.put(key, parse(map.getArray(key)));
                    break;
                case Map:
                    result.put(key, parse(map.getMap(key)));
                    break;
                default:
                    break;
            }
        }
        return result;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
 
Example 15
Source File: PjActions.java    From react-native-sip with GNU General Public License v3.0 5 votes vote down vote up
private static void formatIntent(Intent intent, ReadableMap configuration) {
    if (configuration == null) {
        return;
    }

    ReadableMapKeySetIterator it = configuration.keySetIterator();
    while (it.hasNextKey()) {
        String key = it.nextKey();

        switch (configuration.getType(key)) {
            case Null:
                intent.putExtra(key, (String) null);
                break;
            case String:
                intent.putExtra(key, configuration.getString(key));
                break;
            case Number:
                intent.putExtra(key, configuration.getInt(key));
                break;
            case Boolean:
                intent.putExtra(key, configuration.getBoolean(key));
                break;
            case Map:
                intent.putExtra(key, (Serializable) formatMap(configuration.getMap(key)));
                break;
            default:
                Log.w(TAG, "Unable to put extra information for intent: unknown type \""+ configuration.getType(key) +"\"");
                break;
        }
    }
}
 
Example 16
Source File: RNMessageComposeModule.java    From react-native-message-compose with MIT License 4 votes vote down vote up
private String getString(ReadableMap map, String key) {
    if (map.hasKey(key) && map.getType(key) == ReadableType.String) {
        return map.getString(key);
    }
    return null;
}
 
Example 17
Source File: Jsons.java    From pili-react-native with MIT License 4 votes vote down vote up
public static 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.getInt(key));
                    break;
                case String:
                    jsonObject.put(key, readableMap.getString(key));
                    break;
                case Map:
                    jsonObject.put(key, 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;
}
 
Example 18
Source File: RCTConvert.java    From react-native-twilio-chat with MIT License 4 votes vote down vote up
public static 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.getInt(key));
                    break;
                case String:
                    jsonObject.put(key, readableMap.getString(key));
                    break;
                case Map:
                    jsonObject.put(key, 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;
}
 
Example 19
Source File: MusicControlModule.java    From react-native-music-control with MIT License 4 votes vote down vote up
@ReactMethod
synchronized public void setNowPlaying(ReadableMap metadata) {
    init();
    if(artworkThread != null && artworkThread.isAlive()) artworkThread.interrupt();

    String title = metadata.hasKey("title") ? metadata.getString("title") : null;
    String artist = metadata.hasKey("artist") ? metadata.getString("artist") : null;
    String album = metadata.hasKey("album") ? metadata.getString("album") : null;
    String genre = metadata.hasKey("genre") ? metadata.getString("genre") : null;
    String description = metadata.hasKey("description") ? metadata.getString("description") : null;
    String date = metadata.hasKey("date") ? metadata.getString("date") : null;
    long duration = metadata.hasKey("duration") ? (long)(metadata.getDouble("duration") * 1000) : 0;
    int notificationColor = metadata.hasKey("color") ? metadata.getInt("color") : NotificationCompat.COLOR_DEFAULT;
    String notificationIcon = metadata.hasKey("notificationIcon") ? metadata.getString("notificationIcon") : null;

    // If a color is supplied, we need to clear the MediaStyle set during init().
    // Otherwise, the color will not be used for the notification's background.
    boolean removeFade = metadata.hasKey("color");
    if(removeFade) {
        nb.setStyle(new MediaStyle());
    }

    RatingCompat rating;
    if(metadata.hasKey("rating")) {
        if(ratingType == RatingCompat.RATING_PERCENTAGE) {
            rating = RatingCompat.newPercentageRating((float)metadata.getDouble("rating"));
        } else if(ratingType == RatingCompat.RATING_HEART) {
            rating = RatingCompat.newHeartRating(metadata.getBoolean("rating"));
        } else if(ratingType == RatingCompat.RATING_THUMB_UP_DOWN) {
            rating = RatingCompat.newThumbRating(metadata.getBoolean("rating"));
        } else {
            rating = RatingCompat.newStarRating(ratingType, (float)metadata.getDouble("rating"));
        }
    } else {
        rating = RatingCompat.newUnratedRating(ratingType);
    }

    md.putText(MediaMetadataCompat.METADATA_KEY_TITLE, title);
    md.putText(MediaMetadataCompat.METADATA_KEY_ARTIST, artist);
    md.putText(MediaMetadataCompat.METADATA_KEY_ALBUM, album);
    md.putText(MediaMetadataCompat.METADATA_KEY_GENRE, genre);
    md.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, description);
    md.putText(MediaMetadataCompat.METADATA_KEY_DATE, date);
    md.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
    if (android.os.Build.VERSION.SDK_INT > 19) {
        md.putRating(MediaMetadataCompat.METADATA_KEY_RATING, rating);
    }

    nb.setContentTitle(title);
    nb.setContentText(artist);
    nb.setContentInfo(album);
    nb.setColor(notificationColor);

    notification.setCustomNotificationIcon(notificationIcon);

    if(metadata.hasKey("artwork")) {
        String artwork = null;
        boolean localArtwork = false;

        if(metadata.getType("artwork") == ReadableType.Map) {
            artwork = metadata.getMap("artwork").getString("uri");
            localArtwork = true;
        } else {
            artwork = metadata.getString("artwork");
        }

        final String artworkUrl = artwork;
        final boolean artworkLocal = localArtwork;

        artworkThread = new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = loadArtwork(artworkUrl, artworkLocal);

                if(md != null) {
                    md.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap);
                    session.setMetadata(md.build());
                }
                if(nb != null) {
                    nb.setLargeIcon(bitmap);
                    notification.show(nb, isPlaying);
                }

                artworkThread = null;
            }
        });
        artworkThread.start();
    } else {
        md.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, null);
        nb.setLargeIcon(null);
    }

    session.setMetadata(md.build());
    session.setActive(true);
    notification.show(nb, isPlaying);
}
 
Example 20
Source File: RNMailComposeModule.java    From react-native-mail-compose with MIT License 4 votes vote down vote up
private ReadableArray getArray(ReadableMap map, String key) {
    if (map.hasKey(key) && map.getType(key) == ReadableType.Array) {
        return map.getArray(key);
    }
    return null;
}