Example usage for com.google.gson JsonNull INSTANCE

List of usage examples for com.google.gson JsonNull INSTANCE

Introduction

In this page you can find the example usage for com.google.gson JsonNull INSTANCE.

Prototype

JsonNull INSTANCE

To view the source code for com.google.gson JsonNull INSTANCE.

Click Source Link

Document

singleton for JsonNull

Usage

From source file:org.sprintapi.hyperdata.gson.GsonHyperDataSerializer.java

License:Apache License

@Override
public JsonElement serialize(HyperMap src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null) {
        return JsonNull.INSTANCE;
    }//  w w  w. j a va 2s .c  om

    JsonObject obj = new JsonObject();

    final Map<String, Object> metadata = src.getMetadata();
    if (metadata != null) {
        for (String name : metadata.keySet()) {
            Object value = metadata.get(name);
            obj.add(Constants.META_CHAR.concat(name), context.serialize(value));
        }
    }
    for (String name : src.names()) {
        Object value = src.get(name);
        obj.add(name, context.serialize(value));
    }
    return obj;
}

From source file:org.structr.core.graph.GraphObjectModificationState.java

License:Open Source License

private JsonElement toElement(final Object value) {

    if (value != null) {

        if (value instanceof String) {

            return new JsonPrimitive((String) value);

        } else if (value instanceof Number) {

            return new JsonPrimitive((Number) value);

        } else if (value instanceof Boolean) {

            return new JsonPrimitive((Boolean) value);

        } else if (value.getClass().isArray()) {

            final JsonArray arr = new JsonArray();
            final Object[] values = (Object[]) value;

            for (final Object v : values) {
                arr.add(toElement(v));//from  w ww  . j  a v a  2 s.  c  o m
            }

            return arr;

        } else {

            return new JsonPrimitive(value.toString());
        }
    }

    return JsonNull.INSTANCE;
}

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.imported.RuntimeTypeAdapterFactory.java

License:Apache License

/**
 * Takes a reader in any state and returns the next value as a JsonElement.
 *//*from w ww  .j ava 2  s .  c om*/
