Java Code Examples for com.facebook.react.bridge.WritableMap#putNull()

The following examples show how to use com.facebook.react.bridge.WritableMap#putNull() . 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: MapUtil.java    From vinci with Apache License 2.0 7 votes vote down vote up
public static WritableMap toWritableMap(Map<String, Object> map) {
    WritableMap writableMap = Arguments.createMap();
    Iterator iterator = map.entrySet().iterator();

    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry) iterator.next();
        Object value = pair.getValue();

        if (value == null) {
            writableMap.putNull((String) pair.getKey());
        } else if (value instanceof Boolean) {
            writableMap.putBoolean((String) pair.getKey(), (Boolean) value);
        } else if (value instanceof Double) {
            writableMap.putDouble((String) pair.getKey(), (Double) value);
        } else if (value instanceof Integer) {
            writableMap.putInt((String) pair.getKey(), (Integer) value);
        } else if (value instanceof String) {
            writableMap.putString((String) pair.getKey(), (String) value);
        } else if (value instanceof Map) {
            writableMap.putMap((String) pair.getKey(), MapUtil.toWritableMap((Map<String, Object>) value));
        } else if (value.getClass() != null && value.getClass().isArray()) {
            writableMap.putArray((String) pair.getKey(), ArrayUtil.toWritableArray((Object[]) value));
        }

        iterator.remove();
    }

    return writableMap;
}
 
Example 2
Source File: SQLitePlugin.java    From react-native-sqlite-storage with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
private void bindRow(WritableMap row, String key, Cursor cur, int i) {
    int curType = cur.getType(i);

    switch (curType) {
        case Cursor.FIELD_TYPE_NULL:
            row.putNull(key);
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            row.putDouble(key, cur.getLong(i));
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            row.putDouble(key, cur.getDouble(i));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            row.putString(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT)));
            break;
        case Cursor.FIELD_TYPE_STRING:
        default: /* (not expected) */
            row.putString(key, cur.getString(i));
            break;
    }
}
 
Example 3
Source File: SQLiteAndroidDatabase.java    From react-native-sqlite-storage with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
private void bindRow(WritableMap row, String key, Cursor cur, int i) {
    int curType = cur.getType(i);

    switch (curType) {
        case Cursor.FIELD_TYPE_NULL:
            row.putNull(key);
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            row.putDouble(key, cur.getLong(i));
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            row.putDouble(key, cur.getDouble(i));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            row.putString(key, new String(Base64.encode(cur.getBlob(i), Base64.DEFAULT)));
            break;
        case Cursor.FIELD_TYPE_STRING:
        default:
            row.putString(key, cur.getString(i));
            break;
    }
}
 
Example 4
Source File: JsonConvert.java    From react-native-cordova with MIT License 6 votes vote down vote up
public static WritableMap jsonToReact(JSONObject jsonObject) throws JSONException {
    WritableMap writableMap = Arguments.createMap();
    Iterator iterator = jsonObject.keys();
    while(iterator.hasNext()) {
        String key = (String) iterator.next();
        Object value = jsonObject.get(key);
        if (value instanceof Float || value instanceof Double) {
            writableMap.putDouble(key, jsonObject.getDouble(key));
        } else if (value instanceof Number) {
            writableMap.putInt(key, jsonObject.getInt(key));
        } else if (value instanceof String) {
            writableMap.putString(key, jsonObject.getString(key));
        } else if (value instanceof JSONObject) {
            writableMap.putMap(key,jsonToReact(jsonObject.getJSONObject(key)));
        } else if (value instanceof JSONArray){
            writableMap.putArray(key, jsonToReact(jsonObject.getJSONArray(key)));
        } else if (value == JSONObject.NULL){
            writableMap.putNull(key);
        }
    }

    return writableMap;
}
 
