Example usage for com.google.gson JsonObject entrySet

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

Introduction

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

Prototype

public Set<Map.Entry<String, JsonElement>> entrySet() 

Source Link

Document

Returns a set of members of this object.

Usage

From source file:com.cognifide.actions.msg.push.PushMessageReceiver.java

License:Apache License

@Override
public void gotMessage(String topic, String msg) {
    if (!"ACTION".equals(topic)) {
        return;/*  w  ww  .ja v  a2  s.c om*/
    }
    final JsonObject json = GSON.fromJson(msg, JsonObject.class);
    final String type = json.get("type").getAsString();
    final JsonObject payload = json.get("payload").getAsJsonObject();
    final Map<String, String> properties = new LinkedHashMap<String, String>();
    for (Entry<String, JsonElement> e : payload.entrySet()) {
        properties.put(e.getKey(), e.getValue().getAsString());
    }
    messageConsumer.consumeMessage(type, properties);
}

From source file:com.cognifide.actions.msg.websocket.message.SocketMessageReceiver.java

License:Apache License

@Override
public void gotMessage(String msg) {
    final JsonObject json = GSON.fromJson(msg, JsonObject.class);
    final String type = json.get("type").getAsString();
    final JsonObject payload = json.get("payload").getAsJsonObject();
    final Map<String, String> properties = new LinkedHashMap<String, String>();
    for (Entry<String, JsonElement> e : payload.entrySet()) {
        properties.put(e.getKey(), e.getValue().getAsString());
    }//  ww  w.ja v a  2s  .c o m
    messageConsumer.consumeMessage(type, properties);
}

From source file:com.collective.celos.ci.testing.fixtures.compare.json.JsonElementConstructor.java

License:Apache License

private JsonObject deepCopyJsonObject(String path, JsonObject el, Set<String> ignorePaths) {

    JsonObject result = new JsonObject();
    for (Map.Entry<String, JsonElement> entry : el.entrySet()) {
        String newPath = path + "/" + entry.getKey();
        if (!ignorePaths.contains(newPath)) {
            result.add(entry.getKey(), deepCopy(newPath, entry.getValue(), ignorePaths));
        }//from   www  .j av  a2s.c om
    }
    return result;
}

From source file:com.commonslibrary.commons.utils.JsonUtils.java

License:Open Source License

/**
 * JSONObjec??Map-List?// w w  w . ja v  a2  s .c  o m
 *
 * @param json
 * @return
 */
public static Map<String, Object> toMap(JsonObject json) {
    Map<String, Object> map = new HashMap<String, Object>();
    Set<Map.Entry<String, JsonElement>> entrySet = json.entrySet();
    for (Iterator<Map.Entry<String, JsonElement>> iter = entrySet.iterator(); iter.hasNext();) {
        Map.Entry<String, JsonElement> entry = iter.next();
        String key = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof JsonArray)
            map.put((String) key, toList((JsonArray) value));
        else if (value instanceof JsonObject)
            map.put((String) key, toMap((JsonObject) value));
        else
            map.put((String) key, value);
    }
    return map;
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

