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:blusunrize.immersiveengineering.client.models.ModelData.java

public static ModelData fromJson(JsonObject customData, Collection<String> knownKeys, String modelKey,
        ImmutableMap<String, String> texReplacements) {
    String baseLocStr = customData.get(modelKey).getAsString();
    ResourceLocation baseLoc = new ResourceLocation(baseLocStr);
    JsonObject customBase = new JsonObject();
    if (customData.has("custom"))
        customBase = customData.get("custom").getAsJsonObject();
    for (Entry<String, JsonElement> e : customData.entrySet())
        if (!knownKeys.contains(e.getKey()) && !customBase.has(e.getKey()))
            customBase.add(e.getKey(), e.getValue());
    if (customData.has("textures")) {
        JsonObject obj = customData.get("textures").getAsJsonObject();
        ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
        b.putAll(texReplacements);/*w w w  .  j  a  v a  2 s  .  c o m*/
        b.putAll(asMap(obj, true));
        texReplacements = b.build();
    }
    return new ModelData(baseLoc, customBase, texReplacements);
}

From source file:blusunrize.immersiveengineering.client.models.ModelData.java

public static ImmutableMap<String, String> asMap(JsonObject obj, boolean cleanStrings) {
    ImmutableMap.Builder<String, String> ret = new Builder<>();
    for (Entry<String, JsonElement> entry : obj.entrySet())
        if (cleanStrings && entry.getValue().isJsonPrimitive()
                && entry.getValue().getAsJsonPrimitive().isString())
            ret.put(entry.getKey(), entry.getValue().getAsString());
        else/*w  ww  .  j a  v  a  2s .com*/
            ret.put(entry.getKey(), entry.getValue().toString());
    return ret.build();
}

From source file:blusunrize.immersiveengineering.client.models.multilayer.MultiLayerModel.java

@Nonnull
@Override/*from  w  w  w  .j  a  v a  2  s.  co  m*/
public IModel process(ImmutableMap<String, String> customData) {
    Map<BlockRenderLayer, List<ModelData>> newSubs = new HashMap<>();
    JsonParser parser = new JsonParser();
    Map<String, String> unused = new HashMap<>();
    for (String layerStr : customData.keySet())
        if (LAYERS_BY_NAME.containsKey(layerStr)) {

            BlockRenderLayer layer = LAYERS_BY_NAME.get(layerStr);
            JsonElement ele = parser.parse(customData.get(layerStr));
            if (ele.isJsonObject()) {
                ModelData data = ModelData.fromJson(ele.getAsJsonObject(), ImmutableList.of(),
                        ImmutableMap.of());
                newSubs.put(layer, ImmutableList.of(data));
            } else if (ele.isJsonArray()) {
                JsonArray array = ele.getAsJsonArray();
                List<ModelData> models = new ArrayList<>();
                for (JsonElement subEle : array)
                    if (subEle.isJsonObject())
                        models.add(ModelData.fromJson(ele.getAsJsonObject(), ImmutableList.of(),
                                ImmutableMap.of()));
                newSubs.put(layer, models);
            }
        } else
            unused.put(layerStr, customData.get(layerStr));
    JsonObject unusedJson = ModelData.asJsonObject(unused);
    for (Entry<BlockRenderLayer, List<ModelData>> entry : newSubs.entrySet())
        for (ModelData d : entry.getValue())
            for (Entry<String, JsonElement> entryJ : unusedJson.entrySet())
                if (!d.data.has(entryJ.getKey()))
                    d.data.add(entryJ.getKey(), entryJ.getValue());
    if (!newSubs.equals(subModels))
        return new MultiLayerModel(newSubs);
    return this;
}

From source file:br.com.thiaguten.contrib.JsonContrib.java

License:Apache License

