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

The following examples show how to use com.facebook.react.bridge.WritableMap#putNull() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: react-native-twilio-chat   File: RCTConvert.java    License: 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 2
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 3
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 4
Source Project: vinci   File: DataTypeUtils.java    License: 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 5
Source Project: react-native-cordova   File: JsonConvert.java    License: 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 6
@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 7
@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 8
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 9
Source Project: react-native-twilio-chat   File: RCTConvert.java    License: 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 10
Source Project: react-native-update   File: Utils.java    License: 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
@Override
public void onUserInfoChange(UserInfo userInfo) {
    WritableMap map = Arguments.createMap();
    map.putMap("userInfo",RCTConvert.UserInfo(userInfo));
    map.putNull("updated");

    sendEvent("ipMessagingClient:userInfoUpdated", map);
}
 
Example 12
Source Project: vinci   File: MapUtil.java    License: 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));
        }

        iterator.remove();
    }

    return writableMap;
}
 
Example 13
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 14
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 15
@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 16
Source Project: Instabug-React-Native   File: MapUtil.java    License: 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 17
Source Project: photo-viewer   File: Utils.java    License: 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;
}
 
Example 18
Source Project: magnet-client   File: Utils.java    License: 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 19
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 20
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;
}