private static final byte[] hashObject(JsonObject o, String r) throws ContinusecException {
    ArrayList<byte[]> entries = new ArrayList<byte[]>();
    for (Map.Entry<String, JsonElement> e : o.entrySet()) {
        entries.add(ArrayUtils.addAll(hashString(e.getKey(), r), objectHashWithRedaction(e.getValue(), r)));
    }//from w w  w.  ja  v a  2s  . c o  m
    Collections.sort(entries, ByteArrayComparator.getInstance());

    MessageDigest d = DigestUtils.getSha256Digest();
    d.update((byte) 'd');
    for (byte[] b : entries) {
        d.update(b);
    }
    return d.digest();
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

private static final JsonElement shedObject(JsonObject o, String r) throws ContinusecException {
    JsonObject rv = new JsonObject();
    for (Map.Entry<String, JsonElement> e : o.entrySet()) {
        JsonElement v = e.getValue();/*w w w .java2s  . c  o  m*/
        if (v.isJsonArray()) {
            JsonArray a = v.getAsJsonArray();
            if (a.size() == 2) {
                rv.add(e.getKey(), shedRedactable(a.get(1), r));
            } else {
                throw new InvalidObjectException();
            }
        } else if (v.isJsonPrimitive()) {
            JsonPrimitive p = v.getAsJsonPrimitive();
            if (p.isString()) {
                if (p.getAsString().startsWith(r)) {
                    // all good, but we shed it.
                } else {
                    throw new InvalidObjectException();
                }
            } else {
                throw new InvalidObjectException();
            }
        } else {
            throw new InvalidObjectException();
        }
    }
    return rv;
}

From source file:com.continuuity.loom.cluster.Node.java

License:Apache License

/**
 * Add the provisioner results from a task to the current provisioner results.
 *
 * @param results Results of a task to add to the overall node results
 *///from ww w  . j a  v a2s . c  o  m
public void addResults(JsonObject results) {
    for (Map.Entry<String, JsonElement> entry : results.entrySet()) {
        // special cased for now
        // TODO: make ip a required field for confirm tasks
        if (IPADDRESS_KEY.equals(entry.getKey())) {
            this.properties.setIpaddress(entry.getValue().getAsString());
        } else {
            this.provisionerResults.add(entry.getKey(), entry.getValue());
        }
    }
}

From source file:com.continuuity.loom.codec.json.current.TaskConfigCodec.java

License:Apache License

private JsonObject shallowCopy(JsonObject o) {
    JsonObject out = new JsonObject();
    for (Map.Entry<String, JsonElement> entry : o.entrySet()) {
        out.add(entry.getKey(), entry.getValue());
    }//  ww w  . j av  a  2s  . c om
    return out;
}

From source file:com.continuuity.loom.macro.Expander.java

License:Apache License

/**
 * Given a JSON tree, find the element specified by the path (or the root if path is null). In that subtree,
 * recursively traverse all elements, and expand all String typed right hand side values.  If a macro cannot be
 * expanded due to the cluster object missing certain data, that macro will be left unexpanded.
 *
 * @param json A JSON tree//from w w  w.  jav a2 s.  co  m
 * @param path the path to expand under
 * @param cluster the cluster to use for expanding macros.
 * @param nodes the cluster nodes to use for expanding macros.
 * @param node the cluster node to use for expanding macros.
 * @return a new JSON tree if any expansion took place, and the original JSON tree otherwise.
 * @throws SyntaxException if a macro expression is ill-formed.
 * @throws IncompleteClusterException if the cluster does not have the meta data to expand all macros.
 */
public static JsonElement expand(JsonElement json, @Nullable java.util.List<String> path, Cluster cluster,
        Set<Node> nodes, Node node) throws SyntaxException, IncompleteClusterException {

    // if path is given,
    if (path != null && !path.isEmpty()) {
        String first = path.get(0);
        if (json.isJsonObject()) {
            JsonObject object = json.getAsJsonObject();
            JsonElement json1 = object.get(first);
            if (json1 != null) {
                JsonElement expanded = expand(json1, path.subList(1, path.size()), cluster, nodes, node);
                if (expanded != json1) {
                    // only construct new json object if actual expansion happened
                    JsonObject object1 = new JsonObject();
                    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
                        object1.add(entry.getKey(), entry.getKey().equals(first) ? expanded : entry.getValue());
                    }
                    return object1;
                }
            }
        }
        // path was given, but either no corresponding subtree was found or no expansion happened...
        return json;
    }

    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            String value = primitive.getAsString();
            String expanded = expand(value, cluster, nodes, node);
            if (!expanded.equals(value)) {
                // only return a new json element if actual expansion happened
                return new JsonPrimitive(expanded);
            }
        }
    }

    if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        JsonArray array1 = new JsonArray();
        boolean expansionHappened = false;
        for (JsonElement element : array) {
            JsonElement expanded = expand(element, path, cluster, nodes, node);
            if (expanded != element) {
                expansionHappened = true;
            }
            array1.add(expanded);
        }
        // only return a new json array if actual expansion happened
        if (expansionHappened) {
            return array1;
        }
    }

    if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        JsonObject object1 = new JsonObject();
        boolean expansionHappened = false;
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            JsonElement expanded = expand(entry.getValue(), path, cluster, nodes, node);
            if (expanded != entry.getValue()) {
                expansionHappened = true;
            }
            object1.add(entry.getKey(), expand(entry.getValue(), path, cluster, nodes, node));
        }
        if (expansionHappened) {
            return object1;
        }
    }

    return json;
}

From source file:com.contrastsecurity.ide.eclipse.core.extended.ExtendedContrastSDK.java

License:Open Source License

public StoryResource getStory(String orgUuid, String traceId) throws IOException, UnauthorizedException {
    InputStream is = null;//from w w w  .  ja v a 2  s  . c  o  m
    InputStreamReader reader = null;
    try {
        String traceUrl = getTraceUrl(orgUuid, traceId);
        is = makeRequest(HttpMethod.GET, traceUrl);
        reader = new InputStreamReader(is);

        String inputString = IOUtils.toString(is, "UTF-8");
        StoryResource story = this.gson.fromJson(inputString, StoryResource.class);
        JsonObject object = (JsonObject) new JsonParser().parse(inputString);
        JsonObject storyObject = (JsonObject) object.get("story");
        if (storyObject != null) {
            JsonArray chaptersArray = (JsonArray) storyObject.get("chapters");
            List<Chapter> chapters = story.getStory().getChapters();
            if (chapters == null) {
                chapters = new ArrayList<>();
            } else {
                chapters.clear();
            }
            for (int i = 0; i < chaptersArray.size(); i++) {
                JsonObject member = (JsonObject) chaptersArray.get(i);
                Chapter chapter = gson.fromJson(member, Chapter.class);
                chapters.add(chapter);
                JsonObject properties = (JsonObject) member.get("properties");
                if (properties != null) {
                    Set<Entry<String, JsonElement>> entries = properties.entrySet();
                    Iterator<Entry<String, JsonElement>> iter = entries.iterator();
                    List<PropertyResource> propertyResources = new ArrayList<>();
                    chapter.setPropertyResources(propertyResources);
                    while (iter.hasNext()) {
                        Entry<String, JsonElement> prop = iter.next();
                        // String key = prop.getKey();
                        JsonElement entryValue = prop.getValue();
                        if (entryValue != null && entryValue.isJsonObject()) {
                            JsonObject obj = (JsonObject) entryValue;
                            JsonElement name = obj.get("name");
                            JsonElement value = obj.get("value");
                            if (name != null && value != null) {
                                PropertyResource propertyResource = new PropertyResource();
                                propertyResource.setName(name.getAsString());
                                propertyResource.setValue(value.getAsString());
                                propertyResources.add(propertyResource);
                            }
                        }
                    }
                }

            }
        }
        return story;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(reader);
    }
}