com.facebook.react.bridge.ReadableType Java Examples

The following examples show how to use com.facebook.react.bridge.ReadableType. 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: SQLitePlugin.java    From react-native-sqlite-storage with MIT License 6 votes vote down vote up
private void bindArgsToStatement(SQLiteStatement myStatement, ReadableArray sqlArgs) {
    for (int i = 0; i < sqlArgs.size(); i++) {
        ReadableType type = sqlArgs.getType(i);
        if (type == ReadableType.Number){
            double tmp = sqlArgs.getDouble(i);
            if (tmp == (long) tmp) {
                myStatement.bindLong(i + 1, (long) tmp);
            } else {
                myStatement.bindDouble(i + 1, tmp);
            }
        } else if (sqlArgs.isNull(i)) {
            myStatement.bindNull(i + 1);
        } else {
            myStatement.bindString(i + 1, SQLitePluginConverter.getString(sqlArgs,i,""));
        }
    }
}
 
Example #2
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 #3
Source File: BarLineChartBaseManager.java    From react-native-mp-android-chart with MIT License 6 votes vote down vote up
@ReactProp(name = "zoom")
public void setZoom(BarLineChartBase chart, ReadableMap propMap) {
    if (BridgeUtils.validate(propMap, ReadableType.Number, "scaleX") &&
        BridgeUtils.validate(propMap, ReadableType.Number, "scaleY") &&
        BridgeUtils.validate(propMap, ReadableType.Number, "xValue") &&
        BridgeUtils.validate(propMap, ReadableType.Number, "yValue")) {

        YAxis.AxisDependency axisDependency = YAxis.AxisDependency.LEFT;
        if (propMap.hasKey("axisDependency") &&
                propMap.getString("axisDependency").equalsIgnoreCase("RIGHT")) {
            axisDependency = YAxis.AxisDependency.RIGHT;
        }

        chart.zoom(
                (float) propMap.getDouble("scaleX"),
                (float) propMap.getDouble("scaleY"),
                (float) propMap.getDouble("xValue"),
                (float) propMap.getDouble("yValue"),
                axisDependency
        );
    }
}
 
Example #4
Source File: ReactDrawerLayoutManager_1d0b39_t.java    From coming with MIT License 6 votes vote down vote up
@ReactProp(name = "drawerPosition")
public void setDrawerPosition(ReactDrawerLayout view, Dynamic drawerPosition) {
  if (drawerPosition.isNull()) {
    view.setDrawerPosition(Gravity.START);
  } else if (drawerPosition.getType() == ReadableType.Number) {
    final int drawerPositionNum = drawerPosition.asInt();

    if (Gravity.START == drawerPositionNum || Gravity.END == drawerPositionNum) {
      view.setDrawerPosition(drawerPositionNum);
    } else {
      throw new JSApplicationIllegalArgumentException("Unknown drawerPosition " + drawerPositionNum);
    }
  } else if (drawerPosition.getType() == ReadableType.String) {
    final String drawerPositionStr = drawerPosition.asString();

    if (drawerPositionStr.equals("left")) {
      view.setDrawerPosition(Gravity.START);
    } else if (drawerPositionStr.equals("right")) {
      view.setDrawerPosition(Gravity.END);
    } else {
      throw new JSApplicationIllegalArgumentException("drawerPosition must be 'left' or 'right', received" + drawerPositionStr);
    }
  } else {
    throw new JSApplicationIllegalArgumentException("drawerPosition must be a string or int");
  }
}
 
