mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Kotlinify JSONArguments (#50623)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50623 As per title. Changelog: [Internal] Reviewed By: rshest Differential Revision: D72784115 fbshipit-source-id: 601bae57cd1143f3cdf5f3e93bfed36b8a2015b7
This commit is contained in:
committed by
Facebook GitHub Bot
parent
7aa0499e11
commit
f70dcd8912
@@ -786,12 +786,12 @@ public abstract interface class com/facebook/react/bridge/JSInstance {
|
||||
public abstract fun invokeCallback (ILcom/facebook/react/bridge/NativeArrayInterface;)V
|
||||
}
|
||||
|
||||
public class com/facebook/react/bridge/JSONArguments {
|
||||
public fun <init> ()V
|
||||
public static fun fromJSONArray (Lorg/json/JSONArray;)Lcom/facebook/react/bridge/ReadableArray;
|
||||
public static fun fromJSONArrayString (Ljava/lang/String;)Lcom/facebook/react/bridge/ReadableArray;
|
||||
public static fun fromJSONObject (Lorg/json/JSONObject;)Lcom/facebook/react/bridge/ReadableMap;
|
||||
public static fun fromJSONObjectString (Ljava/lang/String;)Lcom/facebook/react/bridge/ReadableMap;
|
||||
public final class com/facebook/react/bridge/JSONArguments {
|
||||
public static final field INSTANCE Lcom/facebook/react/bridge/JSONArguments;
|
||||
public static final fun fromJSONArray (Lorg/json/JSONArray;)Lcom/facebook/react/bridge/ReadableArray;
|
||||
public static final fun fromJSONArrayString (Ljava/lang/String;)Lcom/facebook/react/bridge/ReadableArray;
|
||||
public static final fun fromJSONObject (Lorg/json/JSONObject;)Lcom/facebook/react/bridge/ReadableMap;
|
||||
public static final fun fromJSONObjectString (Ljava/lang/String;)Lcom/facebook/react/bridge/ReadableMap;
|
||||
}
|
||||
|
||||
public final class com/facebook/react/bridge/JavaOnlyArray : com/facebook/react/bridge/ReadableArray, com/facebook/react/bridge/WritableArray {
|
||||
|
||||
-121
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.infer.annotation.Nullsafe;
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitecture;
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel;
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger;
|
||||
import java.util.Iterator;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@Nullsafe(Nullsafe.Mode.LOCAL)
|
||||
@LegacyArchitecture
|
||||
public class JSONArguments {
|
||||
static {
|
||||
LegacyArchitectureLogger.assertLegacyArchitecture(
|
||||
"JSONArguments", LegacyArchitectureLogLevel.WARNING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JSONObject to ReadableMap
|
||||
*
|
||||
* @param obj The JSONObject to be parsed
|
||||
* @return readableMap from the JSONObject
|
||||
*/
|
||||
public static ReadableMap fromJSONObject(JSONObject obj) throws JSONException {
|
||||
WritableMap result = Arguments.createMap();
|
||||
Iterator<String> keys = obj.keys();
|
||||
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
Object val = obj.get(key);
|
||||
if (val instanceof JSONObject) {
|
||||
result.putMap(key, fromJSONObject((JSONObject) val));
|
||||
} else if (val instanceof JSONArray) {
|
||||
result.putArray(key, fromJSONArray((JSONArray) val));
|
||||
} else if (val instanceof String) {
|
||||
result.putString(key, (String) val);
|
||||
} else if (val instanceof Boolean) {
|
||||
result.putBoolean(key, (Boolean) val);
|
||||
} else if (val instanceof Integer) {
|
||||
result.putInt(key, (Integer) val);
|
||||
} else if (val instanceof Double) {
|
||||
result.putDouble(key, (Double) val);
|
||||
} else if (val instanceof Long) {
|
||||
result.putInt(key, ((Long) val).intValue());
|
||||
} else if (obj.isNull(key)) {
|
||||
result.putNull(key);
|
||||
} else {
|
||||
// Unknown value type. Will throw
|
||||
throw new JSONException("Unexpected value when parsing JSON object. key: " + key);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse String of JSON object to ReadableMap
|
||||
*
|
||||
* @param objStr The String JSON object to be parsed
|
||||
* @return readableMap from the JSONArray
|
||||
*/
|
||||
public static ReadableMap fromJSONObjectString(String objStr) throws JSONException {
|
||||
return fromJSONObject(new JSONObject(objStr));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JSONArray to ReadableArray
|
||||
*
|
||||
* @param arr The JSONArray to be parsed
|
||||
* @return readableArray from the JSONArray
|
||||
*/
|
||||
public static ReadableArray fromJSONArray(JSONArray arr) throws JSONException {
|
||||
WritableArray result = Arguments.createArray();
|
||||
|
||||
for (int i = 0; i < arr.length(); i++) {
|
||||
Object val = arr.get(i);
|
||||
|
||||
if (val instanceof JSONObject) {
|
||||
result.pushMap(fromJSONObject((JSONObject) val));
|
||||
} else if (val instanceof JSONArray) {
|
||||
result.pushArray(fromJSONArray((JSONArray) val));
|
||||
} else if (val instanceof String) {
|
||||
result.pushString((String) val);
|
||||
} else if (val instanceof Boolean) {
|
||||
result.pushBoolean((Boolean) val);
|
||||
} else if (val instanceof Integer) {
|
||||
result.pushInt((Integer) val);
|
||||
} else if (val instanceof Double) {
|
||||
result.pushDouble((Double) val);
|
||||
} else if (val instanceof Long) {
|
||||
result.pushInt(((Long) val).intValue());
|
||||
} else if (arr.isNull(i)) {
|
||||
result.pushNull();
|
||||
} else {
|
||||
// Unknown value type. Will throw
|
||||
throw new JSONException("Unexpected value when parsing JSON array. index: " + i);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse String of JSON array to ReadableArray
|
||||
*
|
||||
* @param arrStr The String JSON array to be parsed
|
||||
* @return readableArray from the JSONArray
|
||||
*/
|
||||
public static ReadableArray fromJSONArrayString(String arrStr) throws JSONException {
|
||||
return fromJSONArray(new JSONArray(arrStr));
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge
|
||||
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitecture
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
|
||||
@LegacyArchitecture
|
||||
public object JSONArguments {
|
||||
|
||||
init {
|
||||
LegacyArchitectureLogger.assertLegacyArchitecture(
|
||||
"JSONArguments", LegacyArchitectureLogLevel.WARNING)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JSONObject to ReadableMap
|
||||
*
|
||||
* @param obj The JSONObject to be parsed
|
||||
* @return readableMap from the JSONObject
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(JSONException::class)
|
||||
public fun fromJSONObject(obj: JSONObject): ReadableMap {
|
||||
val result: WritableMap = Arguments.createMap()
|
||||
val keys = obj.keys()
|
||||
|
||||
while (keys.hasNext()) {
|
||||
val key = keys.next()
|
||||
val value = obj.get(key)
|
||||
|
||||
when (value) {
|
||||
is JSONObject -> result.putMap(key, fromJSONObject(value))
|
||||
is JSONArray -> result.putArray(key, fromJSONArray(value))
|
||||
is String -> result.putString(key, value)
|
||||
is Boolean -> result.putBoolean(key, value)
|
||||
is Int -> result.putInt(key, value)
|
||||
is Double -> result.putDouble(key, value)
|
||||
is Long -> result.putInt(key, value.toInt())
|
||||
else ->
|
||||
if (obj.isNull(key)) {
|
||||
result.putNull(key)
|
||||
} else {
|
||||
throw JSONException("Unexpected value when parsing JSON object. key: $key")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse String of JSON object to ReadableMap
|
||||
*
|
||||
* @param objStr The String JSON object to be parsed
|
||||
* @return readableMap from the JSONArray
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(JSONException::class)
|
||||
public fun fromJSONObjectString(objStr: String): ReadableMap {
|
||||
return fromJSONObject(JSONObject(objStr))
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JSONArray to ReadableArray
|
||||
*
|
||||
* @param arr The JSONArray to be parsed
|
||||
* @return readableArray from the JSONArray
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(JSONException::class)
|
||||
public fun fromJSONArray(arr: JSONArray): ReadableArray {
|
||||
val result: WritableArray = Arguments.createArray()
|
||||
|
||||
for (i in 0 until arr.length()) {
|
||||
val value = arr.get(i)
|
||||
|
||||
when (value) {
|
||||
is JSONObject -> result.pushMap(fromJSONObject(value))
|
||||
is JSONArray -> result.pushArray(fromJSONArray(value))
|
||||
is String -> result.pushString(value)
|
||||
is Boolean -> result.pushBoolean(value)
|
||||
is Int -> result.pushInt(value)
|
||||
is Double -> result.pushDouble(value)
|
||||
is Long -> result.pushInt(value.toInt())
|
||||
else ->
|
||||
if (arr.isNull(i)) {
|
||||
result.pushNull()
|
||||
} else {
|
||||
throw JSONException("Unexpected value when parsing JSON array. index: $i")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse String of JSON array to ReadableArray
|
||||
*
|
||||
* @param arrStr The String JSON array to be parsed
|
||||
* @return readableArray from the JSONArray
|
||||
*/
|
||||
@JvmStatic
|
||||
@Throws(JSONException::class)
|
||||
public fun fromJSONArrayString(arrStr: String): ReadableArray {
|
||||
return fromJSONArray(JSONArray(arrStr))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user