Example usage for com.google.gson JsonObject JsonObject

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

Introduction

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

Prototype

JsonObject

Source Link

Usage

From source file:clientcommunicator.Server.Cookie.java

public void setUser(User userToBeSet) {
    JsonObject resultingCookie = new JsonObject();
    resultingCookie.addProperty("name", userToBeSet.getUsername());
    resultingCookie.addProperty("password", userToBeSet.getPassword());
    resultingCookie.addProperty("playerID", userToBeSet.getPlayerId());
    try {//www  . j av  a2  s  .c  o  m
        this.getUserJsonObject(resultingCookie, true);
    } catch (MalformedCookieException ex) {
        Logger.getLogger(Cookie.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:cloudlens.cli.Whisk.java

License:Apache License

public static JsonObject main(JsonObject args) {
    final JsonObject res = new JsonObject();

    try {/*from   w  w  w .  j  a  v a2 s .co m*/
        final CL cl = new CL(System.out, System.err, false, true);

        String script = "";
        String logURI = "";

        if (args.has("script")) {
            script = args.getAsJsonPrimitive("script").getAsString();
        }
        if (args.has("log")) {
            logURI = args.getAsJsonPrimitive("log").getAsString();
        }

        try {
            final InputStream inputStream = FileReader.fetchFile(logURI);
            cl.source(inputStream);
            final List<ASTElement> top = ASTBuilder.parse(script);
            cl.launch(top);

            final JsonElement jelement = new JsonParser().parse(cl.cl.get("result").asString());
            return jelement.getAsJsonObject();

        } catch (final CLException e) {
            res.addProperty("error", e.getMessage());
            System.err.println(e.getMessage());
        } finally {
            cl.outWriter.flush();
            cl.errWriter.flush();
        }

    } catch (final Exception e) {
        res.addProperty("error", e.getMessage());
        System.err.println(e.getMessage());
    }

    return res;
}

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 {/*w ww  .j  av  a2 s. co 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

/**
 * /*from ww  w. j av a 2 s.  c  om*/
 * @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// w  ww.ja  v a2  s . co m
 * @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();//from  w w  w .j av  a  2 s .com
    return p;
}

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  w w  .  j a v  a  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;//w w w. j a v a2s  .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;
}