List of usage examples for com.facebook.react.bridge ReadableMap keySetIterator
@NonNull ReadableMapKeySetIterator keySetIterator();
From source file:com.adjust.nativemodule.AdjustUtil.java
License:Open Source License
/** * toMap converts a {@link ReadableMap} into a HashMap. * //from www.j a v a2s . co m * @param readableMap The ReadableMap to be conveted. * @return A HashMap containing the data that was in the ReadableMap. */ public static Map<String, Object> toMap(@Nullable ReadableMap readableMap) { if (readableMap == null) { return null; } com.facebook.react.bridge.ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); if (!iterator.hasNextKey()) { return null; } Map<String, Object> result = new HashMap<>(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); String value = toObject(readableMap, key).toString(); if (value == null) { AdjustFactory.getLogger().warn("Null parameter inside key-value pair with key: " + key); continue; } result.put(key, value); } return result; }
From source file:com.amazonaws.reactnative.core.AWSRNClientMarshaller.java
License:Open Source License
public static Map<String, Object> readableMapToMap(final @Nullable ReadableMap readableMap) { if (readableMap == null) { return new HashMap<>(); }/*from ww w .j a va 2 s . c om*/ final ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); if (!iterator.hasNextKey()) { return new HashMap<>(); } final Map<String, Object> result = new HashMap<>(); while (iterator.hasNextKey()) { final String key = iterator.nextKey(); result.put(key, toObject(readableMap, key)); } return result; }
From source file:com.amazonaws.reactnative.core.AWSRNCognitoCredentials.java
License:Open Source License
@ReactMethod public void setLogins(final ReadableMap logins) { final Map<String, String> userLogins = new HashMap<String, String>(); final ReadableMapKeySetIterator loginsIterable = logins.keySetIterator(); while (loginsIterable.hasNextKey()) { final String key = loginsIterable.nextKey(); userLogins.put(keyConverter(key), logins.getString(key)); }//from w w w .ja va 2 s . c om if (userLogins.size() != 0) { credentialsProvider.setLogins(userLogins); } }
From source file:com.blockablewebview.BlockableWebViewManager.java
License:Open Source License
@ReactProp(name = "source") public void setSource(WebView view, @Nullable ReadableMap source) { if (source != null) { if (source.hasKey("html")) { String html = source.getString("html"); if (source.hasKey("baseUrl")) { view.loadDataWithBaseURL(source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);/*from w w w. jav a 2 s. c o m*/ } else { view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING); } return; } if (source.hasKey("uri")) { String url = source.getString("uri"); String previousUrl = view.getUrl(); if (previousUrl != null && previousUrl.equals(url)) { return; } if (source.hasKey("method")) { String method = source.getString("method"); if (method.equals(HTTP_METHOD_POST)) { byte[] postData = null; if (source.hasKey("body")) { String body = source.getString("body"); try { postData = body.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { postData = body.getBytes(); } } if (postData == null) { postData = new byte[0]; } view.postUrl(url, postData); return; } } HashMap<String, String> headerMap = new HashMap<>(); if (source.hasKey("headers")) { ReadableMap headers = source.getMap("headers"); ReadableMapKeySetIterator iter = headers.keySetIterator(); while (iter.hasNextKey()) { String key = iter.nextKey(); headerMap.put(key, headers.getString(key)); } } view.loadUrl(url, headerMap); return; } } view.loadUrl(BLANK_URL); }
From source file:com.boundlessgeo.spatialconnect.jsbridge.RNSpatialConnect.java
License:Apache License
private HashMap<String, Object> convertMapToHashMap(ReadableMap readableMap) { HashMap<String, Object> json = new HashMap<>(); ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); switch (readableMap.getType(key)) { case Null: json.put(key, null);// w ww . j a va 2 s . co m break; case Boolean: json.put(key, readableMap.getBoolean(key)); break; case Number: json.put(key, readableMap.getDouble(key)); break; case String: json.put(key, readableMap.getString(key)); break; case Map: json.put(key, convertMapToHashMap(readableMap.getMap(key))); break; case Array: json.put(key, convertArrayToArrayList(readableMap.getArray(key))); break; } } return json; }
From source file:com.boundlessgeo.spatialconnect.jsbridge.SCBridge.java
License:Apache License
private static JSONObject convertMapToJson(ReadableMap readableMap) { JSONObject object = new JSONObject(); ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); try {// w w w.j a v a 2 s.c o m while (iterator.hasNextKey()) { String key = iterator.nextKey(); switch (readableMap.getType(key)) { case Null: object.put(key, JSONObject.NULL); break; case Boolean: object.put(key, readableMap.getBoolean(key)); break; case Number: object.put(key, readableMap.getDouble(key)); break; case String: object.put(key, readableMap.getString(key)); break; case Map: object.put(key, convertMapToJson(readableMap.getMap(key))); break; case Array: object.put(key, convertArrayToJson(readableMap.getArray(key))); break; } } } catch (JSONException e) { Log.e(LOG_TAG, "Could not convert to json"); e.printStackTrace(); } return object; }
From source file:com.farmisen.react_native_file_uploader.RCTFileUploaderModule.java
License:Open Source License
private void uploadFile(String path, ReadableMap settings, Callback callback) { HttpURLConnection connection = null; FileUploadCountingOutputStream outputStream = null; InputStream inputStream = null; String boundary = "*****" + UUID.randomUUID().toString() + "*****"; try {/*from w ww.java 2 s. co m*/ URL url = new URL(settings.getString(UPLOAD_URL_FIELD)); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); String method = getStringParam(settings, METHOD_FIELD, "POST"); connection.setRequestMethod(method); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("User-Agent", "React Native File Uploader Android HTTP Client"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); String contentType = getStringParam(settings, CONTENT_TYPE_FIELD, "application/octet-stream"); String fileName = getStringParam(settings, FILE_NAME_FIELD, this.filenameForContentType(contentType)); String fieldName = getStringParam(settings, FIELD_NAME_FIELD, "file"); File file = new File(path); FileInputStream fileInputStream = new FileInputStream(file); int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = MAX_BUFFER_SIZE; outputStream = new FileUploadCountingOutputStream(new DataOutputStream(connection.getOutputStream()), file.length(), settings.getString(URI_FIELD), this); outputStream.writeBytes(TWO_HYPHENS + boundary + LINE_END); outputStream.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"" + LINE_END); outputStream.writeBytes("Content-Type: " + contentType + LINE_END); outputStream.writeBytes("Content-Transfer-Encoding: binary" + LINE_END); outputStream.writeBytes(LINE_END); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(LINE_END); ReadableMap params = getMapParam(settings, "data", Arguments.createMap()); ReadableMapKeySetIterator keys = params.keySetIterator(); while (keys.hasNextKey()) { String key = keys.nextKey(); ReadableType type = params.getType(key); String value = null; switch (type) { case String: value = params.getString(key); break; case Number: value = Integer.toString(params.getInt(key)); break; default: callback.invoke(type.toString() + " type not supported.", null); break; } outputStream.writeBytes(TWO_HYPHENS + boundary + LINE_END); outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + LINE_END); outputStream.writeBytes("Content-Type: text/plain" + LINE_END); outputStream.writeBytes(LINE_END + value + LINE_END); } outputStream.writeBytes(TWO_HYPHENS + boundary + TWO_HYPHENS + LINE_END); inputStream = connection.getInputStream(); String responseBody = this.streamToString(inputStream); fileInputStream.close(); inputStream.close(); outputStream.flush(); outputStream.close(); WritableMap result = Arguments.createMap(); result.putString("data", responseBody); result.putInt("status", connection.getResponseCode()); callback.invoke(null, result); } catch (Exception e) { callback.invoke(e.getLocalizedMessage(), null); } }
From source file:com.ibatimesheet.RNJSONUtils.java
License:Apache License
public static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException { JSONObject object = new JSONObject(); ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); switch (readableMap.getType(key)) { case Null: object.put(key, JSONObject.NULL); break; case Boolean: object.put(key, readableMap.getBoolean(key)); break; case Number: object.put(key, readableMap.getDouble(key)); break; case String: object.put(key, readableMap.getString(key)); break; case Map: object.put(key, convertMapToJson(readableMap.getMap(key))); break; case Array: object.put(key, convertArrayToJson(readableMap.getArray(key))); break; }/* w w w . ja v a 2 s . co m*/ } return object; }
From source file:com.lightappbuilder.lab4.lablibrary.rnviews.webview.ReactWebViewManager.java
License:Open Source License
@ReactProp(name = "source") public void setSource(DynamicSizeWrapperView wrapperView, @Nullable ReadableMap source) { ReactWebView view = (ReactWebView) wrapperView.getWrappedChild(); if (source != null) { if (source.hasKey("html")) { String html = source.getString("html"); if (source.hasKey("baseUrl")) { view.loadDataWithBaseURL(source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null);//from w ww . j a v a2s .com } else { view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING); } return; } if (source.hasKey("uri")) { String url = source.getString("uri"); String previousUrl = view.getUrl(); if (previousUrl != null && previousUrl.equals(url)) { return; } if (source.hasKey("method")) { String method = source.getString("method"); if (method.equals(HTTP_METHOD_POST)) { byte[] postData = null; if (source.hasKey("body")) { String body = source.getString("body"); try { postData = body.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { postData = body.getBytes(); } } if (postData == null) { postData = new byte[0]; } view.postUrl(url, postData); return; } } HashMap<String, String> headerMap = new HashMap<>(); if (source.hasKey("headers")) { ReadableMap headers = source.getMap("headers"); ReadableMapKeySetIterator iter = headers.keySetIterator(); while (iter.hasNextKey()) { String key = iter.nextKey(); if ("user-agent".equals(key.toLowerCase(Locale.ENGLISH))) { if (view.getSettings() != null) { view.getSettings().setUserAgentString(headers.getString(key)); } } else { headerMap.put(key, headers.getString(key)); } } } view.loadUrl(url, headerMap); return; } } view.loadUrl(BLANK_URL); }
From source file:com.microsoft.appcenter.reactnative.analytics.ReactNativeUtils.java
License:Open Source License
public static JSONObject convertReadableMapToJsonObject(ReadableMap map) throws JSONException { JSONObject jsonObj = new JSONObject(); ReadableMapKeySetIterator it = map.keySetIterator(); while (it.hasNextKey()) { String key = it.nextKey(); ReadableType type = map.getType(key); switch (type) { case Map: jsonObj.put(key, convertReadableMapToJsonObject(map.getMap(key))); break; case Array: jsonObj.put(key, convertReadableArrayToJsonArray(map.getArray(key))); break; case String: jsonObj.put(key, map.getString(key)); break; case Number: Double number = map.getDouble(key); if ((number == Math.floor(number)) && !Double.isInfinite(number)) { jsonObj.put(key, number.longValue()); } else { jsonObj.put(key, number.doubleValue()); }/*from w w w . ja va2 s. c o m*/ break; case Boolean: jsonObj.put(key, map.getBoolean(key)); break; default: jsonObj.put(key, null); break; } } return jsonObj; }