Example usage for com.google.gson JsonObject get

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

Introduction

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

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

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 w  w . j  av a2s. co  m*/
    }
}

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
 *///from w w w .  j  a  va  2 s. com
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:ch.there.gson.GsonInvokerServiceExporter.java

License:Apache License

@Override
public void handle(HttpExchange exchange) throws IOException {
    Gson gson = GsonFactory.getGson();/* w w w .j  a va 2 s. com*/

    try {
        Integer rpcVersion = Integer.valueOf(exchange.getRequestHeaders().getFirst("rpc-version"));
        CallerRemoteApiVersion.setVersion(rpcVersion);
        InputStreamReader reader = new InputStreamReader(exchange.getRequestBody());
        JsonParser parser = new JsonParser();
        JsonObject remote = parser.parse(reader).getAsJsonObject();
        String methodName = remote.get("methodName").getAsString();

        Type collectionType = new TypeToken<Collection<Class<?>>>() {
        }.getType();
        Collection<Class<?>> parameterTypes = gson.fromJson(remote.get("parameterTypes"), collectionType);

        JsonArray args = remote.get("arguments").getAsJsonArray();
        Class<?>[] params = parameterTypes.toArray(new Class[parameterTypes.size()]);

        Object[] arguments = new Object[params.length];

        for (int i = 0; i < params.length; i++) {
            Class<?> clazz = params[i];
            Object argument = gson.fromJson(args.get(i), clazz);
            arguments[i] = argument;
        }

        RemoteInvocation remoteInvocation = new RemoteInvocation(methodName, params, arguments);
        RemoteInvocationResult result = invokeAndCreateResult(remoteInvocation, getProxy());
        writeRemoteInvocationResult(exchange, result);
        exchange.close();
    } catch (Throwable e) {
        e.printStackTrace();
    }

}

From source file:ch.usz.c3pro.c3_pro_android_framework.dataqueue.EncryptedDataQueue.java

License:Open Source License

/**
 * Decrypts a FHIR resource contained in a json object received from a C3-PRO server.
 *//*  w  ww  .j  av  a 2 s.  co  m*/
public IBaseResource decryptResource(JsonObject jsonObject)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
        IllegalBlockSizeException, InvalidAlgorithmParameterException {
    if (jsonObject.has("symmetric_key") && jsonObject.has("message")) {
        String keyString = jsonObject.get("symmetric_key").getAsString();
        String objectString = jsonObject.get("message").getAsString();

        if (!Strings.isNullOrEmpty(keyString) && !Strings.isNullOrEmpty(objectString)) {
            byte[] keyBytes = Base64.decode(keyString, Base64.DEFAULT);
            byte[] objectBytes = Base64.decode(objectString, Base64.DEFAULT);

            SecretKey key = rsaUtility.unwrapKey(keyBytes);
            byte[] resourceBytes = aesUtility.deCryptData(objectBytes, key);
            String resourceString = new String(resourceBytes);
            return Pyro.getFhirContext().newJsonParser().parseResource(resourceString);
        }
    }
    Log.e(Logging.logTag,
            "The jsonObject could not be decrypted, fields are invalid: " + jsonObject.toString());
    return null;
}

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

License:Open Source License

private POIBaseType getPOIBaseType(JsonObject json) {
    POIBaseType base = new POIBaseType();
    if (json.has(ID) && !json.get(ID).isJsonNull())
        base.setId(json.get(ID).getAsString());

    if (json.has(VALUE) && !json.get(VALUE).isJsonNull())
        base.setValue(json.get(VALUE).getAsString());

    if (json.has(HREF) && !json.get(HREF).isJsonNull())
        base.setHref(json.get(HREF).getAsString());

    if (json.has(TYPE) && !json.get(TYPE).isJsonNull())
        base.setType(json.get(TYPE).getAsString());

    if (json.has(LANG) && !json.get(LANG).isJsonNull())
        base.setLang(json.get(LANG).getAsString());

    if (json.has(BASE) && !json.get(BASE).isJsonNull())
        base.setBase(json.get(BASE).getAsString());

    try {//from   ww w.j  a v  a 2  s . com
        if (json.has(CREATED) && !json.get(CREATED).isJsonNull())
            base.setCreated(
                    new SimpleDateFormat(FORMAT, Locale.ENGLISH).parse(json.get(CREATED).getAsString()));

        if (json.has(UPDATED) && !json.get(UPDATED).isJsonNull())
            base.setCreated(
                    new SimpleDateFormat(FORMAT, Locale.ENGLISH).parse(json.get(UPDATED).getAsString()));

        if (json.has(DELETED) && !json.get(DELETED).isJsonNull())
            base.setCreated(
                    new SimpleDateFormat(FORMAT, Locale.ENGLISH).parse(json.get(DELETED).getAsString()));

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

    if (json.has(AUTHOR) && !json.get(AUTHOR).isJsonNull()) {
        base.setAuthor(getPOITermType(json.get(AUTHOR).getAsJsonObject()));
    }

    if (json.has(LICENSE) && !json.get(LICENSE).isJsonNull()) {
        base.setAuthor(getPOITermType(json.get(LICENSE).getAsJsonObject()));
    }

    return base;
}

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

License:Open Source License

private POITermType getPOITermType(JsonObject json) {
    POITermType termType = new POITermType();
    if (json.has(TERM) && !json.get(TERM).isJsonNull())
        termType.setTerm(json.get(TERM).getAsString());

    if (json.has(VALUE) && !json.get(VALUE).isJsonNull())
        termType.setValue(json.get(VALUE).getAsString());

    copyTo(termType, getPOIBaseType(json));
    return termType;
}

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 ww w  . ja  v  a 2  s  . com
    }

    return hypermediaLink;
}

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

License:Open Source License

private void getPoints(Location location, JsonArray array) {
    for (int i = 0; i < array.size(); i++) {
        JsonObject ob = array.get(i).getAsJsonObject();

        Geometry g = new Geometry();
        g.setPosList(ob.get(POINT_P).getAsJsonObject().get(POS_LIST).getAsString());

        Point point = new Point();
        point.setTerm(ob.get(TERM).getAsString());
        point.setPoint(g);//from  w  ww  .ja va2s . co m

        location.addPoint(point);
    }
}

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

License:Open Source License

private void getLine(Location location, JsonArray array) {
    for (int i = 0; i < array.size(); i++) {
        JsonObject ob = array.get(i).getAsJsonObject();

        Geometry g = new Geometry();
        g.setPosList(ob.get(LINE_STRING).getAsJsonObject().get(POS_LIST).getAsString());

        Line line = new Line();
        line.setTerm(ob.get(TERM).getAsString());
        line.setLineString(g);/*from w ww.  j a  v  a 2 s. c  o m*/

        location.addLine(line);
    }
}

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

License:Open Source License

private void getSimplePolygon(Location location, JsonArray array) {
    for (int i = 0; i < array.size(); i++) {
        JsonObject ob = array.get(i).getAsJsonObject();

        Geometry g = new Geometry();
        g.setPosList(ob.get(SIMPLE_POLYGON).getAsJsonObject().get(POS_LIST).getAsString());

        Polygon polygon = new Polygon();
        polygon.setTerm(ob.get(TERM).getAsString());
        polygon.setSimplePolygon(g);/*from   w  w  w.ja v a 2  s .  co  m*/

        location.addPolygon(polygon);
    }
}