Skip to content
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

[BUG] 🚀 sdk init or send image and text will be happen error (about json exceptions) 🚀 #32

Open
lgz5689 opened this issue Aug 14, 2024 · 3 comments · Fixed by #33
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@lgz5689
Copy link
Member

lgz5689 commented Aug 14, 2024

OpenIM Server Version

3.8.0 later

Operating System and CPU Architecture

macOS (ARM)

Deployment Method

Source Code Deployment

Bug Description and Steps to Reproduce

Errors may occur:

  • init sdk : ReactNativeJS com.sass_crm_rn_cn I 'initSDK error', [Error: please check params and dir]
  • init sdk : promise.reject("-1", "please check params and dir")
  • send message: Error: json: cannot unmarshal number 1.723458869536E12 into Go struct field MsgStruct.createTime of type int64

Screenshots Link

@lgz5689 lgz5689 added the bug Something isn't working label Aug 14, 2024
@lgz5689 lgz5689 pinned this issue Aug 14, 2024
@lgz5689
Copy link
Member Author

lgz5689 commented Aug 14, 2024

🌟 solution 🌟

if you is react-native": "0.73.6" later , can be used normally. otherwise you can refer to the following modification.

Ps: Due to the error caused by such modification in version 0.73.6, the sdk uses map.toString by default. If compatibility problems occur, you can modify the source code yourself or use patch-packageto fix them.

@lgz5689 lgz5689 added documentation Improvements or additions to documentation and removed bug Something isn't working labels Aug 14, 2024
@lgz5689 lgz5689 changed the title [BUG] sdk init or send image and text will be happen error (about json exceptions) [BUG] 🌟 sdk init or send image and text will be happen error (about json exceptions) Aug 14, 2024
@lgz5689 lgz5689 changed the title [BUG] 🌟 sdk init or send image and text will be happen error (about json exceptions) [BUG] 🛠️ sdk init or send image and text will be happen error (about json exceptions) Aug 14, 2024
@lgz5689 lgz5689 changed the title [BUG] 🛠️ sdk init or send image and text will be happen error (about json exceptions) [BUG] 🚀 sdk init or send image and text will be happen error (about json exceptions) 🚀 Aug 14, 2024
@lgz5689 lgz5689 added the bug Something isn't working label Aug 14, 2024
@lgz5689 lgz5689 reopened this Aug 14, 2024
@lgz5689
Copy link
Member Author

lgz5689 commented Aug 14, 2024

updated to the latest 3.8.1-rc.1

@OrrinHatch
Copy link
Contributor

OrrinHatch commented Sep 12, 2024

react native 0.70 version:
in node_modules/open-im-sdk-rn/android/src/main/java/com/openimsdkrn/utils add RNJSONUtils.java

RNJSONUtils.java

/**
 *     Copyright 2016 IBM Corp.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.openimsdkrn.utils;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class RNJSONUtils {

  public static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
    WritableMap map = new WritableNativeMap();

    Iterator<String> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
      String key = iterator.next();
      Object value = jsonObject.get(key);
      if (value instanceof JSONObject) {
        map.putMap(key, convertJsonToMap((JSONObject) value));
      } else if (value instanceof JSONArray) {
        map.putArray(key, convertJsonToArray((JSONArray) value));
      } else if (value instanceof Boolean) {
        map.putBoolean(key, (Boolean) value);
      } else if (value instanceof Integer) {
        map.putInt(key, (Integer) value);
      } else if (value instanceof Double) {
        map.putDouble(key, (Double) value);
      } else if (value instanceof String) {
        map.putString(key, (String) value);
      } else {
        map.putString(key, value.toString());
      }
    }
    return map;
  }

  public static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException {
    WritableArray array = new WritableNativeArray();

    for (int i = 0; i < jsonArray.length(); i++) {
      Object value = jsonArray.get(i);
      if (value instanceof JSONObject) {
        array.pushMap(convertJsonToMap((JSONObject) value));
      } else if (value instanceof JSONArray) {
        array.pushArray(convertJsonToArray((JSONArray) value));
      } else if (value instanceof Boolean) {
        array.pushBoolean((Boolean) value);
      } else if (value instanceof Integer) {
        array.pushInt((Integer) value);
      } else if (value instanceof Double) {
        array.pushDouble((Double) value);
      } else if (value instanceof String) {
        array.pushString((String) value);
      } else {
        array.pushString(value.toString());
      }
    }
    return array;
  }

  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;
      }
    }
    return object;
  }

  public static JSONArray convertArrayToJson(ReadableArray readableArray) throws JSONException {
    JSONArray array = new JSONArray();
    for (int i = 0; i < readableArray.size(); i++) {
      switch (readableArray.getType(i)) {
        case Null:
          break;
        case Boolean:
          array.put(readableArray.getBoolean(i));
          break;
        case Number:
          array.put(readableArray.getDouble(i));
          break;
        case String:
          array.put(readableArray.getString(i));
          break;
        case Map:
          array.put(convertMapToJson(readableArray.getMap(i)));
          break;
        case Array:
          array.put(convertArrayToJson(readableArray.getArray(i)));
          break;
      }
    }
    return array;
  }
}

in node_modules/open-im-sdk-rn/android/src/main/java/com/openimsdkrn/OpenImSdkRnModule.java

import com.openimsdkrn.utils.RNJSONUtils;

 private String readableMap2string(ReadableMap map) {
    try {
      String jsonString = RNJSONUtils.convertMapToJson(map).toString();
      return jsonString;
    } catch (Exception e) {
        e.printStackTrace();
        return map.toString();
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants