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() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:edu.uniandes.ecos.codeaholics.config.Authorization.java

/**
 * Get from token body the corresponding Claim with key pKey
 * /*from ww  w .j a v a2s .  c  o m*/
 * @param pJwt
 * @param pKey
 * @return
 * @throws InvalidTokenException
 *             : in case the token has not three parts or if claim is not
 *             found
 */
public static String getTokenClaim(String pJwt, String pKey) throws InvalidTokenException {

    String[] jwtElements = pJwt.split("\\.");

    if (jwtElements.length < 3) {
        throw new InvalidTokenException("Token is invalid", "505");
    }

    String value = null;

    ByteArrayInputStream inStream = new ByteArrayInputStream(Base64.getDecoder().decode(jwtElements[1]));

    BufferedReader streamReader = new BufferedReader(new InputStreamReader(inStream));
    StringBuilder responseStrBuilder = new StringBuilder();
    String inputStr;

    try {
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(responseStrBuilder.toString()).getAsJsonObject();

        if (json.getAsJsonObject().has(pKey)) {
            value = json.getAsJsonObject().get(pKey).getAsString();
            log.info("Found key with value: " + value);
        } else {
            throw new InvalidTokenException("Token is invalid", "505");
        }

    } catch (IOException e) {
        e.printStackTrace();

    }

    return value;
}

From source file:edu.uniandes.ecos.codeaholics.config.GeneralUtil.java

/**
 * @param pRequest/*from   w  ww .jav a2 s. c o m*/
 * @param pKey
 * @return
 */
public static String extractFromBody(Request pRequest, String pKey) {

    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(pRequest.body()).getAsJsonObject();
    String value = "";
    if (json.getAsJsonObject().has(pKey)) {
        value = json.getAsJsonObject().get(pKey).getAsString();
        log.info("Found key with value: " + value);
    }
    return value;

}

From source file:eu.sisob.uma.NPL.Researchers.Freebase.LocationDataResolver_Method2.java

License:Open Source License

/**
 * Fill a locationSet objet (fill alias of locations, and fill all possible row type city, region, country
 * /* w ww.  jav a  2  s.  co m*/
 * Format of the object to parse
 * 
 * "result": [    
 *      {
 *      "/location/location/containedby": [
 *        {
 *          "/location/location/containedby": [
 *            {
 *              "/location/location/containedby": [
 *                {
 *                  "type": "/location/country",
 *                  "id": "/en/united_states",
 *                  "/common/topic/alias": [
 *                    "America",
 *                    "U.S.",
 *                    "USA",
 *                    "United States",
 *                    "United States of America",
 *                    "US",
 *                    "the states"
 *                  ],
 *                  "name": "United States of America"
 *                }
 *              ],
 *              "type": "/location/administrative_division",
 *              "id": "/en/massachusetts",
 *              "/common/topic/alias": [
 *                "Mass.",
 *                "Bay State",
 *                "Commonwealth of Massachusetts",
 *                "MA",
 *                "Mass"
 *              ],
 *              "name": "Massachusetts"
 *            }
 *          ],
 *          "type": "/location/citytown",
 *          "id": "/en/boston_massachusetts",
 *          "/common/topic/alias": [
 *            "Beantown",
 *            "Boston, Massachusetts",
 *            "City on the Hill",
 *            "The Hub",
 *            "The Hub of the Universe",
 *            "Athens of America",
 *            "Suffolk County / Boston city",
 *            "Boston [Mass."
 *          ],
 *          "name": "Boston"
 *        },
 *        ...
 *      }       
 * 
 * @param result
 * @return
 */
private locationSet getLocationsFromJsonObject1(JsonObject result) {
    locationSet locations_set = new locationSet();

    JsonArray countainers_cities = result.getAsJsonObject().getAsJsonArray("/location/location/containedby");

    if (countainers_cities != null) {
        for (JsonElement countainer_cities : countainers_cities) {
            JsonArray countainers_regions = countainer_cities.getAsJsonObject()
                    .getAsJsonArray("/location/location/containedby");

            if (countainers_regions != null) {
                for (JsonElement countainer_regions : countainers_regions) {
                    JsonArray countainers_countries = countainer_regions.getAsJsonObject()
                            .getAsJsonArray("/location/location/containedby");

                    if (countainers_countries != null) {
                        for (JsonElement countainer_countries : countainers_countries) {

                            String city = countainer_cities.getAsJsonObject().getAsJsonPrimitive("name")
                                    .getAsString();
                            String region = countainer_regions.getAsJsonObject().getAsJsonPrimitive("name")
                                    .getAsString();
                            String country = countainer_countries.getAsJsonObject().getAsJsonPrimitive("name")
                                    .getAsString();

                            String[] cityRegionCountry = new String[] { city, region, country };

                            if (!locations_set.alias.containsKey(city)) {

                                Iterator<JsonElement> it = countainer_cities.getAsJsonObject()
                                        .getAsJsonArray("/common/topic/alias").iterator();
                                ArrayList<String> aux = new ArrayList<String>();
                                while (it.hasNext()) {
                                    String s = it.next().getAsString();
                                    aux.add(s);
                                }
                                locations_set.alias.put(city, aux.toArray(new String[aux.size()]));
                            }

                            if (!locations_set.alias.containsKey(region)) {

                                Iterator<JsonElement> it = countainer_regions.getAsJsonObject()
                                        .getAsJsonArray("/common/topic/alias").iterator();
                                ArrayList<String> aux = new ArrayList<String>();
                                while (it.hasNext()) {
                                    String s = it.next().getAsString();
                                    aux.add(s);
                                }
                                locations_set.alias.put(region, aux.toArray(new String[aux.size()]));
                            }

                            if (!locations_set.alias.containsKey(country)) {
                                Iterator<JsonElement> it = countainer_countries.getAsJsonObject()
                                        .getAsJsonArray("/common/topic/alias").iterator();
                                ArrayList<String> aux = new ArrayList<String>();
                                while (it.hasNext()) {
                                    String s = it.next().getAsString();
                                    aux.add(s);
                                }
                                locations_set.alias.put(country, aux.toArray(new String[aux.size()]));
                            }

                            locations_set.cityRegionCountryList.add(cityRegionCountry);
                        }
                    }

                }
            }

        }
    }

    return locations_set;
}

From source file:fr.inria.atlanmod.discoverer.JsonSource.java

License:Open Source License

/**
 * Computes a list of JSON objects according to the data of this source.
 * If the source includes inputs, the list will include the set of input elements as roots for the 
 * JSON data of each source provided./* www.j  av a2 s .co  m*/
 * If the source does not include inputs, the listt will include all the objects from the JSON data
 * of each source provided.
 * 
 * @return
 */
public List<JsonObject> getSourceDigested() {
    List<JsonObject> result = new ArrayList<JsonObject>();
    if (this.withInput == true) {
        for (JsonData data : this.getJsonData()) {
            JsonObject inputElement = data.getInput();
            JsonElement outputElement = data.getData();
            inputElement.getAsJsonObject().add(getName() + "Output", outputElement);
            result.add(inputElement);
        }
    } else {
        for (JsonData data : this.getJsonData()) {
            JsonElement outputElement = data.getData();
            if (outputElement.isJsonArray()) {
                for (int i = 0; i < outputElement.getAsJsonArray().size(); i++)
                    if (outputElement.getAsJsonArray().get(i).isJsonObject())
                        result.add(outputElement.getAsJsonArray().get(i).getAsJsonObject());
            } else if (outputElement.isJsonObject()) {
                result.add(outputElement.getAsJsonObject());
            }
        }
    }
    return result;
}