Example 5
Source File: DataTypeUtils.java    From vinci with Apache License 2.0 6 votes vote down vote up
private static void putIntoMap(WritableMap writableMap, String key, Object value) {
    if (value instanceof String) {
        writableMap.putString(key, (String) value);
    } else if (value instanceof Boolean) {
        writableMap.putBoolean(key, (Boolean) value);
    } else if (value instanceof Double) {
        writableMap.putDouble(key, (Double) value);
    } else if (value instanceof Integer) {
        writableMap.putInt(key, (Integer) value);
    } else if (value == null) {
        writableMap.putNull(key);
    } else if (value.getClass().isArray()) {
        WritableArray array = Arguments.createArray();
        for (int i = 0, size = Array.getLength(value); i < size; ++i) {
            putIntoArray(array, Array.get(value, i));
        }
        writableMap.putArray(key, array);
    } else if (value instanceof Collection) {
        writableMap.putArray(key, toWritableArray((Collection<?>) value));
    } else if (value instanceof Map) {
        //noinspection unchecked
        writableMap.putMap(key, toWritableMap((Map<String, ?>) value));
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 6
Source File: RCTConvert.java    From react-native-twilio-ip-messaging with MIT License 6 votes vote down vote up
public static WritableMap Member(Member member) {
    WritableMap map = Arguments.createMap();

    map.putMap("userInfo", UserInfo(member.getUserInfo()));
    if (member.getLastConsumedMessageIndex() == null) {
        map.putNull("lastConsumedMessageIndex");
    }
    else {
        map.putInt("lastConsumedMessageIndex", member.getLastConsumedMessageIndex().intValue());
    }
    if (member.getLastConsumptionTimestamp() == null) {
        map.putNull("lastConsumptionTimestamp");
    }
    else {
        map.putString("lastConsumptionTimestamp", member.getLastConsumptionTimestamp());
    }

    return map;
}
 
Example 7
Source File: FabricTwitterKitUtils.java    From react-native-fabric-twitterkit with MIT License 6 votes vote down vote up
private static WritableMap jsonToWritableMap(final JSONObject jsonObject) throws JSONException {
    final WritableMap writableMap = Arguments.createMap();
    final Iterator iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        final String key = (String) iterator.next();
        final Object value = jsonObject.get(key);
        if (value instanceof Float || value instanceof Double) {
            writableMap.putDouble(key, jsonObject.getDouble(key));
        } else if (value instanceof Number) {
            writableMap.putInt(key, jsonObject.getInt(key));
        } else if (value instanceof String) {
            writableMap.putString(key, jsonObject.getString(key));
        } else if (value instanceof JSONObject) {
            writableMap.putMap(key, jsonToWritableMap(jsonObject.getJSONObject(key)));
        } else if (value instanceof JSONArray) {
            writableMap.putArray(key, jsonToWritableArray(jsonObject.getJSONArray(key)));
        } else if (value instanceof Boolean) {
            writableMap.putBoolean(key, jsonObject.getBoolean(key));
        } else if (value == JSONObject.NULL) {
            writableMap.putNull(key);
        }
    }
    return writableMap;
}
 
Example 8
Source File: RCTConvert.java    From react-native-twilio-chat with MIT License 6 votes vote down vote up
public static WritableMap Member(Member member) {
    WritableMap map = Arguments.createMap();

    map.putMap("userInfo", UserInfo(member.getUserInfo()));
    if (member.getLastConsumedMessageIndex() == null) {
        map.putNull("lastConsumedMessageIndex");
    }
    else {
        map.putInt("lastConsumedMessageIndex", member.getLastConsumedMessageIndex().intValue());
    }
    if (member.getLastConsumptionTimestamp() == null) {
        map.putNull("lastConsumptionTimestamp");
    }
    else {
        map.putString("lastConsumptionTimestamp", member.getLastConsumptionTimestamp());
    }

    return map;
}
 
Example 9
Source File: RCTTwilioIPMessagingClient.java    From react-native-twilio-ip-messaging with MIT License 5 votes vote down vote up
@Override
public void onUserInfoChange(UserInfo userInfo) {
    WritableMap map = Arguments.createMap();
    map.putMap("userInfo",RCTConvert.UserInfo(userInfo));
    map.putNull("updated");

    sendEvent("ipMessagingClient:userInfoUpdated", map);
}
 
Example 10
Source File: Utils.java    From react-native-update with MIT License 5 votes vote down vote up
public static WritableMap convertJsonObjectToWriteable(JSONObject jsonObj) {
    WritableMap map = Arguments.createMap();
    Iterator<String> it = jsonObj.keys();
    while(it.hasNext()){
        String key = it.next();
        Object obj = null;
        try {
            obj = jsonObj.get(key);
        } catch (JSONException jsonException) {
            // Should not happen.
            throw new RuntimeException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException);
        }

        if (obj instanceof JSONObject)
            map.putMap(key, convertJsonObjectToWriteable((JSONObject) obj));
        else if (obj instanceof JSONArray)
            map.putArray(key, convertJsonArrayToWriteable((JSONArray) obj));
        else if (obj instanceof String)
            map.putString(key, (String) obj);
        else if (obj instanceof Double)
            map.putDouble(key, (Double) obj);
        else if (obj instanceof Integer)
            map.putInt(key, (Integer) obj);
        else if (obj instanceof Boolean)
            map.putBoolean(key, (Boolean) obj);
        else if (obj == null)
            map.putNull(key);
        else
            throw new RuntimeException("Unrecognized object: " + obj);
    }

    return map;
}
 
