Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:ch.icclab.cyclops.consume.data.UsageDeserializer.java

License:Open Source License

@Override
public void preDeserialize(Class<? extends T> clazz, JsonElement jsonElement, Gson gson) {

    // valid JSON object
    if (jsonElement != null && jsonElement.isJsonObject()) {
        JsonObject root = jsonElement.getAsJsonObject();

        // map data to string so it can be persisted as jsonb
        if (root.has(Usage.DATA_FIELD.getName())) {
            root.addProperty(Usage.DATA_FIELD.getName(),
                    new Gson().toJson(root.get(Usage.DATA_FIELD.getName())));
        }/*from   w ww . j  a  v a2 s .com*/
    }
}

From source file:ch.iterate.openstack.swift.Client.java

License:Open Source License

/**
 * Lists the segments associated with an existing object.
 *
 * @param region    The name of the storage region
 * @param container The name of the container
 * @param name      The name of the object
 * @return a Map from container to lists of storage objects if a large object is present, otherwise null
 */// w  w w .  ja  v a2  s  . c  o m
public Map<String, List<StorageObject>> listObjectSegments(Region region, String container, String name)
        throws IOException {

    Map<String, List<StorageObject>> existingSegments = new HashMap<String, List<StorageObject>>();

    try {
        ObjectMetadata existingMetadata = getObjectMetaData(region, container, name);

        if (existingMetadata.getMetaData().containsKey(Constants.MANIFEST_HEADER)) {
            /*
             * We have found an existing dynamic large object, so use the prefix to get a list of
             * existing objects. If we're putting up a new dlo, make sure the segment prefixes are
             * different, then we can delete anything that's not in the new list if necessary.
             */
            String manifestDLO = existingMetadata.getMetaData().get(Constants.MANIFEST_HEADER);
            String segmentContainer = manifestDLO.substring(1, manifestDLO.indexOf('/', 1));
            String segmentPath = manifestDLO.substring(manifestDLO.indexOf('/', 1), manifestDLO.length());
            existingSegments.put(segmentContainer, this.listObjects(region, segmentContainer, segmentPath));
        } else if (existingMetadata.getMetaData().containsKey(Constants.X_STATIC_LARGE_OBJECT)) {
            /*
             * We have found an existing static large object, so grab the manifest data that
             * details the existing segments - delete any later that we don't need any more
             */
            boolean isSLO = "true".equals(existingMetadata.getMetaData().get(Constants.X_STATIC_LARGE_OBJECT)
                    .toLowerCase(Locale.ENGLISH));
            if (isSLO) {
                final JsonParser parser = new JsonParser();
                URIBuilder urlBuild = new URIBuilder(region.getStorageUrl(container, name));
                urlBuild.setParameter("multipart-manifest", "get");
                URI url = urlBuild.build();
                HttpGet method = new HttpGet(url);
                Response response = this.execute(method);
                if (response.getStatusCode() == HttpStatus.SC_OK) {
                    String manifest = response.getResponseBodyAsString();
                    JsonArray segments = parser.parse(manifest).getAsJsonArray();
                    for (JsonElement o : segments) {
                        /*
                         * Parse each JSON object in the list and create a list of Storage Objects
                         */
                        JsonObject segment = o.getAsJsonObject();
                        String objectPath = segment.get("name").getAsString();
                        String segmentContainer = objectPath.substring(1, objectPath.indexOf('/', 1));
                        String segmentPath = objectPath.substring(objectPath.indexOf('/', 1) + 1,
                                objectPath.length());
                        List<StorageObject> containerSegments = existingSegments.get(segmentContainer);
                        if (containerSegments == null) {
                            containerSegments = new ArrayList<StorageObject>();
                            existingSegments.put(segmentContainer, containerSegments);
                        }
                        final StorageObject object = new StorageObject(segmentPath);
                        object.setSize(Long.valueOf(segment.get("bytes").getAsString()));
                        object.setMd5sum(segment.get("hash").getAsString());
                        object.setLastModified(segment.get("last_modified").getAsString());
                        object.setMimeType(segment.get("content_type").getAsString());
                        containerSegments.add(object);
                    }
                } else {
                    method.abort();
                    throw new GenericException(response);
                }
            }
        } else {
            /*
             * Not a large object, so return null
             */
            return null;
        }
    } catch (NotFoundException e) {
        /*
         * Just means no object exists with the specified region, container and name
         */
        return null;
    } catch (JsonParseException e) {
        throw new GenericException("JSON parsing failed reading static large object manifest", e);
    } catch (URISyntaxException e) {
        throw new GenericException("URI Building failed reading static large object manifest", e);
    }

    return existingSegments;
}

From source file:cheerPackage.JSONUtils.java

public static String getJsonAttributeValue(String rawJson, String attribute)
        throws MalformedJsonException, JsonSyntaxException {
    //just a single Json Object
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(rawJson);
    if (json.isJsonObject()) {
        return json.getAsJsonObject().get(attribute).getAsString();
    } else if (json.isJsonPrimitive()) {
        return json.getAsString();
    } else {/*from w w  w.  j  av a  2 s . c o m*/
        System.out.println(
                "This function only works on Json objects and primitives, use getValieFromArrayElement for arrays");
        return null;
    }
}

