Example usage for com.google.gson JsonPrimitive getAsNumber

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

Introduction

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

Prototype

@Override
public Number getAsNumber() 

Source Link

Document

convenience method to get this element as a Number.

Usage

From source file:hdm.stuttgart.esell.router.GeneralObjectDeserializer.java

License:Apache License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        }//from   w w  w.j  a  va2  s.  co  m
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        Object[] result = new Object[array.size()];
        int i = 0;
        for (JsonElement element : array) {
            result[i] = deserialize(element, null, context);
            ++i;
        }
        return result;
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        Map<String, Object> result = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            Object value = deserialize(entry.getValue(), null, context);
            result.put(entry.getKey(), value);
        }
        return result;
    } else {
        throw new JsonParseException("Unknown JSON type for JsonElement " + json.toString());
    }
    return null;
}

From source file:io.thinger.thinger.views.Element.java

License:Open Source License

public static Element createPrimitiveElement(String name, JsonPrimitive primitive, LinearLayout layout,
        boolean output) {
    if (primitive.isBoolean()) {
        return new BoolValue(layout, name, primitive.getAsBoolean(), output);
    } else if (primitive.isNumber()) {
        return new NumberValue(layout, name, primitive.getAsNumber(), output);
    } else if (primitive.isString()) {
        return new StringValue(layout, name, primitive.getAsString(), output);
    }/*from  w w  w  . j  a v a2s. c o  m*/
    return null;
}

From source file:it.polimi.tower4clouds.data_analyzer.DAInputDataUnmarshaller.java

License:Apache License

@Override
public Model unmarshal(String inputData) throws Exception {
    //      logger.debug("Unmarshalling data");
    //      long startTime = System.currentTimeMillis();
    Model model = ModelFactory.createDefaultModel();

    JsonArray jsonData = jsonParser.parse(inputData).getAsJsonArray();
    //      logger.debug("{} monitoring datum json object(s) received",
    //            jsonData.size());

    for (JsonElement jsonElement : jsonData) {
        JsonObject jsonDatum = jsonElement.getAsJsonObject();
        Resource resourceDatum = model.createResource(UUID.randomUUID().toString()).addProperty(RDF.type,
                MO.MonitoringDatum);/*from w  w w.j  a va2 s. com*/
        for (Entry<String, JsonElement> pair : jsonDatum.entrySet()) {
            String property = pair.getKey();
            JsonPrimitive value = pair.getValue().getAsJsonPrimitive();
            if (value.isBoolean()) {
                resourceDatum.addProperty(MO.makeProperty(property),
                        model.createTypedLiteral(value.getAsBoolean(), XSDDatatype.XSDboolean));
            } else if (value.isString()) {
                resourceDatum.addProperty(MO.makeProperty(property),
                        model.createTypedLiteral(value.getAsString(), XSDDatatype.XSDstring));
            } else if (value.isNumber()) {
                resourceDatum.addProperty(MO.makeProperty(property),
                        model.createTypedLiteral(value.getAsNumber().doubleValue(), XSDDatatype.XSDdouble));
            } else {
                logger.error("Unknown datum property: {}", value);
            }
        }
    }
    //      logger.debug("Unmarshalling completed in {} seconds",
    //            ((double) (System.currentTimeMillis() - startTime)) / 1000);

    return model;
}

From source file:jp.pay.model.EventDataDeserializer.java

License:Open Source License

private Object deserializeJsonPrimitive(JsonPrimitive element) {
    if (element.isBoolean()) {
        return element.getAsBoolean();
    } else if (element.isNumber()) {
        return element.getAsNumber();
    } else {/* w w w. ja  v a2 s.  com*/
        return element.getAsString();
    }
}

From source file:net.doubledoordev.backend.util.JsonNBTHelper.java

License:Open Source License

/**
 * There is no way to detect number types and NBT is picky about this. Lets hope the original type id is there, otherwise we are royally screwed.
 *//*  www.j  av a 2s. c  o m*/
public static Tag parseJSON(JsonPrimitive element) {
    String string = element.getAsString();
    if (string.contains(":")) {
        String[] split = string.split(":", 2);
        switch (TagType.getByTypeName(split[0])) {
        case TAG_END:
            return new EndTag();
        case TAG_BYTE:
            return new ByteTag("", Byte.parseByte(split[1]));
        case TAG_SHORT:
            return new ShortTag("", Short.parseShort(split[1]));
        case TAG_INT:
            return new IntTag("", Integer.parseInt(split[1]));
        case TAG_LONG:
            return new LongTag("", Long.parseLong(split[1]));
        case TAG_FLOAT:
            return new FloatTag("", Float.parseFloat(split[1]));
        case TAG_DOUBLE:
            return new DoubleTag("", Double.parseDouble(split[1]));
        case TAG_BYTE_ARRAY:
            return parseJSONByteArray(split[1]);
        case TAG_STRING:
            return new StringTag("", split[1]);
        // TAG_LIST != JsonPrimitive
        // TAG_COMPOUND != JsonPrimitive
        case TAG_INT_ARRAY:
            return parseJSONIntArray(split[1]);
        case TAG_SHORT_ARRAY:
            return parseJSONShortArray(split[1]);
        }
    }

    // Now it becomes guesswork.
    if (element.isString())
        return new StringTag("", string);
    if (element.isBoolean())
        return new ByteTag("", element.getAsBoolean());

    Number n = element.getAsNumber();
    if (n instanceof Byte)
        return new ByteTag("", n.byteValue());
    if (n instanceof Short)
        return new ShortTag("", n.shortValue());
    if (n instanceof Integer)
        return new IntTag("", n.intValue());
    if (n instanceof Long)
        return new LongTag("", n.longValue());
    if (n instanceof Float)
        return new FloatTag("", n.floatValue());
    if (n instanceof Double)
        return new DoubleTag("", n.doubleValue());

    try {
        return new IntTag("", Integer.parseInt(element.toString()));
    } catch (NumberFormatException ignored) {

    }
    throw new NumberFormatException(element.getAsNumber() + " is was not able to be parsed.");
}