Example 11
Source File: ArgumentUtils.java    From react-native-pjsip with GNU General Public License v3.0 5 votes vote down vote up
private static WritableMap fromJsonObject(JsonObject object) {
    WritableMap result = new WritableNativeMap();

    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        Object value = fromJson(entry.getValue());

        if (value instanceof WritableMap) {
            result.putMap(entry.getKey(), (WritableMap) value);
        } else if (value instanceof WritableArray) {
            result.putArray(entry.getKey(), (WritableArray) value);
        } else if (value instanceof String) {
            result.putString(entry.getKey(), (String) value);
        } else if (value instanceof LazilyParsedNumber) {
            result.putInt(entry.getKey(), ((LazilyParsedNumber) value).intValue());
        } else if (value instanceof Integer) {
            result.putInt(entry.getKey(), (Integer) value);
        } else if (value instanceof Double) {
            result.putDouble(entry.getKey(), (Double) value);
        } else if (value instanceof Boolean) {
            result.putBoolean(entry.getKey(), (Boolean) value);
        } else {
            Log.d("ArgumentUtils", "Unknown type: " + value.getClass().getName());
            result.putNull(entry.getKey());
        }
    }

    return result;
}
 
Example 12
Source File: MapUtil.java    From react-native-background-geolocation with Apache License 2.0 5 votes vote down vote up
public static WritableMap toWritableMap(Map<String, Object> map) {
    WritableMap writableMap = Arguments.createMap();
    Iterator iterator = map.entrySet().iterator();

    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        Object value = pair.getValue();

        if (value == null) {
            writableMap.putNull((String) pair.getKey());
        } else if (value instanceof Boolean) {
            writableMap.putBoolean((String) pair.getKey(), (Boolean) value);
        } else if (value instanceof Double) {
            writableMap.putDouble((String) pair.getKey(), (Double) value);
        } else if (value instanceof Integer) {
            writableMap.putInt((String) pair.getKey(), (Integer) value);
        } else if (value instanceof String) {
            writableMap.putString((String) pair.getKey(), (String) value);
        } else if (value instanceof Map) {
            writableMap.putMap((String) pair.getKey(), MapUtil.toWritableMap((Map<String, Object>) value));
        } else if (value.getClass() != null && value.getClass().isArray()) {
            writableMap.putArray((String) pair.getKey(), ArrayUtil.toWritableArray((Object[]) value));
        }
    }

    return writableMap;
}
 
Example 13
Source File: ConfigMapperTest.java    From react-native-background-geolocation with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullableProps() throws JSONException {
    WritableMap map = Arguments.createMap();
    map.putNull("url");
    map.putNull("syncUrl");
    map.putNull("notificationIconColor");
    map.putNull("notificationTitle");
    map.putNull("notificationText");
    map.putNull("notificationIconLarge");
    map.putNull("notificationIconSmall");

    Config config = ConfigMapper.fromMap((ReadableMap)map);

    Assert.assertEquals(Config.NullString, config.getUrl());
    Assert.assertTrue(config.hasUrl());
    Assert.assertFalse(config.hasValidUrl());

    Assert.assertEquals(Config.NullString, config.getSyncUrl());
    Assert.assertTrue(config.hasSyncUrl());
    Assert.assertFalse(config.hasValidSyncUrl());

    Assert.assertEquals(Config.NullString, config.getNotificationIconColor());
    Assert.assertFalse(config.hasNotificationIconColor());

    Assert.assertEquals(Config.NullString, config.getNotificationTitle());
    Assert.assertTrue(config.hasNotificationTitle());

    Assert.assertEquals(Config.NullString, config.getNotificationText());
    Assert.assertTrue(config.hasNotificationText());

    Assert.assertEquals(Config.NullString, config.getLargeNotificationIcon());
    Assert.assertFalse(config.hasLargeNotificationIcon());

    Assert.assertEquals(Config.NullString, config.getSmallNotificationIcon());
    Assert.assertFalse(config.hasSmallNotificationIcon());
}
 
