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.adobe.acs.commons.json.JsonObjectUtil.java

License:Apache License

public static void pruneToDepth(JsonObject obj, int depth) {
    AbstractJSONObjectVisitor prune = new AbstractJSONObjectVisitor() {
        @Override//from  w  w w .ja  va  2s .c o m
        protected void visit(JsonObject jsonObject) {
            if (getCurrentDepth() >= depth) {
                Set<String> allKeys = jsonObject.entrySet().stream().map(Entry::getKey)
                        .collect(Collectors.toSet());
                allKeys.forEach(jsonObject::remove);
            }
        }
    };
    prune.accept(obj);
}

From source file:com.adobe.acs.commons.remoteassets.impl.RemoteAssetsNodeSyncImpl.java

License:Apache License

/**
 * Create or update resources from remote JSON.
 *
 * @param json JsonObject//w  ww .j a v a2s. c om
 * @param resource Resource
 * @throws IOException exception
 * @throws RepositoryException exception
 */
private void createOrUpdateNodes(final ResourceResolver remoteAssetsResolver, final JsonObject json,
        final Resource resource) throws IOException, RepositoryException {
    for (Map.Entry<String, JsonElement> jsonEntry : json.entrySet()) {
        JsonElement jsonElement = jsonEntry.getValue();
        if (jsonElement.isJsonObject()) {
            createOrUpdateNodesForJsonObject(remoteAssetsResolver, jsonEntry.getKey(), resource);
        } else if (jsonElement.isJsonArray()) {
            setNodeArrayProperty(remoteAssetsResolver, jsonEntry.getKey(), jsonElement.getAsJsonArray(),
                    resource);
        } else {
            setNodeProperty(remoteAssetsResolver, jsonEntry.getKey(), json, resource);
        }
    }
}

From source file:com.adobe.acs.commons.synth.children.ChildrenAsPropertyResource.java

License:Apache License

/**
 * Serializes all children data as JSON to the resource's propertyName.
 *
 * @throws InvalidDataFormatException// ww w.  j a va  2 s .co  m
 */
private void serialize() throws InvalidDataFormatException {
    final long start = System.currentTimeMillis();

    final ModifiableValueMap modifiableValueMap = this.resource.adaptTo(ModifiableValueMap.class);
    JsonObject childrenJSON = new JsonObject();

    try {
        // Add the new entries to the JSON
        for (Resource childResource : this.orderedCache) {
            childrenJSON.add(childResource.getName(), this.serializeToJSON(childResource));
        }

        if (childrenJSON.entrySet().size() > 0) {
            // Persist the JSON back to the Node
            modifiableValueMap.put(this.propertyName, childrenJSON.toString());
        } else {
            // Nothing to persist; delete the property
            modifiableValueMap.remove(this.propertyName);
        }

        log.debug("Persist operation for [ {} ] in [ {} ms ]",
                this.resource.getPath() + "/" + this.propertyName, System.currentTimeMillis() - start);

    } catch (NoSuchMethodException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    } catch (IllegalAccessException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    } catch (InvocationTargetException e) {
        throw new InvalidDataFormatException(this.resource, this.propertyName, childrenJSON.toString());
    }
}

From source file:com.adobe.acs.commons.synth.children.ChildrenAsPropertyResource.java

License:Apache License

/**
 * Converts a JSONObject to the list of SyntheticChildAsPropertyResources.
 *
 * @param jsonObject the JSONObject to deserialize.
 * @return the list of SyntheticChildAsPropertyResources the jsonObject represents.
 *///from  w  ww.ja  v a  2  s .c  o m
protected final List<SyntheticChildAsPropertyResource> deserializeToSyntheticChildResources(
        JsonObject jsonObject) {
    final List<SyntheticChildAsPropertyResource> resources = new ArrayList<>();

    for (Entry<String, JsonElement> elem : jsonObject.entrySet()) {
        final String nodeName = elem.getKey();
        JsonObject entryJSON = elem.getValue().getAsJsonObject();

        if (entryJSON == null) {
            continue;
        }

        final ValueMap properties = new ValueMapDecorator(new HashMap<>());
        for (Entry<String, JsonElement> prop : entryJSON.entrySet()) {
            final String propName = prop.getKey();
            properties.put(propName, prop.getValue().getAsString());
        }

        resources.add(new SyntheticChildAsPropertyResource(this.getParent(), nodeName, properties));
    }

    return resources;
}

From source file:com.adobe.acs.commons.wcm.impl.AbstractWidgetConfigurationServlet.java

License:Apache License

/**
 * Load the base configuration and "underlay" it under the provided
 * configuration so that the provided configuration overwrites the default
 * configuration.// w  w w.  j a  va  2  s.c o  m
 *
 * @param config the configuration to underlay
 * @param resource the resource to underlay
 * @return the underlayed configuration
 * @throws JSONException
 * @throws ServletException
 */
