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.github.nyrkovalex.deploy.me.parsing.Parser.java

License:Open Source License

public Set<T> parse(JsonObject jsonObject) {
    return jsonObject.entrySet().stream().map(entry -> basicParser.parse(entry.getKey(), entry.getValue()))
            .collect(toSet());//from   www. j a va 2s .c o m
}

From source file:com.github.sheigutn.pushbullet.gson.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }//from ww w. j  a v a2 s  .  com

    if (baseDelegateAdapter == null) {
        baseDelegateAdapter = gson.getDelegateAdapter(RuntimeTypeAdapterFactory.this, TypeToken.get(baseType));
    }

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
    for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
    }

    return new TypeAdapter<R>() {
        @SuppressWarnings("unchecked")
        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //remove(typeFieldName) changed to get(typeFieldName)
            if (labelJsonElement == null) {
                return (R) baseDelegateAdapter.fromJsonTree(jsonElement);
            }
            String label = labelJsonElement.getAsString();
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
            if (delegate == null) {
                throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label
                        + "; did you forget to register a subtype?");
            }
            return delegate.fromJsonTree(jsonElement);
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
            Class<?> srcType = value.getClass();
            String label = subtypeToLabel.get(srcType);
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
            if (delegate == null) {
                throw new JsonParseException(
                        "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
            }
            JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
            //if (jsonObject.has(typeFieldName)) {
            //    throw new JsonParseException("cannot serialize " + srcType.getName()
            //            + " because it already defines a field named " + typeFieldName); <-- Commented
            //}
            JsonObject clone = new JsonObject();
            clone.add(typeFieldName, new JsonPrimitive(label));
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                clone.add(e.getKey(), e.getValue());
            }
            Streams.write(clone, out);
        }
    }.nullSafe();
}

From source file:com.github.strawberry.util.Json.java

License:Open Source License

public static Map<String, Object> parse(String json) {
    JsonObject o = (JsonObject) parser.parse(json);
    Set<Map.Entry<String, JsonElement>> set = o.entrySet();
    Map<String, Object> map = Maps.newHashMap();
    for (Map.Entry<String, JsonElement> e : set) {
        String key = e.getKey();//from   w ww. j a  v  a  2s  .  c  o  m
        JsonElement value = e.getValue();
        if (!value.isJsonPrimitive()) {
            if (value.isJsonObject()) {
                map.put(key, parse(value.toString()));

            } else if (value.isJsonArray()) {
                map.put(key, parseArray(value.toString()));

            }
        } else {
            map.put(key, parsePrimitive(value));
        }

    }
    return map;

}

From source file:com.github.tarsys.android.orm.dataobjects.JSONDataSource.java

public void addDataObject(JsonObject item) {
    JsonArray datos = this.getData();

    if (this.columns.isEmpty()) {
        if (item != null) {
            for (Map.Entry<String, JsonElement> e : item.entrySet())
                this.columns.add(e.getKey());
        }/*from  w w  w .j av  a2s .  c om*/
    }
    datos.add(item);

    this.jsonData = datos.toString();
}

From source file:com.github.tarsys.android.orm.dataobjects.JSONDataSource.java

public JSONDataSource(JsonArray jArray) {
    if (jArray != null && jArray.size() > 0) {
        // cogemos el primer objeto del array, para sacarle sus claves...
        try {/* ww w .  java  2 s .c  om*/
            JsonObject obj = jArray.get(0).getAsJsonObject();
            if (obj != null) {
                for (Map.Entry<String, JsonElement> e : obj.entrySet())
                    this.columns.add(e.getKey());
            }
            this.jsonData = jArray.toString();
        } catch (Exception ex) {
        }
    }
}

From source file:com.google.debugging.sourcemap.SourceMapConsumerV3.java

License:Apache License

/**
 * Parses the given contents containing a source map.
 *//*  w  ww.  j a  v  a  2s  .  c om*/