private static void deepJsonObjectSearchToJsonObjectFlattened(JsonObject jsonObject,
        JsonObject jsonObjectFlattened) {
    if (jsonObject == null || jsonObjectFlattened == null) {
        throw new IllegalArgumentException(
                "JsonObject and/or JsonObjectFlattened parameter(s) must not be null");
    }//from  ww w . j a va 2s .c  om

    for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        String key = entry.getKey();
        JsonElement value = entry.getValue();

        if (value.isJsonObject()) {
            deepJsonObjectSearchToJsonObjectFlattened((JsonObject) value, jsonObjectFlattened);
        } else if (value.isJsonArray()) {
            // Creates new flattened JsonArray instance.
            JsonArray jsonArray = new JsonArray();
            // Iterates recursively in JsonArray (value), checking content and populating the flattened JsonArray instance.
            deepJsonArraySearchToJsonArrayFlattened((JsonArray) value, jsonArray);
            // Adds the flattened JsonArray instance in the flattened JsonObject instance.
            jsonObjectFlattened.add(key, jsonArray);
        } else {
            // FIXME - WORKAROUND
            // Attention: A map can not contain duplicate keys. So, the
            // duplicate key is concatenated with a counter, to avoid data
            // loss. I could not think of anything better yet.
            if (jsonObjectFlattened.has(key)) {
                jsonObjectFlattened.add(key + COUNTER.getAndIncrement(), value);
            } else {
                jsonObjectFlattened.add(key, value);
            }
        }
    }
}

From source file:br.unicamp.cst.bindings.soar.JSoarCodelet.java

License:Open Source License