protected final JsonObject underlay(JsonObject config, Resource resource) throws ServletException {
    JsonObject baseStructure = JsonObjectUtil.toJsonObject(resource);
    if (baseStructure != null) {
        config.entrySet().forEach(e -> baseStructure.add(e.getKey(), e.getValue()));
        return baseStructure;
    } else {
        return config;
    }
}

From source file:com.adobe.ags.curly.controller.DataImporterController.java

License:Apache License

private void openJson(File file) throws FileNotFoundException, IOException {
    FileReader fileReader = new FileReader(file);
    try (BufferedReader reader = new BufferedReader(fileReader)) {
        JsonParser parser = new JsonParser();
        JsonObject data = parser.parse(reader).getAsJsonObject();

        data.entrySet().stream().filter((entry) -> (entry.getValue().isJsonArray())).forEach((entry) -> {
            worksheetSelector.getItems().add(entry.getKey());
        });/* w  ww.  j  a  v  a 2s  . co m*/

        sheetReader = (String node) -> readNodes(data.get(node).getAsJsonArray());
        skipFirstSelection.setValue(1);
        Platform.runLater(() -> worksheetSelector.getSelectionModel().selectFirst());
    }
}

From source file:com.adobe.ags.curly.controller.DataImporterController.java

License:Apache License

private List<List<String>> readNodes(JsonArray data) {
    TreeSet<String> attributes = new TreeSet<>();
    List<Map<String, String>> rows = new ArrayList<>();
    data.forEach((elem) -> {//w  w w  .  j  ava2s.  c  o  m
        if (elem.isJsonObject()) {
            JsonObject row = elem.getAsJsonObject();
            Map<String, String> rowMap = row.entrySet().stream()
                    .filter((entry) -> entry.getValue().isJsonPrimitive()).collect(Collectors
                            .toMap((entry) -> entry.getKey(), (entry) -> entry.getValue().getAsString()));
            rows.add(rowMap);
            attributes.addAll(rowMap.keySet());
        }
    });
    List<List<String>> results = rows.stream()
            .map((row) -> attributes.stream().map((attr) -> row.get(attr)).collect(Collectors.toList()))
            .collect(Collectors.toList());
    results.add(0, new ArrayList<String>(attributes));
    return results;
}

From source file:com.agateau.pixelwheels.stats.JsonGameStatsIO.java

License:Open Source License

@Override
public void load() {
    Assert.check(mGameStats != null, "setGameStats() has not been called");
    if (!mHandle.exists()) {
        return;/* w  ww .j  av  a2  s .  com*/
    }
    mGameStats.mTrackStats.clear();
    String json = mHandle.readString("UTF-8");
    JsonParser parser = new JsonParser();
    JsonObject root = parser.parse(json).getAsJsonObject();
    JsonObject trackStatsObject = root.getAsJsonObject("trackStats");
    for (Map.Entry<String, JsonElement> kv : trackStatsObject.entrySet()) {
        String trackId = kv.getKey();
        mGameStats.addTrack(trackId);
        loadTrackStats(mGameStats.getTrackStats(trackId), kv.getValue().getAsJsonObject());
    }
}

From source file:com.algollabs.rgx.java

License:Open Source License

/**
 * Handles an API Construct request.//  w  w  w .  j a  v  a2 s .c om
 *
 * Request JSON structure:
 * {
 *   "pattern": "json_encoded_regex",
 *   "subject": "string",
 *   "flags": ""
 * }
 *
 */
private void testExpression(Request request, Response response, PrintStream stream) {
    try {
        String ptrn = null;
        String subj = null;
        String flags = null;

        // parse the required attributes: pattern, subject, and flags

        JsonElement jsonElement = parser_.parse(request.getContent());
        JsonObject jsonObject = jsonElement.getAsJsonObject();

        for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            String k = entry.getKey();
            JsonElement v = entry.getValue();

            if (k.equals("pattern")) {
                ptrn = gson_.fromJson(v, String.class);
            } else if (k.equals("subject")) {
                subj = gson_.fromJson(v, String.class);
            } else if (k.equals("flags")) {
                flags = gson_.fromJson(v, String.class);
            } else {
                throw new UnrecognizedAttributeException(k);
            }
        }

        // have a Construct test the expression
        stream.println((new Construct(ptrn, subj, flags, true)).test());
        response.setStatus(Status.OK);
    } catch (UnrecognizedAttributeException e) {
        e.printStackTrace();

        response.setStatus(Status.BAD_REQUEST);
        stream.println(e.getMessage());

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

        response.setStatus(Status.INTERNAL_SERVER_ERROR);
        stream.println(e.getMessage());
    }

    stream.close();
}

From source file:com.aliyun.odps.udf.utils.CounterUtils.java

License:Apache License

private static Counters createFromJson(JsonObject obj) {
    Counters counters = new Counters();
    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String key = entry.getKey();
        CounterGroup group = counters.getGroup(key);
        fromJson(group, entry.getValue().getAsJsonObject());
    }/*from w  w w.  j ava 2 s  .  c o  m*/
    return counters;
}