Example usage for com.google.gson JsonObject getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void getLocation(POI poi, JsonObject jObject) {
    JsonObject json = jObject.getAsJsonObject(LOCATION);
    JsonArray array;/*from  w ww .ja v  a2s.co  m*/

    Location location = new Location();
    if (json.has(POINT)) {
        array = json.getAsJsonArray(POINT);
        getPoints(location, array);
    }

    if (json.has(LINE)) {
        array = json.getAsJsonArray(LINE);
        getLine(location, array);
    }

    if (json.has(POLYGON)) {
        array = json.getAsJsonArray(POLYGON);
        getSimplePolygon(location, array);
    }

    if (json.has(ADDRESS))
        location.setAddress(getPOIBaseType(json.getAsJsonObject(ADDRESS)));

    if (json.has(RELATIONSHIP)) {
        array = json.getAsJsonArray(RELATIONSHIP);
        getRelationships(location, array);
    }

    poi.setLocation(location);
}

From source file:client.getforgeid.java

License:Creative Commons License

/**
 * reads json data to get forge profile and forge id
 * @return the last used version id of forge
 * @throws IOException// w  w  w  .j a va 2  s  .  c  om
 */
public static String findForgeProfile() throws IOException {
    File configFile = getConfigFile();

    Gson gson = GsonUtil.make();
    JsonObject jobj;
    try (InputStream is = new FileInputStream(configFile); InputStreamReader isr = new InputStreamReader(is)) {
        jobj = gson.fromJson(isr, JsonElement.class).getAsJsonObject();
    }

    JsonObject profiles = jobj.getAsJsonObject("profiles");
    JsonObject forge = profiles.getAsJsonObject("Forge");
    if (forge == null)
        return null;

    return forge.getAsJsonPrimitive("lastVersionId").getAsString();
}

From source file:client.getforgeid.java

License:Creative Commons License

/**
 * gets last run minecraft version/*from   ww  w .  j a  va  2s . c o  m*/
 * @return returns the last run version id 
 * @throws IOException
 */
public static String findLastUsedMcVersion() throws IOException {
    File configFile = getConfigFile();

    Gson gson = GsonUtil.make();
    JsonObject jobj;
    try (InputStream is = new FileInputStream(configFile); InputStreamReader isr = new InputStreamReader(is)) {
        jobj = gson.fromJson(isr, JsonElement.class).getAsJsonObject();
    }

    String selectedprofile = jobj.getAsJsonPrimitive("selectedProfile").getAsString();

    JsonObject profiles = jobj.getAsJsonObject("profiles");
    JsonObject mc = profiles.getAsJsonObject(selectedprofile);
    if (mc == null)
        return null;

    String lastVersionID = mc.getAsJsonPrimitive("lastVersionId").getAsString();
    if (lastVersionID.length() > 6) {
        lastVersionID = lastVersionID.substring(0, 5); // TODO: work out
        // better way to get
        // mc version.
    }
    return lastVersionID;
}

From source file:clientcommunicator.operations.BuildRoadRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    JsonObject roadLocation = obj.getAsJsonObject("roadLocation");
    this.free = obj.get("free").getAsBoolean();
    int x = roadLocation.get("x").getAsInt();
    int y = roadLocation.get("y").getAsInt();
    String direction = roadLocation.get("direction").getAsString();
    this.location = new EdgeLocation(new HexLocation(x, y), Abbreviate.unAbbreviateEdge(direction));
}

From source file:clientcommunicator.operations.DiscardCardsRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    JsonObject resourceCards = obj.getAsJsonObject("discardedCards");
    int brick = resourceCards.get("brick").getAsInt();
    int ore = resourceCards.get("ore").getAsInt();
    int sheep = resourceCards.get("sheep").getAsInt();
    int wheat = resourceCards.get("wheat").getAsInt();
    int wood = resourceCards.get("wood").getAsInt();
    this.discardedCards = new ResourceCards(brick, wheat, wood, ore, sheep);
}

From source file:clientcommunicator.operations.PlaySoldierRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    this.victimIndex = new NullablePlayerIdx(obj.get("victimIndex").getAsInt());
    JsonObject locationObj = obj.getAsJsonObject("location");
    int x = locationObj.get("x").getAsInt();
    int y = locationObj.get("y").getAsInt();
    this.newLocation = new HexLocation(x, y);
}

From source file:clientcommunicator.operations.RoadBuildingCardRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    this.spot1 = this.getLocation(obj.getAsJsonObject("spot1"));
    this.spot2 = this.getLocation(obj.getAsJsonObject("spot2"));
}

From source file:clientcommunicator.operations.RobPlayerRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerThatsRobbingIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    this.victimIndex = new NullablePlayerIdx(obj.get("victimIndex").getAsInt());
    JsonObject locationObj = obj.getAsJsonObject("location");
    int x = locationObj.get("x").getAsInt();
    int y = locationObj.get("y").getAsInt();
    this.location = new HexLocation(x, y);
}

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

License:Apache License

public CallResult serviceCall(String inf, JsonObject params, boolean isEncrypt) {
    String result = null;//  w  ww .  j  a  v a  2s  . com
    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/*ww  w.j  a  v a 2 s.  co 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;
}