Example #5
Source File: BarChartManager.java    From react-native-mp-android-chart with MIT License 6 votes vote down vote up
@Override
void dataSetConfig(IDataSet<BarEntry> dataSet, ReadableMap config) {
    BarDataSet barDataSet = (BarDataSet) dataSet;

    ChartDataSetConfigUtils.commonConfig(barDataSet, config);
    ChartDataSetConfigUtils.commonBarLineScatterCandleBubbleConfig(barDataSet, config);

    if (BridgeUtils.validate(config, ReadableType.Number, "barSpacePercent")) {
        barDataSet.setBarSpacePercent((float) config.getDouble("barSpacePercent"));
    }
    if (BridgeUtils.validate(config, ReadableType.String, "barShadowColor")) {
        barDataSet.setBarShadowColor(Color.parseColor(config.getString("barShadowColor")));
    }
    if (BridgeUtils.validate(config, ReadableType.Number, "highlightAlpha")) {
        barDataSet.setHighLightAlpha(config.getInt("highlightAlpha"));
    }
    if (BridgeUtils.validate(config, ReadableType.Array, "stackLabels")) {
        barDataSet.setStackLabels(BridgeUtils.convertToStringArray(config.getArray("stackLabels")));
    }
}
 
Example #6
Source File: CatalystNativeJSToJavaParametersTestCase.java    From react-native-GPay with MIT License 6 votes vote down vote up
public void testIntOutOfRangeThrown() {
  mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnArrayWithLargeInts();
  mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapWithLargeInts();
  waitForBridgeAndUIIdle();

  assertEquals(1, mRecordingTestModule.getArrayCalls().size());
  assertEquals(1, mRecordingTestModule.getMapCalls().size());

  ReadableArray array = mRecordingTestModule.getArrayCalls().get(0);
  assertNotNull(array);

  ReadableMap map = mRecordingTestModule.getMapCalls().get(0);
  assertNotNull(map);

  assertEquals(ReadableType.Number, array.getType(0));
  assertUnexpectedTypeExceptionThrown(array, 0, "int");
  assertEquals(ReadableType.Number, array.getType(1));
  assertUnexpectedTypeExceptionThrown(array, 1, "int");

  assertEquals(ReadableType.Number, map.getType("first"));
  assertUnexpectedTypeExceptionThrown(map, "first", "int");
  assertEquals(ReadableType.Number, map.getType("second"));
  assertUnexpectedTypeExceptionThrown(map, "second", "int");
}
 
Example #7
Source File: CatalystNativeJSToJavaParametersTestCase.java    From react-native-GPay with MIT License 6 votes vote down vote up
public void testGetTypeFromArray() {
  mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class)
      .returnArrayWithStringDoubleIntMapArrayBooleanNull();
  waitForBridgeAndUIIdle();

  List<ReadableArray> calls = mRecordingTestModule.getArrayCalls();
  assertEquals(1, calls.size());
  ReadableArray array = calls.get(0);

  assertEquals(ReadableType.String, array.getType(0));
  assertEquals(ReadableType.Number, array.getType(1));
  assertEquals(ReadableType.Number, array.getType(2));
  assertEquals(ReadableType.Map, array.getType(3));
  assertEquals(ReadableType.Array, array.getType(4));
  assertEquals(ReadableType.Boolean, array.getType(5));
  assertEquals(ReadableType.Null, array.getType(6));
}
 
Example #8
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 #9
Source File: JSStackTrace.java    From react-native-GPay with MIT License 6 votes vote down vote up
public static String format(String message, ReadableArray stack) {
  StringBuilder stringBuilder = new StringBuilder(message).append(", stack:\n");
  for (int i = 0; i < stack.size(); i++) {
    ReadableMap frame = stack.getMap(i);
    stringBuilder
      .append(frame.getString("methodName"))
      .append("@")
      .append(stackFrameToModuleId(frame))
      .append(frame.getInt("lineNumber"));
    if (frame.hasKey("column") &&
      !frame.isNull("column") &&
      frame.getType("column") == ReadableType.Number) {
      stringBuilder
        .append(":")
        .append(frame.getInt("column"));
    }
    stringBuilder.append("\n");
  }
  return stringBuilder.toString();
}
 