public void parse(JsonObject sourceMapRoot, SourceMapSupplier sectionSupplier) throws SourceMapParseException {
    try {
        // Check basic assertions about the format.
        int version = sourceMapRoot.get("version").getAsInt();
        if (version != 3) {
            throw new SourceMapParseException("Unknown version: " + version);
        }

        if (sourceMapRoot.has("file") && sourceMapRoot.get("file").getAsString().isEmpty()) {
            throw new SourceMapParseException("File entry is empty");
        }

        if (sourceMapRoot.has("sections")) {
            // Looks like a index map, try to parse it that way.
            parseMetaMap(sourceMapRoot, sectionSupplier);
            return;
        }

        lineCount = sourceMapRoot.has("lineCount") ? sourceMapRoot.get("lineCount").getAsInt() : -1;
        String lineMap = sourceMapRoot.get("mappings").getAsString();

        sources = getJavaStringArray(sourceMapRoot.get("sources").getAsJsonArray());
        names = getJavaStringArray(sourceMapRoot.get("names").getAsJsonArray());

        if (lineCount >= 0) {
            lines = new ArrayList<>(lineCount);
        } else {
            lines = new ArrayList<>();
        }

        if (sourceMapRoot.has("sourceRoot")) {
            sourceRoot = sourceMapRoot.get("sourceRoot").getAsString();
        }

        for (Map.Entry<String, JsonElement> entry : sourceMapRoot.entrySet()) {
            if (entry.getKey().startsWith("x_")) {
                extensions.put(entry.getKey(), entry.getValue());
            }
        }

        new MappingBuilder(lineMap).build();
    } catch (JsonParseException ex) {
        throw new SourceMapParseException("JSON parse exception: " + ex);
    }
}

From source file:com.google.debugging.sourcemap.SourceMapObject.java

License:Apache License

/**
 * Construct a new {@link SourceMapObject} from the source JSON.
 *///from   ww  w . j av a 2  s  . co m
public SourceMapObject(String contents) throws SourceMapParseException {
    try {
        JsonObject sourceMapRoot = new Gson().fromJson(contents, JsonObject.class);

        version = sourceMapRoot.get("version").getAsInt();
        file = getStringOrNull(sourceMapRoot, "file");
        lineCount = sourceMapRoot.has("lineCount") ? sourceMapRoot.get("lineCount").getAsInt() : -1;
        mappings = getStringOrNull(sourceMapRoot, "mappings");
        sourceRoot = getStringOrNull(sourceMapRoot, "sourceRoot");

        if (sourceMapRoot.has("sections")) {
            ImmutableList.Builder<SourceMapSection> builder = ImmutableList.builder();
            for (JsonElement each : sourceMapRoot.get("sections").getAsJsonArray()) {
                builder.add(buildSection(each.getAsJsonObject()));
            }
            sections = builder.build();
        } else {
            sections = null;
        }

        sources = getJavaStringArray(sourceMapRoot.get("sources"));
        names = getJavaStringArray(sourceMapRoot.get("names"));

        Map<String, Object> extensions = new LinkedHashMap<>();
        for (Map.Entry<String, JsonElement> entry : sourceMapRoot.entrySet()) {
            if (entry.getKey().startsWith("x_")) {
                extensions.put(entry.getKey(), entry.getValue());
            }
        }
        this.extensions = Collections.unmodifiableMap(extensions);
    } catch (JsonParseException ex) {
        throw new SourceMapParseException("JSON parse exception: " + ex);
    }
}

From source file:com.google.debugging.sourcemap.SourceMapObjectParser.java

License:Apache License

public static SourceMapObject parse(String contents) throws SourceMapParseException {

    SourceMapObject.Builder builder = SourceMapObject.builder();

    try {/*  ww  w.j a  va2 s  . c om*/
        JsonObject sourceMapRoot = new Gson().fromJson(contents, JsonObject.class);

        builder.setVersion(sourceMapRoot.get("version").getAsInt());
        builder.setFile(getStringOrNull(sourceMapRoot, "file"));
        builder.setLineCount(sourceMapRoot.has("lineCount") ? sourceMapRoot.get("lineCount").getAsInt() : -1);
        builder.setMappings(getStringOrNull(sourceMapRoot, "mappings"));
        builder.setSourceRoot(getStringOrNull(sourceMapRoot, "sourceRoot"));

        if (sourceMapRoot.has("sections")) {
            ImmutableList.Builder<SourceMapSection> listBuilder = ImmutableList.builder();
            for (JsonElement each : sourceMapRoot.get("sections").getAsJsonArray()) {
                listBuilder.add(buildSection(each.getAsJsonObject()));
            }
            builder.setSections(listBuilder.build());
        }

        builder.setSources(getJavaStringArray(sourceMapRoot.get("sources")));
        builder.setNames(getJavaStringArray(sourceMapRoot.get("names")));

        Map<String, Object> extensions = new LinkedHashMap<>();
        for (Map.Entry<String, JsonElement> entry : sourceMapRoot.entrySet()) {
            if (entry.getKey().startsWith("x_")) {
                extensions.put(entry.getKey(), entry.getValue());
            }
        }
        builder.setExtensions(Collections.unmodifiableMap(extensions));

    } catch (JsonParseException ex) {
        throw new SourceMapParseException("JSON parse exception: " + ex);
    }

    return builder.build();
}

