Search in sources :

Example 91 with JSONArray

use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.

the class WXBridgeManager method callNative.

/**
   * Dispatch the native task to be executed.
     *
   * @param instanceId {@link WXSDKInstance#mInstanceId}
   * @param tasks tasks to be executed
   * @param callback next tick id
   */
public int callNative(String instanceId, String tasks, String callback) {
    if (TextUtils.isEmpty(tasks)) {
        if (WXEnvironment.isApkDebugable()) {
            WXLogUtils.e("[WXBridgeManager] callNative: call Native tasks is null");
        }
        commitJSBridgeAlarmMonitor(instanceId, WXErrorCode.WX_ERR_INVOKE_NATIVE, "[WXBridgeManager] callNative: call Native tasks is null");
        return IWXBridge.INSTANCE_RENDERING_ERROR;
    }
    if (WXEnvironment.isApkDebugable()) {
        mLodBuilder.append("[WXBridgeManager] callNative >>>> instanceId:").append(instanceId).append(", tasks:").append(tasks).append(", callback:").append(callback);
        WXLogUtils.d(mLodBuilder.substring(0));
        mLodBuilder.setLength(0);
    }
    if (mDestroyedInstanceId != null && mDestroyedInstanceId.contains(instanceId)) {
        return IWXBridge.DESTROY_INSTANCE;
    }
    long start = System.currentTimeMillis();
    JSONArray array = JSON.parseArray(tasks);
    if (WXSDKManager.getInstance().getSDKInstance(instanceId) != null) {
        WXSDKManager.getInstance().getSDKInstance(instanceId).jsonParseTime(System.currentTimeMillis() - start);
    }
    int size = array.size();
    if (size > 0) {
        try {
            JSONObject task;
            for (int i = 0; i < size; ++i) {
                task = (JSONObject) array.get(i);
                if (task != null && WXSDKManager.getInstance().getSDKInstance(instanceId) != null) {
                    Object target = task.get(MODULE);
                    if (target != null) {
                        if (WXDomModule.WXDOM.equals(target)) {
                            WXDomModule dom = getDomModule(instanceId);
                            dom.callDomMethod(task);
                        } else {
                            WXModuleManager.callModuleMethod(instanceId, (String) target, (String) task.get(METHOD), (JSONArray) task.get(ARGS));
                        }
                    } else if (task.get(COMPONENT) != null) {
                        //call component
                        WXDomModule dom = getDomModule(instanceId);
                        dom.invokeMethod((String) task.get(REF), (String) task.get(METHOD), (JSONArray) task.get(ARGS));
                    } else {
                        throw new IllegalArgumentException("unknown callNative");
                    }
                }
            }
        } catch (Exception e) {
            WXLogUtils.e("[WXBridgeManager] callNative exception: ", e);
            commitJSBridgeAlarmMonitor(instanceId, WXErrorCode.WX_ERR_INVOKE_NATIVE, "[WXBridgeManager] callNative exception " + e.getCause());
        }
    }
    if (UNDEFINED.equals(callback) || NON_CALLBACK.equals(callback)) {
        return IWXBridge.INSTANCE_RENDERING_ERROR;
    }
    // get next tick
    getNextTick(instanceId, callback);
    return IWXBridge.INSTANCE_RENDERING;
}
Also used : WXDomModule(com.taobao.weex.dom.WXDomModule) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) WXException(com.taobao.weex.common.WXException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WXRuntimeException(com.taobao.weex.common.WXRuntimeException)

Example 92 with JSONArray

use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.

the class WXBridge method callNativeModule.

/**
   * Bridge module Js Method
   * support Sync or Async through setting  Annotation as {@link com.taobao.weex.annotation.JSMethod }
   * @param instanceId  Instance ID
   * @param module  the name of module
   * @param method  the name of method
   * @param arguments  the arguments of the method
   * @param options  option arguments for extending
   * @return  the result
   */
@Override
public Object callNativeModule(String instanceId, String module, String method, byte[] arguments, byte[] options) {
    JSONArray argArray = JSON.parseArray(new String(arguments));
    Object object = WXBridgeManager.getInstance().callNativeModule(instanceId, module, method, argArray, options);
    return new WXJSObject(object);
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 93 with JSONArray

use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.

the class WXDomObject method parse.

/**
   * Parse the jsonObject to {@link WXDomObject} recursively
   * @param json the original JSONObject
   * @return Dom Object corresponding to the JSONObject.
   */
@Nullable
public static WXDomObject parse(JSONObject json, WXSDKInstance wxsdkInstance) {
    if (json == null || json.size() <= 0) {
        return null;
    }
    String type = (String) json.get(TYPE);
    WXDomObject domObject = WXDomObjectFactory.newInstance(type);
    domObject.setViewPortWidth(wxsdkInstance.getViewPortWidth());
    if (domObject == null) {
        return null;
    }
    domObject.parseFromJson(json);
    domObject.mDomContext = wxsdkInstance;
    Object children = json.get(CHILDREN);
    if (children != null && children instanceof JSONArray) {
        JSONArray childrenArray = (JSONArray) children;
        int count = childrenArray.size();
        for (int i = 0; i < count; ++i) {
            domObject.add(parse(childrenArray.getJSONObject(i), wxsdkInstance), -1);
        }
    }
    return domObject;
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) Nullable(android.support.annotation.Nullable)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)93 JSONObject (com.alibaba.fastjson.JSONObject)37 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)8 List (java.util.List)7 StringReader (java.io.StringReader)6 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)5 Reader (java.io.Reader)5 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)3 Setting (org.aisen.android.common.setting.Setting)3 JSONReader (com.alibaba.fastjson.JSONReader)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2