public ArrayList<Object> getCommandsJSON(String package_with_beans_classes) {
    ArrayList<Object> commandList = new ArrayList<Object>();
    JsonObject templist = getJsoar().getOutputLinkJSON();
    Set<Map.Entry<String, JsonElement>> set = templist.entrySet();
    Iterator<Entry<String, JsonElement>> it = set.iterator();
    while (it.hasNext()) {
        Entry<String, JsonElement> entry = it.next();
        String key = entry.getKey();
        JsonObject commandtype = entry.getValue().getAsJsonObject();
        try {//from w  ww .j  a va 2 s .co  m
            Class type = Class.forName(package_with_beans_classes + "." + key);
            Object command = type.newInstance();
            type.cast(command);
            for (Field field : type.getFields()) {
                if (commandtype.has(field.getName())) {
                    if (commandtype.get(field.getName()).getAsJsonPrimitive().isNumber()) {
                        field.set(command, commandtype.get(field.getName()).getAsFloat());

                    } else if (commandtype.get(field.getName()).getAsJsonPrimitive().isBoolean()) {
                        field.set(command, commandtype.get(field.getName()).getAsBoolean());

                    } else {
                        field.set(command, commandtype.get(field.getName()).getAsString());
                    }
                }
            }
            commandList.add(command);

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

From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java

License:Open Source License

public void buildWmeInputTreeFromJson(JsonObject json, Identifier id) {
    Set<Map.Entry<String, JsonElement>> entryset = json.entrySet();
    Entry<String, JsonElement> entry;
    Object value;//from www  .java  2 s.  c  om
    Iterator<Entry<String, JsonElement>> itr = entryset.iterator();
    while (itr.hasNext()) {
        entry = itr.next();
        String key = entry.getKey();
        if (entry.getValue().isJsonPrimitive()) {
            if (entry.getValue().getAsJsonPrimitive().isNumber()) {
                value = (double) entry.getValue().getAsJsonPrimitive().getAsDouble();
                createFloatWME(id, key, (double) value);
            } else if (entry.getValue().getAsJsonPrimitive().isString()) {
                value = (String) entry.getValue().getAsJsonPrimitive().getAsString();
                createStringWME(id, key, (String) value);
            } else if (entry.getValue().getAsJsonPrimitive().isBoolean()) {
                value = (Boolean) entry.getValue().getAsJsonPrimitive().getAsBoolean();
                createStringWME(id, key, value.toString());
            }
        } else if (entry.getValue().isJsonObject()) {
            Identifier newID = createIdWME(id, key);
            if (entry.getValue().getAsJsonObject().size() == 0) {
                continue;
            }
            buildWmeInputTreeFromJson(entry.getValue().getAsJsonObject(), newID);
        }
    }
}

From source file:buri.ddmsence.util.Util.java

License:Open Source License

/**
 * Adds a value to a JSON object, but only if it is not empty and not null.
 * /*from  w  ww.jav a  2s  .  c om*/
 * @param object the object to add to
 * @param name the name of the array, if added
 * @param value the value to add
 */
public static void addNonEmptyJsonProperty(JsonObject object, String name, Object value) {
    if (value == null)
        return;
    if (value instanceof AbstractAttributeGroup) {
        AbstractAttributeGroup castValue = (AbstractAttributeGroup) value;
        if (!castValue.isEmpty()) {
            if (Boolean.valueOf(PropertyReader.getProperty("output.json.inlineAttributes"))) {
                JsonObject enclosure = castValue.getJSONObject();
                for (Entry<String, JsonElement> entry : enclosure.entrySet()) {
                    object.add(entry.getKey(), entry.getValue());
                }
            } else {
                object.add(name, castValue.getJSONObject());
            }
        }
    } else if (value instanceof Boolean) {
        Boolean castValue = (Boolean) value;
        object.addProperty(name, castValue);
    } else if (value instanceof Double) {
        Double castValue = (Double) value;
        object.addProperty(name, castValue);
    } else if (value instanceof Integer) {
        Integer castValue = (Integer) value;
        object.addProperty(name, castValue);
    } else if (value instanceof JsonArray) {
        JsonArray castValue = (JsonArray) value;
        if (castValue.size() != 0)
            object.add(name, castValue);
    } else if (value instanceof JsonObject) {
        JsonObject castValue = (JsonObject) value;
        object.add(name, castValue);
    } else if (value instanceof String) {
        String castValue = (String) value;
        if (!Util.isEmpty(castValue))
            object.addProperty(name, castValue);
    } else
        throw new IllegalArgumentException("Unexpected class for JSON property: " + value);
}

From source file:burp.Helper.java

License:Apache License

public String parseSchemaParams(String param, JsonObject definitions) {
    Gson gson = new Gson();
    String result = "";

    for (Map.Entry<String, JsonElement> entry : definitions.entrySet()) {
        if (entry.getKey().equals(param)) {
            Schema schema = gson.fromJson(entry.getValue(), Schema.class);

            if (schema.getProperties() != null) {
                for (Map.Entry<String, JsonElement> entry1 : schema.getProperties().entrySet()) {
                    for (Map.Entry<String, JsonElement> entry2 : entry1.getValue().getAsJsonObject()
                            .entrySet()) {
                        if (entry2.getKey().equals("type")) {
                            result += entry1.getKey() + "={" + entry2.getValue().getAsString() + "}&";
                        } else if (entry2.getKey().equals("$ref")) {
                            String[] parts = entry2.getValue().getAsString().split("/");

                            result += parseSchemaParams(parts[parts.length - 1], definitions);
                        }//w  ww .  jav a 2s . c o  m
                    }
                }
            }
        }
    }

    return result;
}

From source file:ca.ualberta.cs.team1travelexpenseapp.gsonUtils.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }//from   w w w . j  a va2 s .  c o m

    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>() {
        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
            }
            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);
            }
            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);
        }
    };
}

From source file:cc.kave.commons.utils.json.RuntimeTypeAdapterFactory.java

License:Apache License

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

    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>() {
        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            // (kave adaptation) was: ".remove(typeFiledName)"
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
            }
            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 {
            if (value == null) {
                Streams.write(null, out);
                return;
            }
            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();
            // (kave adaptation) disabled check
            // if (jsonObject.has(typeFieldName)) {
            // throw new JsonParseException("cannot serialize " +
            // srcType.getName()
            // + " because it already defines a field named " +
            // typeFieldName);
            // }
            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);
        }
    };
}