Example #10
Source File: SQLiteManager.java    From react-native-android-sqlite with MIT License 6 votes vote down vote up
public void exec(final String sql, final ReadableArray values) {
	SQLiteStatement statement = Assertions.assertNotNull(db).compileStatement(sql);

	db.beginTransaction();
	try {
		for (int i=0; i < values.size(); i++) {
			if (values.getType(i) == ReadableType.Number) {
				statement.bindLong(i+1, values.getInt(i));
			} else {
				statement.bindString(i+1, values.getString(i));
			}
		}

		statement.execute();
		db.setTransactionSuccessful();
	} catch (Exception e) {
		throw e;
	} finally {
		db.endTransaction();
	}
}
 
Example #11
Source File: ChartBaseManager.java    From react-native-mp-android-chart with MIT License 6 votes vote down vote up
@ReactProp(name = "description")
public void setDescription(Chart chart, ReadableMap propMap) {
    if (BridgeUtils.validate(propMap, ReadableType.String, "text")) {
        chart.setDescription(propMap.getString("text"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.String, "textColor")) {
        chart.setDescriptionColor(Color.parseColor(propMap.getString("textColor")));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "textSize")) {
        chart.setDescriptionTextSize((float) propMap.getDouble("textSize"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "positionX") &&
            BridgeUtils.validate(propMap, ReadableType.Number, "positionY")) {

        chart.setDescriptionPosition((float) propMap.getDouble("positionX"), (float) propMap.getDouble("positionY"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.String, "fontFamily") ||
            BridgeUtils.validate(propMap, ReadableType.Number, "fontStyle")) {
        chart.setDescriptionTypeface(BridgeUtils.parseTypeface(chart.getContext(), propMap, "fontStyle", "fontFamily"));
    }
}
 
Example #12
Source File: LayoutShadowNode.java    From react-native-GPay with MIT License 6 votes vote down vote up
void setFromDynamic(Dynamic dynamic) {
  if (dynamic.isNull()) {
    unit = YogaUnit.UNDEFINED;
    value = YogaConstants.UNDEFINED;
  } else if (dynamic.getType() == ReadableType.String) {
    final String s = dynamic.asString();
    if (s.equals("auto")) {
      unit = YogaUnit.AUTO;
      value = YogaConstants.UNDEFINED;
    } else if (s.endsWith("%")) {
      unit = YogaUnit.PERCENT;
      value = Float.parseFloat(s.substring(0, s.length() - 1));
    } else {
      throw new IllegalArgumentException("Unknown value: " + s);
    }
  } else {
    unit = YogaUnit.POINT;
    value = PixelUtil.toPixelFromDIP(dynamic.asDouble());
  }
}
 
Example #13
Source File: CandleStickChartManager.java    From react-native-mp-android-chart with MIT License 6 votes vote down vote up
@Override
CandleEntry createEntry(ReadableArray yValues, int index) {
    if (!ReadableType.Map.equals(yValues.getType(index))) {
        throw new IllegalArgumentException();
    }

    ReadableMap entryData = yValues.getMap(index);
    if (!BridgeUtils.validate(entryData, ReadableType.Number, "shadowH") ||
            !BridgeUtils.validate(entryData, ReadableType.Number, "shadowL") ||
            !BridgeUtils.validate(entryData, ReadableType.Number, "open") ||
            !BridgeUtils.validate(entryData, ReadableType.Number, "close")) {
        throw new IllegalArgumentException("CandleStick data must contain: shadowH, shadowL, open and close values");
    }

    float shadowH = (float) entryData.getDouble("shadowH");
    float shadowL = (float) entryData.getDouble("shadowL");
    float open = (float) entryData.getDouble("open");
    float close = (float) entryData.getDouble("close");

    return new CandleEntry(index, shadowH, shadowL, open, close);
}
 
Example #14
Source File: ChartDataSetConfigUtils.java    From react-native-mp-android-chart with MIT License 6 votes vote down vote up
public static void commonLineRadarConfig(LineRadarDataSet dataSet, ReadableMap config) {
    if (BridgeUtils.validate(config, ReadableType.String, "fillColor")) {
        dataSet.setFillColor(Color.parseColor(config.getString("fillColor")));
    }
    if (BridgeUtils.validate(config, ReadableType.Number, "fillAlpha")) {
        dataSet.setFillAlpha(config.getInt("fillAlpha"));
    }
    // TODO setFillDrawable android.graphics.drawable.Drawable
    if (BridgeUtils.validate(config, ReadableType.Boolean, "drawFilled")) {
        dataSet.setDrawFilled(config.getBoolean("drawFilled"));
    }
    if (BridgeUtils.validate(config, ReadableType.Number, "lineWidth")) {
        float lineWidth = (float) config.getDouble("lineWidth");
        if (lineWidth >= 0.2f && lineWidth < 10f) {
            dataSet.setLineWidth(lineWidth);
        }
    }
}
 
Example #15
Source File: OAuthManagerModule.java    From react-native-oauth with MIT License 5 votes vote down vote up
public static 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, OAuthManagerModule.recursivelyDeconstructReadableMap(readableArray.getMap(i)));
        break;
      case Array:
        deconstructedList.add(i, OAuthManagerModule.recursivelyDeconstructReadableArray(readableArray.getArray(i)));
        break;
      default:
        throw new IllegalArgumentException("Could not convert object at index " + i + ".");
    }
  }
  return deconstructedList;
}
 
Example #16
Source File: BridgeUtils.java    From react-native-mp-android-chart with MIT License 5 votes vote down vote up
public static String[] convertToStringArray(ReadableArray readableArray) {
    String[] array = new String[readableArray.size()];

    for (int i = 0; i < readableArray.size(); i++) {
        if (!ReadableType.String.equals(readableArray.getType(i))) {
            throw new IllegalArgumentException("Expecting array of strings");
        }
        array[i] = readableArray.getString(i);
    }

    return array;
}
 
Example #17
Source File: BridgeUtils.java    From react-native-mp-android-chart with MIT License 5 votes vote down vote up
public static int[] parseColors(ReadableArray readableArray) {
    int[] array = new int[readableArray.size()];

    for (int i = 0; i < readableArray.size(); i++) {
        if (!ReadableType.String.equals(readableArray.getType(i))) {
            throw new IllegalArgumentException("Expecting array of strings");
        }
        array[i] = Color.parseColor(readableArray.getString(i));
    }

    return array;
}
 
Example #18
Source File: Utils.java    From react-native-update with MIT License 5 votes vote down vote up
public static JSONArray convertReadableToJsonArray(ReadableArray arr) {
    JSONArray jsonArr = new JSONArray();
    for (int i=0; i<arr.size(); i++) {
        ReadableType type = arr.getType(i);
        switch (type) {
            case Map:
                jsonArr.put(convertReadableToJsonObject(arr.getMap(i)));
                break;
            case Array:
                jsonArr.put(convertReadableToJsonArray(arr.getArray(i)));
                break;
            case String:
                jsonArr.put(arr.getString(i));
                break;
            case Number:
                Double number = arr.getDouble(i);
                if ((number == Math.floor(number)) && !Double.isInfinite(number)) {
                    // This is a whole number.
                    jsonArr.put(number.longValue());
                } else {
                    try {
                        jsonArr.put(number.doubleValue());
                    } catch (JSONException jsonException) {
                        throw new RuntimeException("Unable to put value " + arr.getDouble(i) + " in JSONArray");
                    }
                }
                break;
            case Boolean:
                jsonArr.put(arr.getBoolean(i));
                break;
            case Null:
                jsonArr.put(null);
                break;
        }
    }

    return jsonArr;
}
 
Example #19
Source File: ChartDataSetConfigUtils.java    From react-native-mp-android-chart with MIT License 5 votes vote down vote up
public static void commonConfig(DataSet dataSet, ReadableMap config) {
    // Setting main color
    if (BridgeUtils.validate(config, ReadableType.String, "color")) {
        dataSet.setColor(Color.parseColor(config.getString("color")));
    }
    if (BridgeUtils.validate(config, ReadableType.Array, "colors")) {
        dataSet.setColors(BridgeUtils.parseColors(config.getArray("colors")));
    }

    // TODO more config to add: https://github.com/PhilJay/MPAndroidChart/wiki/The-DataSet-class

    if (BridgeUtils.validate(config, ReadableType.Boolean, "drawValues")) {
        dataSet.setDrawValues(config.getBoolean("drawValues"));
    }
}
 
