Example usage for com.google.gson JsonParser JsonParser

List of usage examples for com.google.gson JsonParser JsonParser

Introduction

In this page you can find the example usage for com.google.gson JsonParser JsonParser.

Prototype

@Deprecated
public JsonParser() 

Source Link

Usage

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String getErrorInfo(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;/*from  w w w  .j  a v  a  2 s.c om*/
    try {
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }
    if (jo.has("errorInfo")) {
        String value = jo.getAsJsonPrimitive("errorInfo").getAsString();
        return value;
    } else {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static int getErrorCode(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;//from   w w w . j a v  a  2s .  co  m
    try {
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }
    if (jo.has("errorCode")) {
        String value = jo.getAsJsonPrimitive("errorCode").getAsString();
        return Integer.parseInt(value);
    } else {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static JsonObject getParams(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;/*from   w  ww. jav a2s . c  o  m*/
    try {
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    if (jo.has("params")) {
        return jo.getAsJsonObject("params");
    } else {
        return null;
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildSignedParams(String p, String signKey) throws CrossException {
    String md5Value = Security.crossingSign(p, signKey, "");
    JsonObject jo = new JsonObject();
    jo.addProperty("sign", md5Value);
    JsonParser jp = new JsonParser();
    JsonElement jop = null;//w ww.j  av a2s  .c  o  m
    try {
        jop = jp.parse(p);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    jo.add("params", jop);
    return jo.toString();
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String checkSignAndRemove(String p, String signKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;//from   w w w.jav a  2  s .c om
    try {
        jop = jp.parse(p);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("sign") || jo.has("params")) {
        String sign = jo.getAsJsonPrimitive("sign").getAsString();
        np = jo.getAsJsonObject("params").toString();
        String signValue = Security.crossingSign(np, signKey, "");
        if (!sign.equals(signValue)) {
            throw new CrossException(ErrorCode.COMMON_ERR_SIGN_BAD.getCode(),
                    ErrorCode.COMMON_ERR_SIGN_BAD.getInfo());
        }

        //remove sign field only and return
        Iterator<Entry<String, JsonElement>> it = jo.entrySet().iterator();
        JsonObject rjo = new JsonObject();
        Entry<String, JsonElement> en;
        while (it.hasNext()) {
            en = it.next();
            if (!en.getKey().equals("sign")) {
                rjo.add(en.getKey(), en.getValue());
            }
        }
        return rjo.toString();
    }

    return p;
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildDecryptedParams(String encryptParams, String decryptKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;/* ww w  . ja va 2s . c om*/
    try {
        jop = jp.parse(encryptParams);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("encrypted")) {
        String encrypted = jo.getAsJsonPrimitive("encrypted").getAsString();
        String paramsValue = Security.crossingDecrypt(encrypted, decryptKey, "DES");
        int ec = jo.getAsJsonPrimitive("errorCode").getAsInt();
        String ed = jo.getAsJsonPrimitive("errorInfo").getAsString();
        JsonParser jpr = new JsonParser();
        JsonObject jor = null;
        try {
            jor = (JsonObject) jpr.parse(paramsValue);
        } catch (JsonSyntaxException je) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
        jor.addProperty("errorCode", ec);
        jor.addProperty("errorInfo", ed);
        np = jor.toString();
        return np;
    }

    return encryptParams;
}

From source file:club.jmint.mifty.service.MiftyService.java

License:Apache License

public String callProxy(String method, String params, boolean isEncrypt) throws TException {
    //parse parameters and verify signature
    CrossLog.logger.debug("callProxy: " + method + "(in: " + params + ")");
    JsonObject ip;/*from www  .  j a  v a 2 s  .co m*/
    try {
        ip = parseInputParams(params, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //extract all parameters
    HashMap<String, String> inputMap = new HashMap<String, String>();
    Iterator<Entry<String, JsonElement>> it = ip.entrySet().iterator();
    Entry<String, JsonElement> en = null;
    String key;
    JsonElement je;
    while (it.hasNext()) {
        en = it.next();
        key = en.getKey();
        je = en.getValue();
        if (je.isJsonArray()) {
            inputMap.put(key, je.getAsJsonArray().toString());
            //System.out.println(je.getAsJsonArray().toString());
        } else if (je.isJsonNull()) {
            inputMap.put(key, je.getAsJsonNull().toString());
            //System.out.println(je.getAsJsonNull().toString());
        } else if (je.isJsonObject()) {
            inputMap.put(key, je.getAsJsonObject().toString());
            //System.out.println(je.getAsJsonObject().toString());
        } else if (je.isJsonPrimitive()) {
            inputMap.put(key, je.getAsJsonPrimitive().getAsString());
            //System.out.println(je.getAsJsonPrimitive().toString());
        } else {
            //unkown type;
        }
    }

    //execute specific method
    Method[] ma = this.getClass().getMethods();
    int idx = 0;
    for (int i = 0; i < ma.length; i++) {
        if (ma[i].getName().equals(method)) {
            idx = i;
            break;
        }
    }
    HashMap<String, String> outputMap = null;
    try {
        Method m = this.getClass().getDeclaredMethod(method, ma[idx].getParameterTypes());
        outputMap = (HashMap<String, String>) m.invoke(this, inputMap);
        CrossLog.logger.debug("callProxy: " + method + "() executed.");
    } catch (NoSuchMethodException nsm) {
        CrossLog.logger.error("callProxy: " + method + "() not found.");
        CrossLog.printStackTrace(nsm);
        return buildOutputError(ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getCode(),
                ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getInfo());
    } catch (Exception e) {
        CrossLog.logger.error("callProxy: " + method + "() executed with exception.");
        CrossLog.printStackTrace(e);
        if (e instanceof CrossException) {
            return buildOutputByCrossException((CrossException) e);
        } else {
            return buildOutputError(ErrorCode.COMMON_ERR_UNKOWN.getCode(),
                    ErrorCode.COMMON_ERR_UNKOWN.getInfo());
        }
    }
    //if got error then return immediately
    String ec = outputMap.get("errorCode");
    String ecInfo = outputMap.get("errorInfo");
    if ((ec != null) && (!ec.isEmpty())) {
        return buildOutputError(Integer.parseInt(ec), ecInfo);
    }

    //build response parameters
    JsonObject op = new JsonObject();
    Iterator<Entry<String, String>> ito = outputMap.entrySet().iterator();
    Entry<String, String> eno = null;
    JsonParser jpo = new JsonParser();
    JsonElement jeo = null;
    while (ito.hasNext()) {
        eno = ito.next();
        try {
            jeo = jpo.parse(eno.getValue());
        } catch (JsonSyntaxException e) {
            //            MyLog.logger.error("callProxy: Malformed output parameters["+eno.getKey()+"].");
            //            return buildOutputError(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), 
            //                  ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
            //we do assume that it should be a common string
            jeo = new JsonPrimitive(eno.getValue());
        }
        op.add(eno.getKey(), jeo);
    }

    String output = null;
    try {
        output = buildOutputParams(op, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    CrossLog.logger.debug("callProxy: " + method + "(out: " + output + ")");
    return output;
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleSpecialty(String json, MessageBean response) {
    try {//from   ww  w. j a  v  a 2 s.c o m
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String specialtyIds = jsonObject.get("specialtyId").getAsString();
        int interval = jsonObject.get("interval").getAsInt();

        ResponseT1<SpecialtyResponseBean> resp = new ResponseT1<>();

        if (TextUtils.isEmpty(specialtyIds)) {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            resp.setInfo("?");
        } else {

            ArrayList<String> paths = new ArrayList<>();
            String failedIds = null;
            if ("-1".equals(specialtyIds)) {
                List<RstrSpecialty> specialties = DBHelper.get(mContext).findSpecialtyByWhere(null, null);

                if (specialties != null && specialties.size() > 0) {
                    for (RstrSpecialty specialty : specialties) {
                        paths.add(specialty.getMedia_path());
                    }
                }
            } else {
                String[] ids = specialtyIds.split(",");
                failedIds = "";
                for (int i = 0; i < ids.length; i++) {
                    String id = ids[i].trim();

                    String selection = DBHelper.MediaDBInfo.FieldName.FOOD_ID + "=?";
                    String[] selectionArgs = new String[] { id };
                    List<RstrSpecialty> specialties = DBHelper.get(mContext).findSpecialtyByWhere(selection,
                            selectionArgs);

                    if (specialties != null && specialties.size() > 0) {
                        paths.add(specialties.get(0).getMedia_path());
                    } else {
                        failedIds += id + ",";
                    }
                }
                if (!TextUtils.isEmpty(failedIds)) {
                    failedIds = failedIds.substring(0, failedIds.length() - 1);
                }
            }

            if (paths.size() > 0) {
                if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                        || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                        || GlobalValues.IS_RSTR_PROJECTION) {
                    boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

                    GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
                    GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
                    GlobalValues.IS_RSTR_PROJECTION = true;
                    GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
                    AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

                    SpecialtyResponseBean specialtyResponseBean = new SpecialtyResponseBean();
                    specialtyResponseBean.setFounded_count(paths.size());
                    specialtyResponseBean.setFailed_ids(failedIds);
                    resp.setContent(specialtyResponseBean);
                    if (TextUtils.isEmpty(failedIds)) {
                        resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
                        resp.setInfo("??");
                    } else {
                        resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SPECIALTY_INCOMPLETE);
                        resp.setInfo(failedIds);
                    }
                    ProjectOperationListener.getInstance(mContext).showSpecialty(paths, interval, isNewDevice);
                } else {
                    resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
                    if (GlobalValues.IS_LOTTERY) {
                        resp.setInfo(
                                "?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
                    } else {
                        resp.setInfo(
                                "?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
                    }
                }
            } else {
                resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
                resp.setInfo("??");
            }
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleGreeting(String json, MessageBean response) {
    try {/*from  w w  w .  ja v  a 2  s.  c o  m*/
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String words = jsonObject.get("word").getAsString();
        int template = jsonObject.get("templateId").getAsInt();

        ResponseT1 resp = new ResponseT1();
        if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID) || GlobalValues.IS_RSTR_PROJECTION) {
            boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

            GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
            GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
            GlobalValues.IS_RSTR_PROJECTION = true;
            GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
            AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
            resp.setInfo("??");

            ProjectOperationListener.getInstance(mContext).showGreeting(words, template, 1000 * 60 * 5,
                    isNewDevice);

            //                Intent intent = new Intent(mContext, GreetingService.class);
            //                intent.putExtra(GreetingService.EXTRA_DEVICE_ID, deviceId);
            //                intent.putExtra(GreetingService.EXTRA_DEVICE_NAME, deviceName);
            //                intent.putExtra(GreetingService.EXTRA_WORDS, words);
            //                intent.putExtra(GreetingService.EXTRA_TEMPLATE, template);
            //                try {
            //                    mContext.unbindService(connection);
            //                } catch (Exception e) {
            //                    e.printStackTrace();
            //                }
            //                mContext.bindService(intent, connection, Service.BIND_AUTO_CREATE);
        } else {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            if (GlobalValues.IS_LOTTERY) {
                resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
            } else {
                resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
            }
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleAdv(String json, MessageBean response) {
    try {// w  ww  .j a  v a 2  s. c  o  m
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String videoIds = jsonObject.get("vid").getAsString();

        String[] ids = videoIds.split(",");
        String failedIds = "";
        ArrayList<String> paths = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            String id = ids[i].trim();

            String selection = null;
            String[] selectionArgs = null;
            if (!"-1".equals(id)) {
                selection = DBHelper.MediaDBInfo.FieldName.VID + "=?";
                selectionArgs = new String[] { id };
            }
            List<MediaLibBean> videos = DBHelper.get(mContext).findPlayListByWhere(selection, selectionArgs);

            if (videos != null && videos.size() > 0) {
                paths.add(videos.get(0).getMediaPath());
            } else {
                failedIds += id + ",";
            }
        }

        if (!TextUtils.isEmpty(failedIds)) {
            failedIds = failedIds.substring(0, failedIds.length() - 1);
        }

        ResponseT1<AdvResponseBean> resp = new ResponseT1();
        if (paths.size() > 0) {
            if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                    || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                    || GlobalValues.IS_RSTR_PROJECTION) {
                boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

                GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
                GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
                GlobalValues.IS_RSTR_PROJECTION = true;
                GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
                AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

                AdvResponseBean advResponseBean = new AdvResponseBean();
                advResponseBean.setFailed_ids(failedIds);
                resp.setContent(advResponseBean);
                if (TextUtils.isEmpty(failedIds)) {
                    resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
                    resp.setInfo("??");
                } else {
                    resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SPECIALTY_INCOMPLETE);
                    resp.setInfo(failedIds);
                }
                ProjectOperationListener.getInstance(mContext).showAdv(paths, isNewDevice);
            } else {
                resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
                if (GlobalValues.IS_LOTTERY) {
                    resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
                } else {
                    resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
                }
            }
        } else {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            resp.setInfo("?");
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}