Example 14
Source File: MapUtil.java    From Instabug-React-Native with MIT License 5 votes vote down vote up
public static WritableMap toWritableMap(Map<String, Object> map) {
    WritableMap writableMap = Arguments.createMap();
    Iterator iterator = map.entrySet().iterator();

    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry) iterator.next();
        Object value = pair.getValue();

        if (value == null) {
            writableMap.putNull((String) pair.getKey());
        } else if (value instanceof Boolean) {
            writableMap.putBoolean((String) pair.getKey(), (Boolean) value);
        } else if (value instanceof Double) {
            writableMap.putDouble((String) pair.getKey(), (Double) value);
        } else if (value instanceof Integer) {
            writableMap.putInt((String) pair.getKey(), (Integer) value);
        } else if (value instanceof String) {
            writableMap.putString((String) pair.getKey(), (String) value);
        } else if (value instanceof Map) {
            writableMap.putMap((String) pair.getKey(), MapUtil.toWritableMap((Map<String,
                    Object>) value));
        } else if (value.getClass() != null && value.getClass().isArray()) {
            writableMap.putArray((String) pair.getKey(), ArrayUtil.toWritableArray((Object[])
                    value));
        }

        iterator.remove();
    }

    return writableMap;
}
 
Example 15
Source File: RCTConvert.java    From react-native-twilio-chat with MIT License 5 votes vote down vote up
public static WritableMap jsonToWritableMap(JSONObject jsonObject) {
    WritableMap writableMap = new WritableNativeMap();

    if (jsonObject == null) {
        return null;
    }


    Iterator<String> iterator = jsonObject.keys();
    if (!iterator.hasNext()) {
        return null;
    }


    try {
        while (iterator.hasNext()) {
            String key = iterator.next();
            Object value = jsonObject.get(key);
            if (value == null) {
                writableMap.putNull(key);
            } else if (value instanceof Boolean) {
                writableMap.putBoolean(key, (Boolean) value);
            } else if (value instanceof Integer) {
                writableMap.putInt(key, (Integer) value);
            } else if (value instanceof Double) {
                writableMap.putDouble(key, (Double) value);
            } else if (value instanceof String) {
                writableMap.putString(key, (String) value);
            } else if (value instanceof JSONObject) {
                writableMap.putMap(key, jsonToWritableMap((JSONObject) value));
            } else if (value instanceof JSONArray) {
                writableMap.putArray(key, jsonArrayToWritableArray((JSONArray) value));
            }
        }
    } catch (JSONException ex){
            // Do nothing and fail silently
    }

    return writableMap;
}
 
Example 16
Source File: ArgumentUtils.java    From react-native-sip with GNU General Public License v3.0 5 votes vote down vote up
private static WritableMap fromJsonObject(JsonObject object) {
    WritableMap result = new WritableNativeMap();

    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        Object value = fromJson(entry.getValue());

        if (value instanceof WritableMap) {
            result.putMap(entry.getKey(), (WritableMap) value);
        } else if (value instanceof WritableArray) {
            result.putArray(entry.getKey(), (WritableArray) value);
        } else if (value instanceof String) {
            result.putString(entry.getKey(), (String) value);
        } else if (value instanceof LazilyParsedNumber) {
            result.putInt(entry.getKey(), ((LazilyParsedNumber) value).intValue());
        } else if (value instanceof Integer) {
            result.putInt(entry.getKey(), (Integer) value);
        } else if (value instanceof Double) {
            result.putDouble(entry.getKey(), (Double) value);
        } else if (value instanceof Boolean) {
            result.putBoolean(entry.getKey(), (Boolean) value);
        } else {
            Log.d("ArgumentUtils", "Unknown type: " + value.getClass().getName());
            result.putNull(entry.getKey());
        }
    }

    return result;
}
 
Example 17
Source File: Utils.java    From magnet-client with Mozilla Public License 2.0 4 votes vote down vote up
@Nullable
public static WritableMap jsonToWritableMap(JSONObject jsonObject) {
    WritableMap writableMap = new WritableNativeMap();

    if (jsonObject == null) {
        return null;
    }

    Iterator<String> iterator = jsonObject.keys();
    if (!iterator.hasNext()) {
        return writableMap;
    }

    while (iterator.hasNext()) {
        String key = iterator.next();

        try {
            Object value = jsonObject.get(key);

            if (value == null) {
                writableMap.putNull(key);
            } else if (value instanceof Boolean) {
                writableMap.putBoolean(key, (Boolean) value);
            } else if (value instanceof Integer) {
                writableMap.putInt(key, (Integer) value);
            } else if (value instanceof Double) {
                writableMap.putDouble(key, (Double) value);
            } else if (value instanceof String) {
                writableMap.putString(key, (String) value);
            } else if (value instanceof JSONObject) {
                writableMap.putMap(key, jsonToWritableMap((JSONObject) value));
            } else if (value instanceof JSONArray) {
                writableMap.putArray(key, jsonArrayToWritableArray((JSONArray) value));
            }
        } catch (JSONException ex) {
            // Do nothing and fail silently
        }
    }

    return writableMap;
}
 