From source file:com.google.gdt.googleapi.core.ApiDirectoryListingJsonCodec.java

License:Open Source License

protected void populateApiInfoFromJson(URL baseURL, JsonObject object, MutableApiInfo info) {
    if (object.has("name")) {
        info.setName(object.get("name").getAsString());
    }//from   w w w .j  a  va 2 s .  c  o  m
    if (object.has("version")) {
        info.setVersion(object.get("version").getAsString());
    }
    if (object.has("title")) {
        info.setDisplayName(object.get("title").getAsString());
    }
    if (object.has("publisher")) {
        info.setPublisher(object.get("publisher").getAsString());
    }
    if (object.has("description")) {
        info.setDescription(object.get("description").getAsString());
    }
    if (object.has("icons")) {
        JsonObject iconLinks = object.getAsJsonObject("icons");
        Set<Entry<String, JsonElement>> iconLinksEntrySet = iconLinks.entrySet();
        for (Entry<String, JsonElement> entry : iconLinksEntrySet) {
            try {
                info.putIconLink(entry.getKey(), new URL(baseURL, entry.getValue().getAsString()));
            } catch (MalformedURLException e) {
                // TODO Add logging warn
            }
        }
    }
    if (object.has("labels")) {
        JsonArray labelsJsonArray = object.getAsJsonArray("labels");

        for (JsonElement labelElement : labelsJsonArray) {
            info.addLabel(labelElement.getAsString());
        }
    }
    if (object.has("releaseDate")) {
        try {
            LocalDate date = new LocalDate(object.get("releaseDate").getAsString());
            info.setReleaseDate(date);
        } catch (IllegalArgumentException e) {
            throw new JsonParseException(e);
        }
    }
    if (object.has("releaseNotesLink")) {
        try {
            info.setReleaseNotesLink(new URL(baseURL, object.get("releaseNotesLink").getAsString()));
        } catch (MalformedURLException e) {
            // TODO Add logging warn
        }
    }
    if (object.has("ranking")) {
        info.setRanking(object.get("ranking").getAsInt());
    }
    if (object.has("discoveryLink")) {
        try {
            info.setDiscoveryLink(new URL(baseURL, object.get("discoveryLink").getAsString()));
        } catch (MalformedURLException e) {
            // TODO Add logging warn
        }
    }
    if (object.has("documentationLink")) {
        try {
            info.setDocumentationLink(new URL(baseURL, object.get("documentationLink").getAsString()));
        } catch (MalformedURLException e) {
            // TODO Add logging warn
        }
    }
    if (object.has("downloadLink")) {
        try {
            info.setDownloadLink(new URL(baseURL, object.get("downloadLink").getAsString()));
        } catch (MalformedURLException e) {
            // TODO Add logging warn
        }
    }
    if (object.has("tosLink")) {
        try {
            info.setTosLink(new URL(baseURL, object.get("tosLink").getAsString()));
        } catch (MalformedURLException e) {
            // TODO Add logging warn
        }
    }
}

From source file:com.google.gerrit.elasticsearch.ElasticIndexVersionDiscovery.java

License:Apache License

List<String> discover(String prefix, String indexName) throws IOException {
    String name = prefix + indexName + "_";
    JestResult result = client.execute(new GetAliases.Builder().addIndex(name + "*").build());
    if (result.isSucceeded()) {
        JsonObject object = result.getJsonObject().getAsJsonObject();
        List<String> versions = new ArrayList<>(object.size());
        for (Entry<String, JsonElement> entry : object.entrySet()) {
            versions.add(entry.getKey().replace(name, ""));
        }/*from  w ww  .  j  a  v  a 2  s .c o  m*/
        return versions;
    }
    return Collections.emptyList();
}