Example usage for com.google.gson JsonSyntaxException JsonSyntaxException

List of usage examples for com.google.gson JsonSyntaxException JsonSyntaxException

Introduction

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

Prototype

public JsonSyntaxException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:cosmos.records.JsonRecords.java

License:Apache License

public static List<MultimapRecord> parseAsMultimap(String json, DocIdGenerator generator)
        throws JsonSyntaxException {
    Preconditions.checkNotNull(json);// ww w  .ja v a  2  s  .  co m
    Preconditions.checkNotNull(generator);

    JsonParser parser = new JsonParser();
    JsonElement topLevelElement = parser.parse(json);

    if (topLevelElement.isJsonNull()) {
        return Collections.emptyList();
    } else if (!topLevelElement.isJsonArray()) {
        throw new JsonSyntaxException("Expected a list of dictionaries");
    }

    final LinkedList<MultimapRecord> records = Lists.newLinkedList();

    // Walk the top level list
    JsonArray list = topLevelElement.getAsJsonArray();
    for (JsonElement e : list) {
        // Make sure that it's a Json Object
        if (!e.isJsonObject()) {
            throw new JsonSyntaxException("Expected a Json Object");
        }

        // Parse each "Object" (map)
        JsonObject map = e.getAsJsonObject();

        records.add(asMultimapRecord(map, generator));
    }

    return records;
}

From source file:de.justi.yagw2api.mumblelink.impl.MumbleLinkAvatar.java

License:Apache License

private static IMumbleLinkAvatar ofWithRetry(final String json, final String originalJson) {
    checkNotNull(originalJson, "missing originalJson");
    checkNotNull(json, "missing json");
    try {/*  w ww  .  ja  v a  2  s .  co m*/
        return GSON.fromJson(json, MumbleLinkAvatar.class);
    } catch (JsonSyntaxException e) {
        LOGGER.trace("Invalid json: {}", json);

        final int end = json.indexOf('}') + 1;
        if (end > 0 && end < json.length()) {
            return ofWithRetry(json.substring(0, end), originalJson);
        } else {
            throw new JsonSyntaxException("Invalid json: " + originalJson);
        }
    }
}

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

License:Creative Commons License

public static float getFloatVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }/*from   ww  w  . j  av  a2 s. c o  m*/

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsFloat();
}

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

License:Creative Commons License

public static String getStringVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        return null;
    }//from w w  w . ja v  a 2  s .co  m

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsString();
}

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

License:Creative Commons License

public static int getIntVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }/* w  w  w.  j  a  va  2  s .c o  m*/

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsInt();
}

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

License:Creative Commons License

public static boolean getBoolVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }/*  w ww. j  av  a2  s .c o  m*/

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsBoolean();
}

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");
    }/*from  w ww.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

@Nonnull
public static NonNullList<ItemStack> getItemStacks(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }/*w ww  .  j  a  v a2s .  co  m*/

    return getStacks(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");
    }/*ww w .j a  v  a  2  s.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

private static boolean processJson(Path file,
        Ex2Function<JsonObject, Boolean, JsonParseException, IOException> callback) {
    if (!"json".equals(FilenameUtils.getExtension(file.toString()))
            || FilenameUtils.getName(file.toString()).startsWith("_")) {
        return true;
    }//from  w ww. j  a v a2s.c  om

    try (BufferedReader reader = Files.newBufferedReader(file)) {
        JsonObject json = JsonUtils.fromJson(reader, JsonObject.class);

        if (json == null || json.isJsonNull()) {
            throw new JsonSyntaxException("Json cannot be null");
        }

        callback.apply(json);

        return true;
    } catch (JsonParseException e) {
        TmrConstants.LOG.log(Level.ERROR,
                String.format("Parsing error loading assembly table recipe from %s", file), e);
        return false;
    } catch (IOException e) {
        TmrConstants.LOG.log(Level.ERROR, String.format("Couldn't read recipe from %s", file), e);
        return false;
    }
}