Example 18
Source File: RCTConvert.java    From react-native-twilio-ip-messaging with MIT License 4 votes vote down vote up
public static WritableMap jsonToWritableMap(JSONObject jsonObject) {
    WritableMap writableMap = new WritableNativeMap();

    if (jsonObject == null) {
        return null;
    }


    Iterator<String> iterator = jsonObject.keys();
    if (!iterator.hasNext()) {
        return null;
    }

    while (iterator.hasNext()) {
        String key = iterator.next();

        try {
            Object value = jsonObject.get(key);

            if (value == null) {
                writableMap.putNull(key);
            } else if (value instanceof Boolean) {
                writableMap.putBoolean(key, (Boolean) value);
            } else if (value instanceof Integer) {
                writableMap.putInt(key, (Integer) value);
            } else if (value instanceof Double) {
                writableMap.putDouble(key, (Double) value);
            } else if (value instanceof String) {
                writableMap.putString(key, (String) value);
            } else if (value instanceof JSONObject) {
                writableMap.putMap(key, jsonToWritableMap((JSONObject) value));
            } else if (value instanceof JSONArray) {
                writableMap.putArray(key, jsonArrayToWritableArray((JSONArray) value));
            }
        } catch (JSONException ex) {
            // Do nothing and fail silently
        }
    }

    return writableMap;
}
 
Example 19
Source File: ReactNativeUtil.java    From react-native-azurenotificationhub with MIT License 4 votes vote down vote up
public static WritableMap convertBundleToMap(Bundle bundle) {
    WritableMap map = Arguments.createMap();
    if (bundle == null) {
        return map;
    }

    for (String key : bundle.keySet()) {
        try {
            Object o = bundle.get(key);

            // TODO: Handle char and all array types
            if (o == null) {
                map.putNull(key);
            } else if (o instanceof Bundle) {
                map.putMap(key, convertBundleToMap((Bundle) o));
            } else if (o instanceof String) {
                map.putString(key, (String) o);
            } else if (o instanceof Float) {
                map.putDouble(key, ((Float) o).doubleValue());
            } else if (o instanceof Double) {
                map.putDouble(key, (Double) o);
            } else if (o instanceof Integer) {
                map.putInt(key, (Integer) o);
            } else if (o instanceof Long) {
                map.putInt(key, ((Long) o).intValue());
            } else if (o instanceof Short) {
                map.putInt(key, ((Short) o).intValue());
            } else if (o instanceof Byte) {
                map.putInt(key, ((Byte) o).intValue());
            } else if (o instanceof Boolean) {
                map.putBoolean(key, (Boolean) o);
            } else {
                map.putNull(key);
            }
        } catch (ClassCastException e) {
            // TODO: Warn
        }
    }

    return map;
}
 
Example 20
Source File: Utils.java    From photo-viewer with Apache License 2.0 4 votes vote down vote up
@Nullable
public static WritableMap jsonToWritableMap(JSONObject jsonObject) {
    WritableMap writableMap = new WritableNativeMap();

    if (jsonObject == null) {
        return null;
    }


    Iterator<String> iterator = jsonObject.keys();
    if (!iterator.hasNext()) {
        return null;
    }

    while (iterator.hasNext()) {
        String key = iterator.next();

        try {
            Object value = jsonObject.get(key);

            if (value == null) {
                writableMap.putNull(key);
            } else if (value instanceof Boolean) {
                writableMap.putBoolean(key, (Boolean) value);
            } else if (value instanceof Integer) {
                writableMap.putInt(key, (Integer) value);
            } else if (value instanceof Double) {
                writableMap.putDouble(key, (Double) value);
            } else if (value instanceof String) {
                writableMap.putString(key, (String) value);
            } else if (value instanceof JSONObject) {
                writableMap.putMap(key, jsonToWritableMap((JSONObject) value));
            } else if (value instanceof JSONArray) {
                writableMap.putArray(key, jsonArrayToWritableArray((JSONArray) value));
            }
        } catch (JSONException ex) {
            // Do nothing and fail silently
        }
    }

    return writableMap;
}