Example usage for com.google.gson JsonObject has

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

Introduction

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

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

From source file:clientcommunicator.operations.CreateGameRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    if (!obj.has("randomTiles") || !obj.has("randomNumbers") || !obj.has("randomPorts") || !obj.has("name")) {
        throw new JSONException("Malformed CreateGameRequest: missing field");
    }//  w  ww.j  a v a 2  s .c o  m
    String randomTilesJSON = obj.get("randomTiles").getAsString();
    randomTilesJSON = randomTilesJSON.toLowerCase();
    if (randomTilesJSON.equals("true") && randomTilesJSON.equals("false") && randomTilesJSON.equals("1")
            && randomTilesJSON.equals("0")) {
        throw new JSONException("Malformed CreateGameRequest: invalid randomTiles");
    }
    String randomNumbersJSON = obj.get("randomNumbers").getAsString();
    randomNumbersJSON = randomNumbersJSON.toLowerCase();
    if (randomNumbersJSON.equals("true") && randomNumbersJSON.equals("false") && randomNumbersJSON.equals("1")
            && randomNumbersJSON.equals("0")) {
        throw new JSONException("Malformed CreateGameRequest: invalid randomNumbers");
    }
    String randomPortsJSON = obj.get("randomPorts").getAsString();
    randomPortsJSON = randomPortsJSON.toLowerCase();
    if (randomPortsJSON.equals("true") && randomPortsJSON.equals("false") && randomPortsJSON.equals("1")
            && randomPortsJSON.equals("0")) {
        throw new JSONException("Malformed CreateGameRequest: invalid randomPorts");
    }
    this.randomTiles = obj.get("randomTiles").getAsBoolean();
    this.randomNumbers = obj.get("randomNumbers").getAsBoolean();
    this.randomPorts = obj.get("randomPorts").getAsBoolean();
    this.name = obj.get("name").getAsString();
}

From source file:clientcommunicator.operations.JoinGameRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    if (!obj.has("id") || !obj.has("color")) {
        throw new JSONException("Malformed JoinGameRequest: missing field");
    }//  w  w  w  .jav  a 2s  .com
    String color = obj.get("color").getAsString().toUpperCase();
    if (!"RED".equals(color) && !"ORANGE".equals(color) && !"YELLOW".equals(color) && !"BLUE".equals(color)
            && !"GREEN".equals(color) && !"PURPLE".equals(color) && !"PUCE".equals(color)
            && !"WHITE".equals(color) && !"BROWN".equals(color)) {
        throw new JSONException("Malformed JoinGameRequest");
    }
    this.playerColor = CatanColor.valueOf(color);
    this.gameId = obj.get("id").getAsInt();
}

From source file:clientcommunicator.Server.Cookie.java

private JsonObject getUserJsonObject(JsonElement element, boolean setNew) throws MalformedCookieException {
    JsonObject jobject = element.getAsJsonObject();
    if (!jobject.has("playerID")) {
        throw new MalformedCookieException();
    }//from   ww w. j  a  v a 2 s.  c  om
    JsonObject resultingCookie = new JsonObject();
    resultingCookie.add("name", jobject.get("name"));
    resultingCookie.add("password", jobject.get("password"));
    resultingCookie.add("playerID", jobject.get("playerID"));
    if (setNew) {
        try {
            String realCookie = resultingCookie.toString();
            realCookie = java.net.URLEncoder.encode(realCookie, "UTF-8");
            this.userInformationString = realCookie;
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Cookie.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return jobject;
}

From source file:cloudlens.cli.Whisk.java

License:Apache License

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

    try {/*from   www .j a va  2 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.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 .  co  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.specs.CrossingService.java

License:Apache License

protected JsonObject parseInputParams(String params, boolean encrypt) throws CrossException {
    //parsing/* w w w.j  a v a2  s  .c o m*/
    JsonParser jp = new JsonParser();
    JsonObject jo, jode, joparams;
    try {
        jo = (JsonObject) jp.parse(params);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    String encrypted, jsonParams;
    if (encrypt) {
        if (!jo.has("encrypted")) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }
        encrypted = jo.getAsJsonPrimitive("encrypted").getAsString();
        jsonParams = buildDecrypt(encrypted);
        if (jsonParams == null) {
            throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(),
                    ErrorCode.COMMON_ERR_DECRYPTION.getInfo());
        }
        try {
            jode = (JsonObject) jp.parse(jsonParams);
        } catch (JsonSyntaxException e) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
        jo = jode;
    }

    //Check signature
    if (!jo.has("sign")) {
        throw new CrossException(ErrorCode.COMMON_ERR_SIGN_MISSING.getCode(),
                ErrorCode.COMMON_ERR_SIGN_MISSING.getInfo());
    }
    String signValue = jo.getAsJsonPrimitive("sign").getAsString();
    if (!jo.has("params")) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
    String inputparams = jo.getAsJsonObject("params").toString();

    String signCheck = buildSign(inputparams, signKey);
    if (!signCheck.equals(signValue)) {
        throw new CrossException(ErrorCode.COMMON_ERR_SIGN_BAD.getCode(),
                ErrorCode.COMMON_ERR_SIGN_BAD.getInfo());
    }

    try {
        joparams = (JsonObject) jp.parse(inputparams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    return joparams;
}

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

License:Apache License

protected String getParamAsString(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }/*from  www  .  ja  va2s .  c o m*/
        return params.getAsJsonPrimitive(key).getAsString();
    }
    return null;
}

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

License:Apache License

protected int getParamAsInt(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }/* www  . ja  v  a2 s .c  om*/
        return params.getAsJsonPrimitive(key).getAsInt();
    }
    return -1;
}

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

License:Apache License

protected boolean getParamAsBoolean(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }/*  w w w.  ja  v a 2  s.  c  o m*/
        return params.getAsJsonPrimitive(key).getAsBoolean();
    }
    return false;
}

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

License:Apache License

protected long getParamAsLong(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }//  w w w .ja  v a 2s  .c o  m
        return params.getAsJsonPrimitive(key).getAsLong();
    }
    return -1;
}