From source file:net.doubledoordev.backend.util.JsonNBTHelper.java

License:Open Source License

public static JsonPrimitive fixNulls(JsonPrimitive primitive) {
    if (primitive.isBoolean())
        return new JsonPrimitive(primitive.getAsBoolean());
    if (primitive.isNumber())
        return new JsonPrimitive(primitive.getAsNumber());
    if (primitive.isString())
        return new JsonPrimitive(primitive.getAsString());
    return JSONPARSER.parse(primitive.toString()).getAsJsonPrimitive();
}

From source file:net.praqma.tracey.tracey_rabbitmq_neo4j_bridge.Tracey2Neo.java

private Object getPrimitiveType(JsonElement jo) {
    if (!jo.isJsonPrimitive()) {
        return null;
    }//from  w  w  w. j  a  v a  2  s  .c  o  m
    JsonPrimitive jp = jo.getAsJsonPrimitive();
    if (jp.isBoolean()) {
        return jp.getAsBoolean();
    }
    if (jp.isNumber()) {
        return jp.getAsNumber();
    }
    if (jp.isString()) {
        return jp.getAsString();
    }
    return null;
}

From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

public ASBase deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = (JsonObject) el;//  ww  w.  j a  v  a2s . com
    ASBase.Builder<?, ?> builder;
    if (type == Collection.class)
        builder = Collection.makeCollection();
    else if (type == Activity.class)
        builder = Activity.makeActivity();
    else if (type == MediaLink.class)
        builder = MediaLink.makeMediaLink();
    else if (type == PlaceObject.class)
        builder = PlaceObject.makePlace();
    else if (type == Mood.class)
        builder = Mood.makeMood();
    else if (type == Address.class)
        builder = Address.makeAddress();
    else {
        JsonPrimitive ot = obj.getAsJsonPrimitive("objectType");
        if (ot != null) {
            String ots = ot.getAsString();
            Class<? extends ASObject.Builder> _class = objsmap.get(ots);
            if (_class != null) {
                builder = Discover.locate(_class, _class.getName());
                try {
                    builder = _class.getConstructor(String.class).newInstance(ots);
                } catch (Throwable t) {
                }

            } else
                builder = ASObject.makeObject(ots);
        } else {
            if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) {
                builder = Activity.makeActivity();
            } else if (obj.has("items")) {
                builder = Collection.makeCollection();
            } else {
                builder = ASObject.makeObject(); // anonymous
            }
        }
    }
    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        if (name.equalsIgnoreCase("objectType"))
            continue;
        Class<?> _class = map.get(name);
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive()) {
            if (_class != null) {
                builder.set(name, context.deserialize(val, _class));
            } else {
                JsonPrimitive prim = val.getAsJsonPrimitive();
                if (prim.isBoolean())
                    builder.set(name, prim.getAsBoolean());
                else if (prim.isNumber())
                    builder.set(name, prim.getAsNumber());
                else {
                    builder.set(name, prim.getAsString());
                }
            }
        } else if (val.isJsonArray()) {
            ImmutableList.Builder<Object> list = ImmutableList.builder();
            JsonArray arr = val.getAsJsonArray();
            processArray(arr, _class, context, list);
            builder.set(name, list.build());
        } else if (val.isJsonObject()) {
            if (map.containsKey(name)) {
                builder.set(name, context.deserialize(val, map.get(name)));
            } else
                builder.set(name, context.deserialize(val, ASObject.class));
        }
    }
    return builder.get();
}

From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

private void processArray(JsonArray arr, Class<?> _class, JsonDeserializationContext context,
        ImmutableList.Builder<Object> list) {
    for (JsonElement mem : arr) {
        if (mem.isJsonPrimitive()) {
            if (_class != null) {
                list.add(context.deserialize(mem, _class));
            } else {
                JsonPrimitive prim2 = (JsonPrimitive) mem;
                if (prim2.isBoolean())
                    list.add(prim2.getAsBoolean());
                else if (prim2.isNumber())
                    list.add(prim2.getAsNumber());
                else
                    list.add(prim2.getAsString());
            }/*w  w  w  . j a v  a  2 s.c o m*/
        } else if (mem.isJsonObject()) {
            list.add(context.deserialize(mem, _class != null ? _class : ASObject.class));
        } else if (mem.isJsonArray()) {
            JsonArray array = mem.getAsJsonArray();
            ImmutableList.Builder<Object> objs = ImmutableList.builder();
            processArray(array, _class, context, objs);
            list.add(objs.build());
        }
    }
}

From source file:org.apache.abdera2.activities.io.gson.MultimapAdapter.java

License:Apache License

protected static Object primdes(JsonPrimitive prim) {
    if (prim.isBoolean())
        return prim.getAsBoolean();
    else if (prim.isNumber())
        return prim.getAsNumber();
    else//from w  w  w  .  jav  a  2  s  .c  om
        return prim.getAsString();
}