Example usage for com.google.gson JsonElement isJsonPrimitive

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

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:org.spongepowered.common.util.persistence.JsonTranslator.java

License:MIT License

private static Object convert(JsonElement element) {
    if (element.isJsonObject()) {
        MemoryDataView container = new MemoryDataContainer();
        for (Entry<String, JsonElement> entry : ((JsonObject) element).entrySet()) {
            Object value = convert(entry.getValue());
            if (value != null) {
                container.set(DataQuery.of(entry.getKey()), value);
            }/*from   w w w  .  j a v  a 2  s. com*/
        }
        return container;
    } else if (element.isJsonArray()) {
        return Lists.newArrayList(Iterables.transform((JsonArray) element, JsonTranslator::convert));
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = (JsonPrimitive) element;
        if (primitive.isString()) {
            return primitive.getAsString();
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        }
    }
    return null;
}

From source file:org.springframework.ide.vscode.commons.util.GsonUtil.java

License:Open Source License

public static String getAsString(JsonElement target) {
    // Need to check if it is a primitive before get as string, otherwise
    // exceptions can be thrown. See the JsonElement javadoc
    if (target != null && target.isJsonPrimitive()) {
        return target.getAsString();
    }/*  w ww  . j av  a  2 s  . c o m*/
    return null;
}

From source file:org.structr.core.JsonInputGSONAdapter.java

License:Open Source License

@Override
public JsonInput deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonInput wrapper = new JsonInput();

    if (json.isJsonObject()) {

        JsonObject obj = json.getAsJsonObject();

        for (Entry<String, JsonElement> entry : obj.entrySet()) {

            String key = entry.getKey();
            JsonElement elem = entry.getValue();

            // static mapping of IdProperty if present
            if ((idProperty != null) && "id".equals(key)) {

                key = idProperty.jsonName();

            }/* ww w.  j av a2 s  . c  om*/

            if (elem.isJsonNull()) {

                wrapper.add(key, null);

            } else if (elem.isJsonObject()) {

                wrapper.add(key, deserialize(elem, typeOfT, context));

            } else if (elem.isJsonArray()) {

                JsonArray array = elem.getAsJsonArray();
                List list = new LinkedList();
                for (JsonElement element : array) {
                    if (element.isJsonPrimitive()) {
                        list.add(fromPrimitive((element.getAsJsonPrimitive())));
                    } else if (element.isJsonObject()) {
                        // create map of values
                        list.add(deserialize(element, typeOfT, context));
                    }
                }

                wrapper.add(key, list);

            } else if (elem.isJsonPrimitive()) {

                // wrapper.add(key, elem.getAsString());
                wrapper.add(key, fromPrimitive((JsonPrimitive) elem));
            }

        }

    } else if (json.isJsonArray()) {

        JsonArray array = json.getAsJsonArray();
        for (JsonElement elem : array) {

            if (elem.isJsonPrimitive()) {
                wrapper.add(elem.toString(), ((JsonPrimitive) elem).getAsString());
            } else if (elem.isJsonObject()) {
                wrapper.add(elem.toString(), deserialize(elem, typeOfT, context));
            } else if (elem.isJsonArray()) {
                wrapper.add(elem.toString(), deserialize(elem, typeOfT, context));
            }
        }
    }

    return wrapper;
}

From source file:org.structr.core.PropertySetGSONAdapter.java

License:Open Source License

private PropertySet deserializeFlatNameValue(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) {

    PropertySet wrapper = new PropertySet();

    if (json.isJsonObject()) {

        JsonObject obj = json.getAsJsonObject();

        for (Entry<String, JsonElement> entry : obj.entrySet()) {

            String key = entry.getKey();
            JsonElement elem = entry.getValue();

            // static mapping of IdProperty if present
            if ((idProperty != null) && "id".equals(key)) {

                key = idProperty;/* ww  w  .  j ava 2s. c o m*/

            }

            if (elem.isJsonNull()) {

                wrapper.add(key, null);

            } else if (elem.isJsonObject()) {

                wrapper.add(key, deserializeFlatNameValue(elem, typeOfT, context));

            } else if (elem.isJsonArray()) {

                JsonArray array = elem.getAsJsonArray();
                List list = new LinkedList();
                for (JsonElement element : array) {
                    if (element.isJsonPrimitive()) {
                        list.add(fromPrimitive((element.getAsJsonPrimitive())));
                    } else if (element.isJsonObject()) {
                        // create map of values
                        list.add(deserializeFlatNameValue(element, typeOfT, context));
                    }
                }

                wrapper.add(key, list);

            } else if (elem.isJsonPrimitive()) {

                // wrapper.add(key, elem.getAsString());
                wrapper.add(key, fromPrimitive((JsonPrimitive) elem));
            }

        }

    } else if (json.isJsonArray()) {

        JsonArray array = json.getAsJsonArray();
        for (JsonElement elem : array) {

            if (elem.isJsonPrimitive()) {
                wrapper.add(elem.toString(), ((JsonPrimitive) elem).getAsString());
            } else if (elem.isJsonObject()) {
                wrapper.add(elem.toString(), deserializeFlatNameValue(elem, typeOfT, context));
            } else if (elem.isJsonArray()) {
                wrapper.add(elem.toString(), deserializeFlatNameValue(elem, typeOfT, context));
            }
        }
    }

    return wrapper;
}

From source file:org.structr.core.rest.JsonInputGSONAdapter.java

License:Open Source License

public static JsonInput deserialize(final JsonElement json, final JsonDeserializationContext context)
        throws JsonParseException {

    final JsonInput wrapper = new JsonInput();
    if (json.isJsonObject()) {

        final JsonObject obj = json.getAsJsonObject();

        for (final Entry<String, JsonElement> entry : obj.entrySet()) {

            final String key = entry.getKey();
            final JsonElement elem = entry.getValue();

            if (elem.isJsonNull()) {

                wrapper.add(key, null);/*  www . java2 s.com*/

            } else if (elem.isJsonObject()) {

                wrapper.add(key, deserialize(elem, context));

            } else if (elem.isJsonArray()) {

                final JsonArray array = elem.getAsJsonArray();
                final List list = new LinkedList();

                for (final JsonElement element : array) {

                    if (element.isJsonPrimitive()) {

                        list.add(fromPrimitive((element.getAsJsonPrimitive())));

                    } else if (element.isJsonObject()) {

                        // create map of values
                        list.add(deserialize(element, context));
                    }
                }

                wrapper.add(key, list);

            } else if (elem.isJsonPrimitive()) {

                // wrapper.add(key, elem.getAsString());
                wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive()));
            }

        }

    } else if (json.isJsonArray()) {

        final JsonArray array = json.getAsJsonArray();
        for (final JsonElement elem : array) {

            if (elem.isJsonPrimitive()) {

                wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive()));

            } else if (elem.isJsonObject()) {

                wrapper.add(elem.toString(), deserialize(elem, context));

            } else if (elem.isJsonArray()) {

                wrapper.add(elem.toString(), deserialize(elem, context));
            }
        }

    } else {

        // when we arrive here, the input element was
        // not one of the expected types => error
        throw new JsonSyntaxException("Invalid JSON, expecting object or array");
    }

    return wrapper;
}

From source file:org.structr.rest.JsonInputGSONAdapter.java

License:Open Source License

private JsonInput deserialize(JsonElement json, JsonDeserializationContext context) throws JsonParseException {

    JsonInput wrapper = new JsonInput();
    if (json.isJsonObject()) {

        JsonObject obj = json.getAsJsonObject();

        for (Entry<String, JsonElement> entry : obj.entrySet()) {

            String key = entry.getKey();
            JsonElement elem = entry.getValue();

            if (elem.isJsonNull()) {

                wrapper.add(key, null);//from  ww  w  .j  a va  2s .  c  o  m

            } else if (elem.isJsonObject()) {

                wrapper.add(key, deserialize(elem, context));

            } else if (elem.isJsonArray()) {

                final JsonArray array = elem.getAsJsonArray();
                final List list = new LinkedList();

                for (JsonElement element : array) {

                    if (element.isJsonPrimitive()) {

                        list.add(fromPrimitive((element.getAsJsonPrimitive())));

                    } else if (element.isJsonObject()) {

                        // create map of values
                        list.add(deserialize(element, context));
                    }
                }

                wrapper.add(key, list);

            } else if (elem.isJsonPrimitive()) {

                // wrapper.add(key, elem.getAsString());
                wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive()));
            }

        }

    } else if (json.isJsonArray()) {

        JsonArray array = json.getAsJsonArray();
        for (JsonElement elem : array) {

            if (elem.isJsonPrimitive()) {

                wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive()));

            } else if (elem.isJsonObject()) {

                wrapper.add(elem.toString(), deserialize(elem, context));

            } else if (elem.isJsonArray()) {

                wrapper.add(elem.toString(), deserialize(elem, context));
            }
        }
    }

    return wrapper;
}

From source file:org.sweble.wom3.serialization.Wom3JsonTypeAdapterBase.java

License:Open Source License

protected static Scope registerNsDecl(ScopeStack scopeStack, Scope scope, String entryName,
        JsonElement entryValue) {
    if (!entryValue.isJsonPrimitive())
        throw new JsonParseException("Expected attribute '" + entryName + "' to be a string");

    entryName = entryName.substring(1);/* w  ww .  ja  va2 s.  com*/
    String valueString = entryValue.getAsString();
    if (entryName.equals(XMLNS_PREFIX)) {
        if (scope == null)
            scope = scopeStack.push();
        scope.setXmlns(valueString);
    } else if (entryName.startsWith(XMLNS_COLON_PREFIX)) {
        String prefix = entryName.substring(XMLNS_COLON_PREFIX.length());
        if (scope == null)
            scope = scopeStack.push();
        scope.put(prefix, valueString);
    }
    return scope;
}

From source file:org.sweble.wom3.serialization.Wom3JsonTypeAdapterBase.java

License:Open Source License

protected static void parseAttribute(ScopeStack scopeStack, Element elem, String entryName,
        JsonElement entryValue) {
    if (!entryValue.isJsonPrimitive())
        throw new JsonParseException("Expected attribute '" + entryName + "' to be a string");
    entryName = entryName.substring(1);/*  w ww. j  ava 2  s  . c  o m*/
    String valueString = entryValue.getAsString();
    int i = entryName.indexOf(':');
    if (i == -1) {
        if (XMLNS_PREFIX.equals(entryName)) {
            elem.setAttributeNS(XMLNS_URI, entryName, valueString);
        } else {
            elem.setAttribute(entryName, valueString);
        }
    } else {
        String prefix = entryName.substring(0, i);
        if (XMLNS_PREFIX.equals(prefix)) {
            elem.setAttributeNS(XMLNS_URI, entryName, valueString);
        } else {
            String nsUri = scopeStack.getNsUriForPrefix(prefix);
            if (nsUri == null)
                throw new NamespaceException("Namespace URI for prefix '" + prefix + "' not declared");
            elem.setAttributeNS(nsUri, entryName, valueString);
        }
    }
}

From source file:org.sweble.wom3.serialization.Wom3NodeCompactJsonTypeAdapter.java

License:Open Source License

private static Node fromJson(Wom3Document doc, JsonElement json, ScopeStack scopeStack) {
    if (json.isJsonPrimitive())
        return doc.createTextNode(json.getAsString());

    // Get node object
    if (!json.isJsonObject())
        throw new JsonParseException("Expected JsonObject or JsonPrimitive");
    JsonObject o = json.getAsJsonObject();

    Scope scope = null;/*from ww  w.  jav a2  s  .c o m*/
    String elemQName = null;
    ValueTypes valueType = null;
    JsonElement nodeValue = null;

    // Get node type
    Set<Entry<String, JsonElement>> entries = o.entrySet();
    for (Entry<String, JsonElement> e : entries) {
        String entryName = e.getKey();
        final JsonElement entryValue = e.getValue();
        if (entryName.startsWith(ELEMENT_NAME_PREFIX)) {
            if (elemQName != null)
                throw new JsonParseException("Node name field occurred repeatedly");

            elemQName = entryName.substring(1);
            nodeValue = entryValue;
        } else if (entryName.startsWith(SPECIAL_TYPE_PREFIX)) {
            if (valueType != null)
                throw new JsonParseException("Type name field occurred repeatedly");

            if (entryName.equals(TYPE_RTD)) {
                valueType = ValueTypes.RTD;
            } else if (entryName.equals(TYPE_TEXT)) {
                valueType = ValueTypes.TEXT;
            } else if (entryName.equals(TYPE_ENTITY_REF)) {
                valueType = ValueTypes.ENTITY_REF;
            } else if (entryName.equals(TYPE_COMMENT)) {
                valueType = ValueTypes.COMMENT;
            } else if (entryName.equals(TYPE_CDATA)) {
                valueType = ValueTypes.CDATA;
            } else
                throw new JsonParseException("Unknown special type '" + entryName + "'");

            nodeValue = entryValue;
        } else if (entryName.startsWith(ATTRIBUTE_PREFIX)) {
            scope = registerNsDecl(scopeStack, scope, entryName, entryValue);
        } else
            throw new JsonParseException("Unexpected field: '" + entryName + "'");
    }

    Element elem = null;
    String defaultNsUri = null;

    if (elemQName != null) {
        defaultNsUri = scopeStack.getXmlns();
        elem = createElement(doc, scopeStack, defaultNsUri, elemQName);
    } else if (valueType == null)
        throw new JsonParseException("Missing type name or node name");

    for (Entry<String, JsonElement> e : entries) {
        String entryName = e.getKey();
        JsonElement entryValue = e.getValue();
        if (entryName.startsWith(ATTRIBUTE_PREFIX)) {
            if (elem == null)
                throw new JsonParseException("Value type '" + valueType + "' cannot have attributes");

            parseAttribute(scopeStack, elem, entryName, entryValue);
        }
    }

    Node result;
    if (elem != null) {
        if (!nodeValue.isJsonArray())
            throw new JsonParseException(
                    "Expected member '" + ELEMENT_NAME_PREFIX + elemQName + "' to be an array.");

        JsonArray children = nodeValue.getAsJsonArray();
        for (JsonElement child : children)
            elem.appendChild(fromJson(doc, child, scopeStack));

        result = elem;
    } else {
        result = valueType.create(doc, nodeValue.getAsString());
    }

    if (scope != null)
        scopeStack.pop();

    return result;
}

