Example usage for com.google.gson JsonElement getAsNumber

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

Introduction

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

Prototype

public Number getAsNumber() 

Source Link

Document

convenience method to get this element as a Number .

Usage

From source file:org.terasology.rendering.nui.widgets.models.JsonTreeConverter.java

License:Apache License

/**
 * @param name The name to be given to this node (if null, is replaced by a default name).
 * @param json The {@link JsonElement} to be converted to a {@link JsonTree}.
 * @return A tree representation of the JSON hierarchy.
 *//*from  ww w  .j  a  v  a  2  s  .  co m*/
private static JsonTree serialize(String name, JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return new JsonTree(new JsonTreeNode(name, json.getAsBoolean(),
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        } else if (primitive.isNumber()) {
            return new JsonTree(new JsonTreeNode(name, json.getAsNumber(),
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        } else if (primitive.isString()) {
            return new JsonTree(new JsonTreeNode(name, json.getAsString(),
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        } else {
            return new JsonTree(new JsonTreeNode(name, null,
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        }
    } else if (json.isJsonArray()) {
        JsonTree tree = new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.ARRAY));
        JsonArray array = json.getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            tree.addChild(serialize(array.get(i)));
        }
        return tree;
    } else if (json.isJsonObject()) {
        JsonTree tree = new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.OBJECT));
        JsonObject object = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            tree.addChild(serialize(entry.getKey(), entry.getValue()));
        }
        return tree;
    } else {
        return new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.NULL));
    }
}

From source file:org.terasology.rendering.nui.widgets.treeView.JsonTreeConverter.java

License:Apache License

/**
 * @param name The name to be given to this node (if null, is replaced by a default name).
 * @param json The {@link JsonElement} to be converted to a {@link JsonTree}.
 * @return A tree representation of the JSON hierarchy.
 *///from  w  ww  .  j  a v a 2 s.c  o  m
private static JsonTree serialize(String name, JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return new JsonTree(new JsonTreeValue(name, json.getAsBoolean(),
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        } else if (primitive.isNumber()) {
            return new JsonTree(new JsonTreeValue(name, json.getAsNumber(),
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        } else if (primitive.isString()) {
            return new JsonTree(new JsonTreeValue(name, json.getAsString(),
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        } else {
            return new JsonTree(new JsonTreeValue(name, null,
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        }
    } else if (json.isJsonArray()) {
        JsonTree tree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.ARRAY));
        JsonArray array = json.getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            tree.addChild(serialize(array.get(i)));
        }
        return tree;
    } else if (json.isJsonObject()) {
        JsonTree tree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.OBJECT));
        JsonObject object = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            tree.addChild(serialize(entry.getKey(), entry.getValue()));
        }
        return tree;
    } else {
        return new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.NULL));
    }
}

From source file:org.virtue.cache.tools.GrandExchangeDumper.java

License:Open Source License

public static void main(String[] args) throws IOException {
    Cache cache = new Cache(FileStore.open(Constants.CACHE_REPOSITORY));
    ObjTypeList.init(cache, Constants.ITEM_DATA);
    JsonObject obj = new JsonObject();
    JsonArray arr = new JsonArray();
    DecimalFormat format = new DecimalFormat("###");

    int itemID = 0;

    File file = new File("./repository/prices.json");
    if (file.exists()) {
        JsonElement el = new JsonParser().parse(new FileReader(file));
        JsonArray entries = el.getAsJsonObject().get("items").getAsJsonArray();
        for (int index = 0; index < entries.size(); index++) {
            JsonObject ob = entries.get(index).getAsJsonObject();
            arr.add(ob);/*  w  ww  .  j a  va 2  s. c om*/
            itemID = ob.get("id").getAsInt() + 1;
        }
    }

    int lastSleep = 0;

    double amount = 0;
    double index = arr.size();

    for (int id = 0; id < ObjTypeList.getInstance().getSize(); id++) {
        ObjType type = ObjTypeList.getInstance().list(id);
        if (type != null && type.stockmarket) {
            amount++;
        }
    }

    System.out.println("Total Items: " + amount + ", Starting ID: " + itemID);

    for (int id = itemID; id < ObjTypeList.getInstance().getSize(); id++) {
        ObjType type = ObjTypeList.getInstance().list(id);
        if (type != null && type.stockmarket) {
            try {
                URL link = new URL("http://api.rsapi.net/ge/item/" + id + ".json");
                InputStream stream = link.openStream();

                while (stream.available() < 1) {
                    ;
                }

                byte[] bytes = new byte[stream.available()];
                stream.read(bytes);
                stream.close();

                String json = new String(bytes);
                String parse = json.replace("[{", "{").replace("}]", "}").replace("\\/", "/");

                JsonElement element = new JsonParser().parse(parse);

                if (element.isJsonNull())
                    continue;

                try {
                    element.getAsJsonObject();
                } catch (Exception e) {
                    continue;
                }

                JsonElement exact = element.getAsJsonObject().get("prices").getAsJsonObject().get("exact");

                JsonObject item = new JsonObject();
                item.addProperty("id", id);
                item.addProperty("price", exact.getAsNumber());
                arr.add(item);

                lastSleep++;
                index++;

                if (lastSleep == 100) {
                    lastSleep = 0;
                    Thread.sleep(TimeUnit.MINUTES.toMillis(2));
                    System.out.println(format.format((index / amount) * 100) + "%");
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }

    obj.add("items", arr);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    FileWriter writer = new FileWriter("./repository/prices.json");
    writer.write(gson.toJson(obj));
    writer.close();
}

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

private static NVBase<?> guessNVBaseArray(JsonArray ja) {
    NVBase<?> ret = null;//from   www.j av a  2 s .  c om

    GNVType guess = null;
    for (int i = 0; i < ja.size(); i++) {
        JsonElement je = ja.get(i);

        if (je.isJsonObject()) {
            // could an NVEntity or NVPairList or NVGenericMap
            // nvpair
            JsonObject jo = je.getAsJsonObject();
            if (jo.size() == 1) {

                if (ret == null) {
                    return new NVPairList(null, new ArrayList<NVPair>());
                }
            }

            if (jo.size() > 1) {
                return new NVGenericMapList();

            }
        } else if (je.isJsonPrimitive()) {
            if (je.getAsJsonPrimitive().isString()) {
                // must be fixed
                //break;
                return new NVStringList();
            }

            GNVType gnv = GNVType.toGNVType(je.getAsNumber());
            if (gnv != null) {
                if (guess == null) {
                    guess = gnv;
                } else {
                    switch (gnv) {

                    case NVDOUBLE:
                        if (guess == GNVType.NVINT || guess == GNVType.NVLONG || guess == GNVType.NVFLOAT) {
                            guess = gnv;
                        }
                        break;
                    case NVFLOAT:
                        if (guess == GNVType.NVINT || guess == GNVType.NVLONG) {
                            guess = gnv;
                        }
                        break;
                    case NVINT:
                        break;
                    case NVLONG:
                        if (guess == GNVType.NVINT) {
                            guess = gnv;
                        }
                        break;
                    default:
                        break;

                    }
                }
            }
        }
    }

    if (ret == null && guess != null) {
        switch (guess) {

        case NVDOUBLE:
            ret = new NVDoubleList(null, new ArrayList<Double>());
            break;
        case NVFLOAT:
            ret = new NVFloatList(null, new ArrayList<Float>());
            break;
        case NVINT:
            ret = new NVIntList(null, new ArrayList<Integer>());
            break;
        case NVLONG:
            ret = new NVLongList(null, new ArrayList<Long>());
            break;
        default:
            break;

        }
    }

    return ret;
}