From source file:cheerPackage.JSONUtils.java

public static String getValueFromArrayElement(String jsonArrayString, String attribute, int index)
        throws MalformedJsonException, JsonSyntaxException {
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(jsonArrayString);
    if (json.isJsonArray()) {
        JsonElement firstItem = json.getAsJsonArray().get(index);
        if (firstItem.isJsonPrimitive()) {
            return firstItem.getAsString();
        } else if (firstItem.isJsonObject()) {
            return firstItem.getAsJsonObject().get(attribute).getAsString();
        } else {/*from w ww  .  j  av  a  2s  .  c  o m*/
            System.out.println(
                    "This function only goes in 1 level (from Array to Object in array, or primitive).");
            return null;
        }
    } else {
        System.out.println("This function only works on Json arrays.");
        return null;
    }
}

From source file:cheerPackage.SocketServer.java

public String getAccessToken(String res, String title) throws JSONException {
    String value = null;//from w  w w. ja  v a 2s  .c  o m
    //just a single Json Object
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(res);
    System.out.println(json);
    System.out.println("HERE IS MY value ---->>>");
    JsonObject object = json.getAsJsonObject();
    System.out.println(object.toString());
    object.toString();
    value = object.get(title).getAsString();

    return value;
}

From source file:cheerPackage.SocketServer.java

public void insertTournamentMatchesToDB(String res) throws JSONException {

    //cant deal with null information -->>>>>
    Matches match = new Matches();
    System.out.println("calling from socketServer ...");
    //convert res json Array
    JSONArray jsonArray = new JSONArray(res);
    //iterate JSONobj in the array inserting the to DB..
    for (int i = 0; i < jsonArray.length(); i++) {
        //System.out.println("from for loop the OBJECT");
        JsonParser parser = new JsonParser();
        JsonElement json = parser.parse(jsonArray.get(i).toString());
        JsonObject object = json.getAsJsonObject();
        object.toString();/*from  ww w  .  j a va 2 s . c om*/
        match.setId(object.get("id").getAsString());
        match.setType(object.get("type").getAsString());
        match.setDiscipline(object.get("discipline").getAsString());
        match.setStatus(object.get("status").getAsString());
        match.setTournamentId(object.get("tournament_id").getAsString());
        match.setNumber(object.get("number").getAsInt());
        match.setStageNumber(object.get("stage_number").getAsInt());
        match.setGroupNumber(object.get("group_number").getAsInt());
        match.setRoundNumber(object.get("round_number").getAsInt());

        //match.setTimezone("FI");
        /**if(object.get("timeZone").getAsString() == null){
        System.out.println("THE TIMEZONE...");
        match.setTimezone("FI");
        }else{
        match.setTimezone(object.get("timeZone").getAsString());
        }**/

        match.setMatchFormat("knockout");
        match.setOpponents(object.get("opponents").toString());
        /**Date today = new Date();
        today.setHours(0); today.setMinutes(0); today.setSeconds(0);
        match.setDate(today);**/
        viewCtrl.insertMatches(match);
    }

}

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

License:Open Source License

private HypermediaLink getResource(JsonObject ob) {
    HypermediaLink hypermediaLink = new HypermediaLink();
    hypermediaLink.setVersion(ob.get(VERSION).getAsString());

    if (ob.has(_LINK)) {
        JsonObject map = ob.getAsJsonObject(_LINK);
        Set<Entry<String, JsonElement>> keySet = map.entrySet();
        for (Entry<String, JsonElement> entry : keySet) {
            JsonElement element = entry.getValue();
            JsonObject value = element.getAsJsonObject();

            Hypermedia link = new Hypermedia();
            link.setHref(value.get(HREF).getAsString());
            link.setTemplated(value.get(TEMPLATED).getAsBoolean());

            hypermediaLink.addHypermedia(entry.getKey(), link);
        }//from   w w  w. ja va2  s  .c  o  m
    }

    return hypermediaLink;
}

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

License:Open Source License

private void getLabels(POI poi, JsonObject jObject) {
    JsonArray jArray = jObject.getAsJsonArray(LABEL);
    for (int i = 0; i < jArray.size(); i++) {
        JsonElement e = jArray.get(i);
        JsonObject o = e.getAsJsonObject();
        poi.addLabel(getPOITermType(o));
    }/*from   w w  w. j  av  a  2  s  . co m*/
}

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

License:Open Source License

private void getDescription(POI poi, JsonObject jObject) {
    JsonArray jArray = jObject.getAsJsonArray(DESCRIPTION);
    for (int i = 0; i < jArray.size(); i++) {
        JsonElement e = jArray.get(i);
        JsonObject o = e.getAsJsonObject();
        poi.addDescription(getPOIBaseType(o));
    }/*from  w  ww  .j  a va 2s .co  m*/
}

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

License:Open Source License

private void getCategories(POI poi, JsonObject jObject) {
    JsonArray jArray = jObject.getAsJsonArray(CATEGORY);
    for (int i = 0; i < jArray.size(); i++) {
        JsonElement e = jArray.get(i);
        JsonObject o = e.getAsJsonObject();
        poi.addCategory(getPOITermType(o));
    }/*from  w  w  w  . j  av a  2s.co m*/
}