Files
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java
T
VonD 351c97ec37 adding missing string case in ReadableNativeMap to HashMap
Summary:about #6639
Closes https://github.com/facebook/react-native/pull/6762

Differential Revision: D3126541

fb-gh-sync-id: f895e6d3b4c0abec41b56a031828763a4e8e210f
fbshipit-source-id: f895e6d3b4c0abec41b56a031828763a4e8e210f
2016-04-01 09:12:20 -07:00

108 lines
3.0 KiB
Java

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.bridge;
import com.facebook.jni.Countable;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import java.util.HashMap;
/**
* Implementation of a read-only map in native memory. This will generally be constructed and filled
* in native code so you shouldn't construct one yourself.
*/
@DoNotStrip
public class ReadableNativeMap extends NativeMap implements ReadableMap {
static {
SoLoader.loadLibrary(ReactBridge.REACT_NATIVE_LIB);
}
@Override
public native boolean hasKey(String name);
@Override
public native boolean isNull(String name);
@Override
public native boolean getBoolean(String name);
@Override
public native double getDouble(String name);
@Override
public native int getInt(String name);
@Override
public native String getString(String name);
@Override
public native ReadableNativeArray getArray(String name);
@Override
public native ReadableNativeMap getMap(String name);
@Override
public native ReadableType getType(String name);
@Override
public ReadableMapKeySetIterator keySetIterator() {
return new ReadableNativeMapKeySetIterator(this);
}
public HashMap<String, Object>toHashMap() {
ReadableMapKeySetIterator iterator = keySetIterator();
HashMap<String, Object> hashMap = new HashMap<>();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
switch (getType(key)) {
case Null:
hashMap.put(key, null);
break;
case Boolean:
hashMap.put(key, getBoolean(key));
break;
case Number:
hashMap.put(key, getDouble(key));
break;
case String:
hashMap.put(key, getString(key));
break;
case Map:
hashMap.put(key, getMap(key).toHashMap());
break;
case Array:
hashMap.put(key, getArray(key).toArrayList());
break;
default:
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
}
}
return hashMap;
}
/**
* Implementation of a {@link ReadableNativeMap} iterator in native memory.
*/
@DoNotStrip
private static class ReadableNativeMapKeySetIterator extends Countable
implements ReadableMapKeySetIterator {
private final ReadableNativeMap mReadableNativeMap;
public ReadableNativeMapKeySetIterator(ReadableNativeMap readableNativeMap) {
mReadableNativeMap = readableNativeMap;
initialize(mReadableNativeMap);
}
@Override
public native boolean hasNextKey();
@Override
public native String nextKey();
private native void initialize(ReadableNativeMap readableNativeMap);
}
}