private static JsonElement parse(JsonReader reader) throws JsonParseException {
    boolean isEmpty = true;
    try {
        reader.peek();
        isEmpty = false;
        return RuntimeTypeAdapterFactory.JSON_ELEMENT.read(reader);
    } catch (EOFException e) {
        /*
         * For compatibility with JSON 1.5 and earlier, we return a JsonNull for
         * empty documents instead of throwing.
         */
        if (isEmpty) {
            return JsonNull.INSTANCE;
        }
        // The stream ended prematurely so it is likely a syntax error.
        throw new JsonSyntaxException(e);
    } catch (MalformedJsonException e) {
        throw new JsonSyntaxException(e);
    } catch (IOException e) {
        throw new JsonIOException(e);
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}

From source file:org.terasology.persistence.typeHandling.gson.GsonSerializationContext.java

License:Apache License

@Override
public PersistedData create(Iterable<PersistedData> data) {
    JsonArray result = new JsonArray();
    for (PersistedData val : data) {
        if (val != null) {
            result.add(((GsonPersistedData) val).getElement());
        } else {//from w  w w. j  ava  2 s  . c  o m
            result.add(JsonNull.INSTANCE);
        }
    }
    return new GsonPersistedData(result);
}

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

License:Apache License

/**
 * @param tree A tree hierarchy based on a {@link JsonElement}, created by the serialize() method.
 * @return The initial {@link JsonElement} reconstructed from the tree.
 *///from   www.  j a  v  a2s. c  om
public static JsonElement deserialize(Tree<JsonTreeNode> tree) {
    JsonTreeNode node = tree.getValue();
    if (node.getType() == JsonTreeNode.ElementType.KEY_VALUE_PAIR
            || node.getType() == JsonTreeNode.ElementType.VALUE) {
        Object value = node.getValue();
        if (value instanceof Boolean) {
            return new JsonPrimitive((Boolean) value);
        } else if (value instanceof Number) {
            return new JsonPrimitive((Number) value);
        } else if (value instanceof String) {
            return new JsonPrimitive((String) value);
        } else {
            return JsonNull.INSTANCE;
        }
    } else if (node.getType() == JsonTreeNode.ElementType.ARRAY) {
        JsonArray array = new JsonArray();
        for (Tree<JsonTreeNode> child : tree.getChildren()) {
            array.add(deserialize(child));
        }
        return array;
    } else if (node.getType() == JsonTreeNode.ElementType.OBJECT) {
        JsonObject object = new JsonObject();
        for (Tree<JsonTreeNode> child : tree.getChildren()) {
            object.add(child.getValue().getKey(), deserialize(child));
        }
        return object;
    } else {
        return JsonNull.INSTANCE;
    }
}

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

License:Apache License

/**
 * @param tree A tree hierarchy based on a {@link JsonElement} as generated by the serialize() method.
 * @return The initial {@link JsonElement} reconstructed from the tree.
 *//*  w  w  w  .ja  v  a  2  s  .  c o  m*/
public static JsonElement deserialize(Tree<JsonTreeValue> tree) {
    JsonTreeValue value = tree.getValue();
    if (value.getType() == JsonTreeValue.Type.KEY_VALUE_PAIR || value.getType() == JsonTreeValue.Type.VALUE) {
        Object primitive = value.getValue();
        if (primitive instanceof Boolean) {
            return new JsonPrimitive((Boolean) primitive);
        } else if (primitive instanceof Number) {
            return new JsonPrimitive((Number) primitive);
        } else if (primitive instanceof String) {
            return new JsonPrimitive((String) primitive);
        } else {
            return JsonNull.INSTANCE;
        }
    } else if (value.getType() == JsonTreeValue.Type.ARRAY) {
        JsonArray array = new JsonArray();
        for (Tree<JsonTreeValue> child : tree.getChildren()) {
            array.add(deserialize(child));
        }
        return array;
    } else if (value.getType() == JsonTreeValue.Type.OBJECT) {
        JsonObject object = new JsonObject();
        for (Tree<JsonTreeValue> child : tree.getChildren()) {
            object.add(child.getValue().getKey(), deserialize(child));
        }
        return object;
    } else {
        return JsonNull.INSTANCE;
    }
}

From source file:org.tuleap.mylyn.task.ui.internal.wizards.query.QueryFieldVisitor.java

License:Open Source License

/**
 * Constructor.// www .  j a  v a2 s.  c o m
 *
 * @param group
 *            The parent composite.
 * @param gson
 *            The Gson to use to deserialize query parameters.
 * @param page
 *            The wizard page.
 */
public QueryFieldVisitor(Composite group, Gson gson, final TuleapCustomQueryPage page) {
    this.group = group;
    this.gson = gson;
    this.page = page;
    IRepositoryQuery currentQuery = page.getQuery();
    String jsonCriteria = null;
    if (currentQuery != null) {
        jsonCriteria = currentQuery.getAttribute(ITuleapQueryConstants.QUERY_CUSTOM_CRITERIA);
    }
    JsonElement jsonElement;
    if (jsonCriteria != null) {
        jsonElement = new JsonParser().parse(jsonCriteria);
    } else {
        jsonElement = JsonNull.INSTANCE;
    }
    if (jsonElement.isJsonObject()) {
        currentQueryCriteria = jsonElement.getAsJsonObject();
    } else {
        currentQueryCriteria = new JsonObject();
    }
    elements = new ArrayList<AbstractTuleapCustomQueryElement<?>>();
}

From source file:org.wso2.carbon.mdm.services.android.util.AndroidDeviceUtils.java

License:Open Source License

private static String getProperty(String properties, String needed) {
    // This is not a key value pair. value is the immediate element to its filed name.
    // Ex:/*from  ww w  .  j a  v  a2 s  .com*/
    // [{"name":"ENCRYPTION_ENABLED","value":"false"},{"name":"PASSCODE_ENABLED","value":"true"},
    // {"name":"BATTERY_LEVEL","value":"100"},{"name":"INTERNAL_TOTAL_MEMORY","value":"0.76"}]
    JsonElement jsonElement = new JsonParser().parse(properties);
    JsonArray jsonArray = jsonElement.getAsJsonArray();
    for (JsonElement element : jsonArray) {
        if (element.isJsonObject()) {
            JsonObject jsonObject = element.getAsJsonObject();
            if (jsonObject.has("name") && jsonObject.get("name").getAsString().equalsIgnoreCase(needed)) {
                if (jsonObject.has("value") && jsonObject.get("value") != JsonNull.INSTANCE) {
                    return jsonObject.get("value").getAsString().replace("%", "");
                } else {
                    return "";
                }
            }
        }
    }
    return "";
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.ScriptObjectMirrorSerializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w w  w .j a  v a  2s.c o  m
 */
@Override
public JsonElement serialize(ScriptObjectMirror jsObj, Type type, JsonSerializationContext context) {
    if ((jsObj == null) || ScriptObjectMirror.isUndefined(jsObj) || jsObj.isFunction()) {
        return JsonNull.INSTANCE;
    }

    if (jsObj.isArray()) {
        JsonArray jsonArray = new JsonArray();
        for (Object item : jsObj.values()) {
            jsonArray.add(serializeFurther(context.serialize(item), context));
        }
        return jsonArray;
    }
    if (jsObj.isEmpty()) {
        return new JsonObject();
    }

    JsonObject jsonObject = new JsonObject();
    for (String key : jsObj.getOwnKeys(false)) {
        jsonObject.add(key, serializeFurther(jsObj.getMember(key), context));
    }
    return jsonObject;
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.ScriptObjectMirrorSerializer.java

License:Open Source License

private JsonElement serializeFurther(Object src, JsonSerializationContext context) {
    return ScriptObjectMirror.isUndefined(src) ? JsonNull.INSTANCE : context.serialize(src);
}