Example #20
Source File: FirestackCloudMessaging.java    From react-native-firestack with MIT License 5 votes vote down vote up
@ReactMethod
public void send(String senderId, String messageId, String messageType, ReadableMap params, final Callback callback) {
    FirebaseMessaging fm = FirebaseMessaging.getInstance();
    RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(senderId);
    remoteMessage.setMessageId(messageId);
    remoteMessage.setMessageType(messageType);
    ReadableMapKeySetIterator iterator = params.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType type = params.getType(key);
        if (type == ReadableType.String) {
            remoteMessage.addData(key, params.getString(key));
            Log.d(TAG, "Firebase send: " + key);
            Log.d(TAG, "Firebase send: " + params.getString(key));
        }
    }
    try {
        fm.send(remoteMessage.build());
        WritableMap res = Arguments.createMap();
        res.putString("status", "success");
        callback.invoke(null, res);
    } catch(Exception e) {
        Log.e(TAG, "Error sending message", e);
        WritableMap error = Arguments.createMap();
        error.putString("code", e.toString());
        error.putString("message", e.toString());
        callback.invoke(error);
    }
}
 
Example #21
Source File: BubbleChartManager.java    From react-native-mp-android-chart with MIT License 5 votes vote down vote up
@Override
void dataSetConfig(IDataSet<BubbleEntry> dataSet, ReadableMap config) {
    BubbleDataSet bubbleDataSet = (BubbleDataSet) dataSet;

    ChartDataSetConfigUtils.commonConfig(bubbleDataSet, config);
    ChartDataSetConfigUtils.commonBarLineScatterCandleBubbleConfig(bubbleDataSet, config);

    // BubbleDataSet only config
    if (BridgeUtils.validate(config, ReadableType.Number, "highlightCircleWidth")) {
        bubbleDataSet.setHighlightCircleWidth((float) config.getDouble("highlightCircleWidth"));
    }
}
 
Example #22
Source File: ArrayUtil.java    From vinci with Apache License 2.0 5 votes vote down vote up
public static Object[] toArray(ReadableArray readableArray) {
    Object[] array = new Object[readableArray.size()];

    for (int i = 0; i < readableArray.size(); i++) {
        ReadableType type = readableArray.getType(i);

        switch (type) {
            case Null:
                array[i] = null;
                break;
            case Boolean:
                array[i] = readableArray.getBoolean(i);
                break;
            case Number:
                array[i] = readableArray.getDouble(i);
                break;
            case String:
                array[i] = readableArray.getString(i);
                break;
            case Map:
                array[i] = MapUtil.toMap(readableArray.getMap(i));
                break;
            case Array:
                array[i] = ArrayUtil.toArray(readableArray.getArray(i));
                break;
        }
    }

    return array;
}
 
Example #23
Source File: ArrayUtil.java    From vinci with Apache License 2.0 5 votes vote down vote up
public static JSONArray toJSONArray(ReadableArray readableArray) throws JSONException {
    JSONArray jsonArray = new JSONArray();

    for (int i = 0; i < readableArray.size(); i++) {
        ReadableType type = readableArray.getType(i);

        switch (type) {
            case Null:
                jsonArray.put(i, null);
                break;
            case Boolean:
                jsonArray.put(i, readableArray.getBoolean(i));
                break;
            case Number:
                jsonArray.put(i, readableArray.getDouble(i));
                break;
            case String:
                jsonArray.put(i, readableArray.getString(i));
                break;
            case Map:
                jsonArray.put(i, MapUtil.toJSONObject(readableArray.getMap(i)));
                break;
            case Array:
                jsonArray.put(i, ArrayUtil.toJSONArray(readableArray.getArray(i)));
                break;
        }
    }

    return jsonArray;
}
 