From source file:org.sweble.wom3.serialization.Wom3NodeJsonTypeAdapter.java

License:Open Source License

private static Node fromJson(Wom3Document doc, JsonElement json, ScopeStack scopeStack) {
    // Get node object
    if (!json.isJsonObject())
        throw new JsonParseException("Expected JsonObject");
    JsonObject o = json.getAsJsonObject();
    Set<Entry<String, JsonElement>> entries = o.entrySet();

    // Get node type
    JsonElement type = o.get(PROPERTY_TYPE);
    if (type == null)
        throw new JsonParseException("Missing member '" + PROPERTY_TYPE + "'");
    if (!type.isJsonPrimitive())
        throw new JsonParseException("Expected member '" + PROPERTY_TYPE + "' to be a string");

    Element elem = null;/*w w w.  ja  v a2s.c o  m*/
    ValueTypes valueType = null;
    Scope scope = null;
    String defaultNsUri = null;

    String typeQName = type.getAsString();
    if (typeQName.startsWith(SPECIAL_TYPE_PREFIX)) {
        if (typeQName.equals("#text")) {
            valueType = ValueTypes.XML_TEXT;
        } else if (typeQName.equals(TYPE_ENTITY_REF)) {
            valueType = ValueTypes.ENTITY_REF;
        } else if (typeQName.equals("#cdata-section")) {
            valueType = ValueTypes.CDATA;
        } else if (typeQName.equals("#comment")) {
            valueType = ValueTypes.COMMENT;
        } else {
            throw new JsonParseException("Unknown special type '" + typeQName + "'");
        }
    } else {
        scope = registerNsDecls(entries, scopeStack, scope);

        defaultNsUri = scopeStack.getXmlns();
        if (defaultNsUri != null && defaultNsUri.isEmpty())
            defaultNsUri = null;

        elem = createElement(doc, scopeStack, defaultNsUri, typeQName);
    }

    int countType = 0;
    int countChildren = 0;
    String nodeValue = null;

    for (Entry<String, JsonElement> e : entries) {
        String entryName = e.getKey();
        JsonElement entryValue = e.getValue();
        if (entryName.startsWith(ATTRIBUTE_PREFIX)) {
            if (elem == null)
                throw new JsonParseException("Node type '" + typeQName + "' cannot have attributes");

            parseAttribute(scopeStack, elem, entryName, entryValue);
        } else if (entryName.startsWith(PROPERTY_PREFIX)) {
            if (entryName.equals(PROPERTY_TYPE)) {
                ++countType;
                if (countType > 1)
                    throw new JsonParseException("Member '" + PROPERTY_TYPE + "' occurred repeatedly");
                // Ignore, we handled that field already
            } else if (entryName.equals(PROPERTY_VALUE)) {
                if (nodeValue != null)
                    throw new JsonParseException("Member '" + PROPERTY_VALUE + "' occurred repeatedly");
                if (valueType == null)
                    throw new JsonParseException("Type '" + typeQName + "' cannot have a value");
                nodeValue = entryValue.getAsString();
            } else if (entryName.equals(PROPERTY_CHILDREN)) {
                ++countChildren;
                if (countChildren > 1)
                    throw new JsonParseException("Member '" + PROPERTY_CHILDREN + "' occurred repeatedly");
                if (valueType != null)
                    throw new JsonParseException("Type '" + typeQName + "' cannot have children");
                if (!entryValue.isJsonArray())
                    throw new JsonParseException("Expected member '" + entryName + "' to be an array.");
                JsonArray children = entryValue.getAsJsonArray();
                for (JsonElement child : children)
                    elem.appendChild(fromJson(doc, child, scopeStack));
            }
        } else
            throw new JsonParseException("Unexpected field: '" + entryName + "'");
    }

    if (scope != null)
        scopeStack.pop();

    return (valueType != null) ? valueType.create(doc, nodeValue) : elem;
}