Skip to content

[RN 0.82] Vendor JSONArguments to JSONToWritableMa #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions android/src/main/java/com/wearconnectivity/JSONToWritableMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.wearconnectivity;

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONToWritableMap {

/**
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.JSONArguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
Expand Down Expand Up @@ -67,7 +66,7 @@ public void sendMessage(ReadableMap messageData, List<Node> connectedNodes, Call
public void onMessageReceived(@NonNull MessageEvent messageEvent) {
try {
JSONObject jsonObject = new JSONObject(messageEvent.getPath());
WritableMap messageAsWritableMap = (WritableMap) JSONArguments.fromJSONObject(jsonObject);
WritableMap messageAsWritableMap = (WritableMap) JSONToWritableMap.fromJSONObject(jsonObject);
String event = jsonObject.getString("event");
FLog.w(TAG, TAG + " event: " + event + " message: " + messageAsWritableMap);
Intent service = new Intent(reactContext, com.wearconnectivity.WearConnectivityTask.class);
Expand Down Expand Up @@ -118,4 +117,4 @@ private void sendMessageToClient(ReadableMap messageData, Node node, Callback re
errorCb.invoke("sendMessage failed: " + e);
}
}
}
}