Example #24
Source File: MapUtil.java    From vinci with Apache License 2.0 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 #25
Source File: MapUtil.java    From vinci 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 #26
Source File: SQLitePluginConverter.java    From react-native-sqlite-storage with MIT License 5 votes vote down vote up
static Object get(ReadableMap map,String key,Object defaultValue){
    if (map == null){
        return defaultValue;
    }

    try {
        Object value = null;
        ReadableType type = map.getType(key);
        switch(type){
            case Boolean:
                value = map.getBoolean(key);
                break;
            case Number:
                value = map.getDouble(key);
                break;
            case String:
                value = map.getString(key);
                break;
            case Map:
                value = map.getMap(key);
                break;
            case Array:
                value = map.getArray(key);
                break;
            case Null:
                value = null;
                break;
        }
        return value;
    } catch (NoSuchKeyException ex){
        return defaultValue;
    }
}
 
Example #27
Source File: RNDataWedgeIntentsModule.java    From react-native-datawedge-intents with MIT License 5 votes vote down vote up
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;
}
 
Example #28
Source File: SegmentAnalyticsModule.java    From react-native-segment-analytics with MIT License 5 votes vote down vote up
/**
 * Transforms ReadableMap to java.util.Map object
 * @param readableMap should not be null or empty
 * @return Map object of readableMap
 */
private Map<String, Object> toMap (ReadableMap readableMap) {
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();

    Map<String, Object> map = new HashMap<>();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType readableType = readableMap.getType(key);

        switch (readableType) {
            case Null:
                map.put(key, null);
                break;
            case Boolean:
                map.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                // Can be int or double.
                map.put(key, readableMap.getDouble(key));
                break;
            case String:
                map.put(key, readableMap.getString(key));
                break;
            case Map:
                map.put(key, this.toMap(readableMap.getMap(key)));
                break;
            case Array:
                map.put(key, this.toArray(readableMap.getArray(key)));
                break;
            default:
                throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
        }
    }
    return map;
}
 
Example #29
Source File: SQLitePluginConverter.java    From react-native-sqlite-storage with MIT License 5 votes vote down vote up
static Object get(ReadableMap map,String key,Object defaultValue){
    if (map == null){
        return defaultValue;
    }

    try {
        Object value = null;
        ReadableType type = map.getType(key);
        switch(type){
            case Boolean:
                value = map.getBoolean(key);
                break;
            case Number:
                value = map.getDouble(key);
                break;
            case String:
                value = map.getString(key);
                break;
            case Map:
                value = map.getMap(key);
                break;
            case Array:
                value = map.getArray(key);
                break;
            case Null:
                value = null;
                break;
        }
        return value;
    } catch (NoSuchKeyException ex){
        return defaultValue;
    }
}
 
Example #30
Source File: DefaultNavigationImplementation.java    From native-navigation with MIT License 5 votes vote down vote up
private static boolean arrayEqual(
    ReadableArray a,
    ReadableArray b
) {
  if (b.size() != a.size()) return false;

  for (int i = 0; i < a.size(); i++) {
    ReadableType type = a.getType(i);
    if (type != b.getType(i)) return false;
    switch (type) {
      case Null:
        break;
      case Boolean:
        if (b.getBoolean(i) != a.getBoolean(i)) return false;
        break;
      case Number:
        if (b.getDouble(i) != a.getDouble(i)) return false;
        break;
      case String:
        if (!b.getString(i).equals(a.getString(i))) return false;
        break;
      case Map:
        if (!mapEqual(a.getMap(i), b.getMap(i))) return false;
        break;
      case Array:
        if (!arrayEqual(a.getArray(i), b.getArray(i))) return false;
        break;
      default:
        Log.e(TAG, "Could not compare object with index: " + i + ".");
    }
  }
  return true;
}