Example usage for com.google.gson JsonElement isJsonArray

List of usage examples for com.google.gson JsonElement isJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonArray.

Prototype

public boolean isJsonArray() 

Source Link

Document

provides check for verifying if this element is an array or not.

Usage

From source file:de.hkneissel.oomph.buildshipimport.impl.BuildshipImportTaskImpl.java

License:Open Source License

private Map<String, String> extractEntries(String name, JsonElement jsonElement) {
    Map<String, String> result = new HashMap<String, String>();

    if (jsonElement.isJsonNull()) {
        // ignore
    } else if (jsonElement.isJsonObject()) {
        result.putAll(extractEntries(jsonElement.getAsJsonObject()));
    } else if (jsonElement.isJsonArray()) {

    } else if (name != null) {
        result.put(name, jsonElement.getAsString());
    }//from   ww  w. ja va2  s. c o  m

    return result;
}

From source file:de.innovationgate.wgpublisher.webtml.utils.JsonUtils.java

License:Open Source License

public Object jsonToJava(JsonElement jsonValue) {
    Object value = null;//from  w  w  w  .  j  a  va  2 s .c om
    if (jsonValue.isJsonNull()) {
        value = null;
    } else if (jsonValue.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) jsonValue;
        if (prim.isNumber()) {
            value = prim.getAsDouble();
        } else if (prim.isBoolean()) {
            value = prim.getAsBoolean();
        } else {
            value = prim.getAsString();
        }
        value = jsonToJavaConversions(value);
    } else if (jsonValue.isJsonArray()) {
        JsonArray array = jsonValue.getAsJsonArray();
        List<Object> list = new ArrayList<Object>();
        for (JsonElement element : array) {
            list.add(jsonToJava(element));
        }
    } else if (jsonValue.isJsonObject()) {
        JsonObject obj = jsonValue.getAsJsonObject();
        Map<String, Object> map = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            map.put(String.valueOf(entry.getKey()), jsonToJava(entry.getValue()));
        }
    }

    return value;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticmi.read.MiJsonMicroImportProcess.java

License:Open Source License

/**
 * Check if the json is valid./*from  w w  w  .  j  ava  2 s.  c o  m*/
 * The JsonObject must contain an result and the result must be a JsonArray. 
 * @return true if the Json is valid, otherwise false
 */
private boolean validate() {
    JsonElement result = json.get("result");
    if (result == null || !result.isJsonArray()) {
        getImportProcessMessages()
                .onMessage(new LocalizedIteraplanMessage(Severity.ERROR, "json.import.exception.notValid"));
        return false;
    }
    return true;
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

@Nonnull
public static ItemStack getItemStack(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }//ww  w .j a  v a 2 s.c  o m

    if (json.isJsonArray()) {
        throw new JsonSyntaxException("Expected value to be an object, not an array");
    }

    if (!json.isJsonObject()) {
        throw new JsonSyntaxException("Expcted value to be an object");
    }

    return getStack((JsonObject) json);
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

private static NonNullList<ItemStack> getStacks(JsonElement json) {
    NonNullList<ItemStack> items = NonNullList.create();

    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }/*from   www .  j av  a  2s.  c  o  m*/

    if (json.isJsonArray()) {
        json.getAsJsonArray().forEach(elem -> {
            if (elem != null && elem.isJsonObject()) {
                items.addAll(getStacks(elem));
            } else {
                throw new JsonSyntaxException("Expcted stack to be an object");
            }
        });
    } else if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();

        if (jsonObj.has("type")
                && MiscUtils.defIfNull(jsonObj.get("type").getAsString(), "").equals("forge:ore_dict")) {
            String oredictName = jsonObj.get("ore").getAsString();
            items.addAll(OreDictionary.getOres(oredictName).stream().map(ItemStack::copy)
                    .collect(Collectors.toList()));
        } else {
            items.add(getStack(jsonObj));
        }
    } else {
        throw new JsonSyntaxException("Expected stack(s) to be an object or an array of objects");
    }

    return items;
}

From source file:de.sanandrew.mods.turretmod.registry.assembly.TurretAssemblyRecipes.java

License:Creative Commons License

@SuppressWarnings("RedundantThrows")
private static boolean registerJsonRecipes(JsonObject json, final ITurretAssemblyRegistry registry)
        throws JsonParseException, IOException {
    String id = JsonUtils.getStringVal(json.get("id"));
    String group = JsonUtils.getStringVal(json.get("group"));
    int fluxPerTick = JsonUtils.getIntVal(json.get("fluxPerTick"));
    int ticksProcessing = JsonUtils.getIntVal(json.get("ticksProcessing"));
    ItemStack result = JsonUtils.getItemStack(json.get("result"));

    JsonElement ingredients = json.get("ingredients");
    if (!ingredients.isJsonArray()) {
        throw new JsonSyntaxException("Ingredients must be an array");
    }/*from ww  w  .j a  va2  s . com*/
    List<IRecipeEntry> entries = new ArrayList<>();
    ingredients.getAsJsonArray().forEach(elem -> {
        if (elem != null && elem.isJsonObject()) {
            JsonObject elemObj = elem.getAsJsonObject();
            int count = JsonUtils.getIntVal(elemObj.get("count"));
            boolean showTooltipText = JsonUtils.getBoolVal(elemObj.get("showTooltipText"), false);
            NonNullList<ItemStack> items = JsonUtils.getItemStacks(elemObj.get("items"));

            int sz = items.size();
            if (sz > 0) {
                IRecipeEntry entry = new RecipeEntry(count).put(items.toArray(new ItemStack[sz]));
                if (showTooltipText) {
                    entry.drawTooltip();
                }

                entries.add(entry);
            }
        }
    });

    return registry.registerRecipe(UUID.fromString(id), registry.getGroup(group), result, fluxPerTick,
            ticksProcessing, entries.toArray(new IRecipeEntry[entries.size()]));
}

From source file:de.sub.goobi.helper.HelperSchritte.java

License:Open Source License

private void replaceJsonElement(JsonElement jel, VariableReplacer replacer) {
    if (jel.isJsonObject()) {
        JsonObject obj = jel.getAsJsonObject();
        for (Entry<String, JsonElement> objEntry : obj.entrySet()) {
            if (objEntry.getValue().isJsonPrimitive()) {
                JsonPrimitive jPrim = objEntry.getValue().getAsJsonPrimitive();
                if (jPrim.isString()) {
                    String newVal = replacer.replace(jPrim.getAsString());
                    obj.addProperty(objEntry.getKey(), newVal);
                }//from   www .ja v  a 2  s . c  om
            } else {
                replaceJsonElement(objEntry.getValue(), replacer);
            }
        }
    } else if (jel.isJsonArray()) {
        JsonArray jArr = jel.getAsJsonArray();
        for (int i = 0; i < jArr.size(); i++) {
            JsonElement innerJel = jArr.get(i);
            if (innerJel.isJsonPrimitive()) {
                JsonPrimitive jPrim = innerJel.getAsJsonPrimitive();
                if (jPrim.isString()) {
                    String newVal = replacer.replace(jPrim.getAsString());
                    jArr.set(i, new JsonPrimitive(newVal));
                }
            } else {
                replaceJsonElement(innerJel, replacer);
            }
        }
    }

}

From source file:de.winniehell.battlebeavers.storage.StepDeserializer.java

License:Open Source License

@Override
public Step deserialize(final JsonElement pJson, final Type pType, final JsonDeserializationContext pContext)
        throws JsonParseException {
    if (!pJson.isJsonArray()) {
        return null;
    }/*from ww w.  j  a  va 2 s  . c  om*/

    final JsonArray array = pJson.getAsJsonArray();

    if (array.size() != 2) {
        return null;
    }

    return dummyPath.new Step(array.get(0).getAsInt(), array.get(1).getAsInt());
}

From source file:de.winniehell.battlebeavers.storage.TileDeserializer.java

License:Open Source License

@Override
public Tile deserialize(final JsonElement pJson, final Type pType, final JsonDeserializationContext pContext)
        throws JsonParseException {
    if (!pJson.isJsonArray()) {
        return null;
    }//w  w w.ja  v a2 s. co m

    final JsonArray array = pJson.getAsJsonArray();

    if (array.size() != 2) {
        return null;
    }

    return new Tile(array.get(0).getAsInt(), array.get(1).getAsInt());
}

From source file:de.winniehell.battlebeavers.storage.WeightedPathDeserializer.java

License:Open Source License

@Override
public WeightedPath deserialize(final JsonElement pJson, final Type pType,
        final JsonDeserializationContext pContext) throws JsonParseException {
    if (!pJson.isJsonArray()) {
        return null;
    }//from  w  w  w  .  j a  v  a2 s.com

    // TODO read path cost
    final WeightedPath path = new WeightedPath(0);

    for (final JsonElement step : pJson.getAsJsonArray()) {
        path.append((Step) pContext.deserialize(step, Step.class));
    }

    return path;
}