Example usage for com.google.gson JsonPrimitive getAsString

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

Introduction

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

Prototype

@Override
public String getAsString() 

Source Link

Document

convenience method to get this element as a String.

Usage

From source file:it.intecs.pisa.util.json.JsonXpath.java

License:Open Source License

public static String getString(JsonObject obj, String xpath) {
    StringTokenizer tokenizer;/*from   w w w  .j  av a 2s  . co m*/

    tokenizer = new StringTokenizer(xpath, "/");

    Object selectedObject;
    selectedObject = get(obj, tokenizer);

    JsonPrimitive primitive;
    primitive = (JsonPrimitive) selectedObject;

    return (String) primitive.getAsString();
}

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 ww.  jav a2 s  . co m
        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 {/*from w  w w . jav  a2  s  .co m*/
        return element.getAsString();
    }
}

From source file:jp.yokomark.utils.gson.utils.JsonElementUtils.java

License:Open Source License

public static String getAsStringOrThrow(JsonPrimitive primitive) throws JsonParseException {
    if (!primitive.isString()) {
        throw new JsonParseException("this primitive is not a json string: " + primitive);
    }/*from  w w w . j a va 2 s.  co m*/
    return primitive.getAsString();
}

From source file:json.GraphSONNodeDeserializer.java

License:Apache License

private Object getTypedValue(JsonPrimitive valueJson) {
    if (valueJson.isBoolean()) {
        return valueJson.getAsBoolean();
    } else if (valueJson.isNumber()) {
        return valueJson.getAsInt();
    } else {/*from w  ww.j  av  a  2  s. c  o  m*/
        return valueJson.getAsString();
    }
}

From source file:JsonParser.ParseJson.java

public static void dumpJSONElement(JsonElement element, String type) {
    if (element.isJsonObject()) {
        //System.out.println("Is an object");
        JsonObject obj = element.getAsJsonObject();
        java.util.Set<java.util.Map.Entry<String, JsonElement>> entries = obj.entrySet();
        java.util.Iterator<java.util.Map.Entry<String, JsonElement>> iter = entries.iterator();
        while (iter.hasNext()) {
            java.util.Map.Entry<String, JsonElement> entry = iter.next();
            //  System.out.println("Key: " + entry.getKey());
            if (entry.getKey().toString().equals("instances")) {
                System.out.println("............Topic: ");
                dumpJSONElement(entry.getValue(), "topic");

            }/* w ww.  j a v  a  2  s.  c om*/
            if (entry.getKey().toString().equals("aspects")) {
                System.out.println("............aspects: ");
                dumpJSONElement(entry.getValue(), "aspect");

            } else if (entry.getKey().toString().equals("positives")) {
                System.out.println(".............Positive Words ");
                dumpJSONElement(entry.getValue(), "positive");
            } else if (entry.getKey().toString().equals("negatives")) {
                System.out.println(".............negatives Words ");
                dumpJSONElement(entry.getValue(), "negative");
            } else {
                dumpJSONElement(entry.getValue(), "");
            }

        }

    } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        //System.out.println("Is an array. Number of values: " + array.size());
        java.util.Iterator<JsonElement> iter = array.iterator();
        while (iter.hasNext()) {
            JsonElement entry = iter.next();

            dumpJSONElement(entry, "");
        }
    } else if (element.isJsonPrimitive()) {
        //System.out.println("Is a primitive");
        JsonPrimitive value = element.getAsJsonPrimitive();
        if (value.isBoolean()) {
            //System.out.println("Is boolean: " + value.getAsBoolean());
        } else if (value.isNumber()) {
            //  System.out.println("Is number: " + value.getAsNumber());
        } else if (value.isString()) {
            //if(!value.getAsString().equals("empty"))
            //{
            //if(type.equals("topic"))
            //{
            System.out.println(type + " :" + value.getAsString());
            //}
        }
    } else if (element.isJsonNull()) {
        System.out.println("Is NULL");
    } else {
        System.out.println("Error. Unknown type of element");
    }
}

From source file:leola.web.WebLeolaLibrary.java

License:Open Source License

/**
 * Converts the {@link JsonElement} into the equivalent {@link LeoObject}
 * /*from w w w  .ja  v a  2 s  .c  o m*/
 * @param element
 * @return the {@link LeoObject}
 */
private static LeoObject toLeoObject(JsonElement element) {
    if (element == null || element.isJsonNull()) {
        return LeoObject.NULL;
    }

    if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        LeoArray leoArray = new LeoArray(array.size());
        array.forEach(e -> leoArray.add(toLeoObject(e)));
        return leoArray;
    }

    if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        LeoMap leoMap = new LeoMap();
        object.entrySet().forEach(entry -> {
            leoMap.putByString(entry.getKey(), toLeoObject(entry.getValue()));
        });

        return leoMap;
    }

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return LeoObject.valueOf(primitive.getAsBoolean());
        }

        if (primitive.isNumber()) {
            return LeoObject.valueOf(primitive.getAsDouble());
        }

        if (primitive.isString()) {
            return LeoString.valueOf(primitive.getAsString());
        }
    }

    return LeoObject.NULL;
}

From source file:me.boomboompower.togglechat.utils.BetterJsonObject.java

License:Open Source License

/**
 * The optional string method, returns the default value if
 * the key is null, empty or the data does not contain
 * the key. This will also return the default value if
 * the data value is not a string/*  w w w . j  av a 2  s  . co  m*/
 *
 * @param key the key the value will be loaded from
 * @return the value in the json data set or the default if the key cannot be found
 */
public String optString(String key, String value) {
    if (key == null || key.isEmpty() || !has(key)) {
        return value;
    }

    JsonPrimitive primitive = asPrimitive(get(key));

    if (primitive != null && primitive.isString()) {
        return primitive.getAsString();
    }
    return value;
}

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