Example usage for com.google.gson JsonElement getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

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

License:Open Source License

public static JsonPrimitive getAsJsonPrimitiveOrThrow(JsonElement element) throws JsonParseException {
    if (!element.isJsonPrimitive()) {
        throw new JsonParseException("this element is not a json primitive: " + element);
    }/*from w  ww.  ja  v  a  2 s.  c  o  m*/
    return element.getAsJsonPrimitive();
}

From source file:jparser.json.adapter.LocalDateAdapter.java

License:Apache License

@Override
public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return LocalDate.parse(json.getAsJsonPrimitive().getAsString());
}

From source file:jsondiscoverer.JsonInjector.java

License:Open Source License

/**
 * Analyzes the type of the attribute to convert the JSON value
 * //ww  w . ja v a 2  s .  co  m
 * @param eAttribute The {@link EAttribute}
 * @param value The value to convert
 * @return The converted value (as {@link JsonPrimitive})
 */
protected Object digestValue(EAttribute eAttribute, JsonElement value) {
    if (eAttribute == null)
        throw new IllegalArgumentException("The eAttribute cannot be null");
    if (value == null)
        throw new IllegalArgumentException("The value cannot be null");

    if (eAttribute.getEType().equals(EcorePackage.Literals.ESTRING)) {
        if (value.isJsonArray() || value.isJsonObject())
            return ""; // TODO Improve discovery process to deal with this
        else
            return value.getAsJsonPrimitive().getAsString();
    } else if (eAttribute.getEType().equals(EcorePackage.Literals.EINT)) {
        return new Integer(value.getAsJsonPrimitive().getAsNumber().intValue());
    } else if (eAttribute.getEType().equals(EcorePackage.Literals.EBOOLEAN)) {
        return value.getAsJsonPrimitive().getAsBoolean() ? Boolean.TRUE : Boolean.FALSE;
    } else {
        return null;
    }
}

From source file:jsondiscoverer.JsonSimpleDiscoverer.java

License:Open Source License

/**
 * Maps JSON types into ECORE types// w ww  .ja  v a2  s.  c  om
 * 
 * @param id Identifier of the feature (to infer the name of the type if non-primitive)
 * @param value {@link JsonElement} including the value
 * @return The mapped type (as {@link EClassifier}
 */
private EClassifier mapType(String id, JsonElement value) {
    if (id == null)
        throw new IllegalArgumentException("id cannot be null");
    if (value == null)
        throw new IllegalArgumentException("jsonObject cannot be null");

    if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
        return EcorePackage.Literals.ESTRING;
    } else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
        return EcorePackage.Literals.EINT;
    } else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isBoolean()) {
        return EcorePackage.Literals.EBOOLEAN;
    } else if (value.isJsonArray()) {
        JsonArray arrayValue = value.getAsJsonArray();
        if (arrayValue.size() > 0) {
            EClassifier generalArrayType = mapType(digestId(id), arrayValue.get(0));
            for (int i = 1; i < arrayValue.size(); i++) {
                JsonElement arrayElement = arrayValue.get(i);
                EClassifier arrayType = mapType(digestId(id), arrayElement);
                if (generalArrayType != arrayType) {
                    LOGGER.finer(
                            "[mapType] Detected array multi-typed, using fallback type (String) for " + id);
                    return EcorePackage.Literals.ESTRING;
                }
            }
            return generalArrayType;
        }
    } else if (value.isJsonObject()) {
        return discoverMetaclass(digestId(id), value.getAsJsonObject());
    }
    LOGGER.finer("[mapType] Type not discovererd for " + id);
    return EcorePackage.Literals.ESTRING;
}

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");

            }//from w  ww.j  a v a  2  s .co m
            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}
 * //w w  w  .  jav 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:mediasearch.twitter.NullJsonStringConverter.java

@Override
public String deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    return json.getAsJsonPrimitive().getAsString();
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public String getString(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) {
        return element.getAsString();
    } else {//from w w w .ja va  2 s.  c om
        throw wrongTypeException(key, "String");
    }
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public boolean getBoolean(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isBoolean()) {
        return element.getAsBoolean();
    } else {/*from   w  ww .  j  a  v  a 2  s .  c  o m*/
        throw wrongTypeException(key, "boolean");
    }
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public Number getNumber(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
        return element.getAsNumber();
    } else {/*from  w w w . ja va 2s.  co  m*/
        throw wrongTypeException(key, "Number");
    }
}