Example usage for com.google.gson JsonObject toString

List of usage examples for com.google.gson JsonObject toString

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:club.jmint.crossing.client.CrossingClient.java

License:Apache License

public CallResult serviceCall(String inf, JsonObject params, boolean isEncrypt) {
    String result = null;//from w ww  .j  a v a 2  s . c o  m
    CallResult cr = new CallResult();
    try {
        if (isEncrypt) {
            result = call(inf, params.toString(), true);
        } else {
            result = call(inf, params.toString());
        }
    } catch (CrossException e) {
        CrossLog.printStackTrace(e);
        CrossLog.logger.error("service call failed.");
        return null;
    }

    JsonParser jp = new JsonParser();
    JsonObject jo;
    try {
        jo = (JsonObject) jp.parse(result);
    } catch (JsonSyntaxException e) {
        CrossLog.printStackTrace(e);
        CrossLog.logger.error("service call failed.");
        return null;
    }

    cr.errorCode = jo.getAsJsonPrimitive("errorCode").getAsInt();
    if (cr.errorCode == ErrorCode.SUCCESS.getCode()) {
        cr.isSuccess = true;
    } else {
        cr.isSuccess = false;
    }
    cr.errorInfo = jo.getAsJsonPrimitive("errorInfo").getAsString();
    if (jo.has("params")) {
        cr.data = jo.getAsJsonObject("params");
    }

    return cr;
}

From source file:club.jmint.crossing.client.DemoClient.java

License:Apache License

public static void main(String[] args) {
    //Create Crossing client call proxy
    CrossingClient cc = CrossingClient.getInstance();

    //Start Crossing Client
    try {/*ww w .j av  a 2s. c o  m*/
        cc.startup();
    } catch (Exception e) {
        System.exit(-1);
    }
    System.out.println("Starting to run example calls.");

    JsonObject pp4 = new JsonObject();
    pp4.addProperty("type", 4);
    pp4.addProperty("fields",
            "user_id,user_true_name,status,pay_status,product_id,product_name,ticket_code,ticket_code_url");
    pp4.addProperty("user_id", "1000000005");
    System.out.println(pp4.toString());

    CallResult result = cc.serviceCall(
            "TwohalfServer@com.twohalf.mifty.service.gen.OrderService@orderQueryByUser", pp4, false);
    if (result != null && result.isSuccess) {
        System.out.println(result.data);
    }

    //Stop Crossing Client
    cc.shutdown();
}

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

License:Apache License

/**
 * //from   w w  w.  ja v a2s.  c  o m
 * @param errorCode
 * @param errorInfo
 * @return
 */
protected String buildOutputError(int errorCode, String errorInfo) {
    JsonObject jo = new JsonObject();
    jo.addProperty("errorCode", errorCode);
    jo.addProperty("errorInfo", errorInfo);
    return jo.toString();
}

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

License:Apache License

protected String buildOutputError(CrossException ce) {
    JsonObject jo = new JsonObject();
    jo.addProperty("errorCode", ce.getErrorCode());
    jo.addProperty("errorInfo", ce.getErrorInfo());
    return jo.toString();
}

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

License:Apache License

/**
 * /*ww  w  .jav  a 2 s . co m*/
 * @param obj
 * @param encrypt
 * @return
 */
protected String buildOutputParams(JsonObject obj, boolean encrypt) throws CrossException {
    if (obj == null || obj.toString().isEmpty()) {
        //we do resume that it's successful without any response parameters.
        JsonObject jo = new JsonObject();
        jo.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        jo.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return jo.toString();
    }

    //create signature
    String op = obj.toString();
    String signValue = buildSign(op, signKey);

    JsonObject jo = new JsonObject();
    jo.addProperty("sign", signValue);
    jo.add("params", obj);

    String encrypted;
    if (encrypt) {
        //encrypt output
        encrypted = buildEncrypt(jo.toString());
        JsonObject joen = new JsonObject();
        joen.addProperty("encrypted", encrypted);
        joen.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        joen.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return joen.toString();
    } else {
        //non-encrypt output
        jo.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        jo.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return jo.toString();
    }

}

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

License:Apache License

/**
 * There exist these kinds of circumstances that we need return by given Errorcode with some output parameters
 * @param obj/*from   ww w. j a v  a 2  s.  c  om*/
 * @param encrypt
 * @param errorCode
 * @param errorInfo
 * @return
 */
protected String buildOutputParamsWithGivenErrorCode(JsonObject obj, boolean encrypt, int errorCode,
        String errorInfo) throws CrossException {
    if (obj == null) {
        return buildOutputError(errorCode, errorInfo);
    }

    //create signature
    String op = obj.toString();
    String signValue = buildSign(op, signKey);

    JsonObject jo = new JsonObject();
    jo.addProperty("sign", signValue);
    jo.addProperty("params", op);

    String encrypted;
    if (encrypt) {
        //encrypt output
        encrypted = buildEncrypt(jo.toString());
        JsonObject joen = new JsonObject();
        joen.addProperty("encrypted", encrypted);
        joen.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        joen.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return joen.toString();
    } else {
        //non-encrypt output
        jo.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        jo.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return jo.toString();
    }
}

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

License:Apache License

public static String createErrorParams(int errorcode, String errorinfo) {
    JsonObject jo = new JsonObject();
    jo.addProperty("errorCode", errorcode);
    jo.addProperty("errorInfo", errorinfo);
    String p = jo.toString();
    return p;/* w w w.ja  v a2  s  .c  o m*/
}

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;/*from   w w  w.  j  ava 2s  .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   www  .  ja va 2 s . co  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) {
        //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;//from  ww  w  . j a  v  a2  s  .  co m
    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;
}