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:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonAnnotations(AnnotationBinding[] annotations) {
    if (annotations == null)
        return JsonNull.INSTANCE;
    JsonArray array = new JsonArray();
    for (AnnotationBinding annotation : annotations) {
        array.add(toJsonAnnotation(annotation));
    }//from  ww  w  .  j a  v a 2s .c  om
    return array;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonAnnotation(AnnotationBinding annotation) {
    JsonObject object = new JsonObject();
    object.add("typeName", annotation.getAnnotationType() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(annotation.getAnnotationType().constantPoolName())));
    object.add("elementValuePairs", toJsonElementValuePairs(annotation.getElementValuePairs()));
    return object;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonElementValuePairs(ElementValuePair[] elementValuePairs) {
    if (elementValuePairs == null)
        return JsonNull.INSTANCE;
    JsonArray array = new JsonArray();
    for (ElementValuePair pair : elementValuePairs) {
        array.add(toJsonElementValuePair(pair));
    }//from w ww. java 2  s  . c o  m
    return array;
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonElementValuePair(ElementValuePair pair) {
    JsonObject object = new JsonObject();
    object.add("name",
            pair.getName() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(pair.getName())));
    object.add("value", toJsonDefaultValue(pair.getValue()));
    return object;
}

From source file:com.flipkart.android.proteus.DataContext.java

License:Apache License

@Nullable
public JsonElement get(String dataPath) {
    String aliasedDataPath = getAliasedDataPath(dataPath, reverseScope, true);
    Result result = Utils.readJson(aliasedDataPath, data, index);
    if (result.isSuccess()) {
        return result.element;
    } else if (result.RESULT_CODE == Result.RESULT_JSON_NULL_EXCEPTION) {
        return JsonNull.INSTANCE;
    }/*w w  w . j a va2s . co m*/
    return null;
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) {

    JsonElement newDataElement;//  w  w  w .jav  a 2  s . c o  m
    JsonArray oldArray;
    JsonArray newArray;
    JsonElement oldArrayItem;
    JsonElement newArrayItem;
    JsonObject oldObject;

    if (oldJson == null || oldJson.isJsonNull()) {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    if (newJson == null || newJson.isJsonNull()) {
        newJson = JsonNull.INSTANCE;
        return newJson;
    }

    if (newJson.isJsonPrimitive()) {
        JsonPrimitive value;
        if (!useCopy) {
            return newJson;
        }
        if (newJson.getAsJsonPrimitive().isBoolean()) {
            value = new JsonPrimitive(newJson.getAsBoolean());
        } else if (newJson.getAsJsonPrimitive().isNumber()) {
            value = new JsonPrimitive(newJson.getAsNumber());
        } else if (newJson.getAsJsonPrimitive().isString()) {
            value = new JsonPrimitive(newJson.getAsString());
        } else {
            value = newJson.getAsJsonPrimitive();
        }
        return value;
    }

    if (newJson.isJsonArray()) {
        if (!oldJson.isJsonArray()) {
            return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson;
        } else {
            oldArray = oldJson.getAsJsonArray();
            newArray = newJson.getAsJsonArray();

            if (oldArray.size() > newArray.size()) {
                while (oldArray.size() > newArray.size()) {
                    oldArray.remove(oldArray.size() - 1);
                }
            }

            for (int index = 0; index < newArray.size(); index++) {
                if (index < oldArray.size()) {
                    oldArrayItem = oldArray.get(index);
                    newArrayItem = newArray.get(index);
                    oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson));
                } else {
                    oldArray.add(newArray.get(index));
                }
            }
        }
    } else if (newJson.isJsonObject()) {
        if (!oldJson.isJsonObject()) {
            return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson;
        } else {
            oldObject = oldJson.getAsJsonObject();
            for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) {
                newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson);
                oldObject.add(entry.getKey(), newDataElement);
            }
        }
    } else {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    return oldJson;
}

From source file:com.flipkart.android.proteus.view.manager.ProteusViewManagerImpl.java

License:Apache License

private void handleBinding(Binding binding) {
    if (binding.hasRegEx()) {
        layoutBuilder.handleAttribute(layoutHandler, (ProteusView) view, binding.getAttributeKey(),
                new JsonPrimitive(binding.getAttributeValue()));
    } else {//w  ww.ja  v  a2s . co m
        Result result = Utils.readJson(binding.getBindingName(), dataContext.getData(), dataContext.getIndex());
        JsonElement dataValue = result.isSuccess() ? result.element : JsonNull.INSTANCE;
        layoutBuilder.handleAttribute(layoutHandler, (ProteusView) view, binding.getAttributeKey(), dataValue);
    }
}

From source file:com.gilecode.yagson.YaGson.java

License:Apache License

/**
 * This method serializes the specified object into its equivalent Json representation, given that the
 * de-serialization type is known, and writes it to the provided {@link JsonWriter}.
 * <p/>//from ww  w.j a v a 2 s .  co  m
 * The root type information is emitted only if necessary, i.e. if the specified de-serialization type differs from
 * the actual object's class. It is guaranteed that the resulting JSON representation may be de-serialized to the
 * similar object using {@code fromJson(json, deserializationType)}, unless some YaGson's features are disabled.
 *
 * @param src       the object for which JSON representation is to be created
 * @param deserializationType The type which will be used for de-serialization of the resulting JSON representation
 * @param writer    Writer to which the Json representation of src needs to be written.
 * @throws JsonIOException if there was a problem writing to the writer
 * @since 1.2
 */
@Override
public void toJson(Object src, Type deserializationType, JsonWriter writer) throws JsonIOException {
    if (src == null) {
        toJson(JsonNull.INSTANCE, writer);
        return;
    }

    if (src instanceof Number && TypeUtils.isNumberType(deserializationType)
            && TypeUtils.typesDiffer(deserializationType, src.getClass())) {
        // a special case, where a user may rely on auto-cast of primitive number types
        // perform such cast here
        src = TypeUtils.convertNumber((Number) src, deserializationType);
    }

    boolean isRootTypeRequired = TypeUtils.isTypeInfoRequired(src.getClass(), deserializationType, false);

    // use the exact source class for serialization, but try to keep the type parameters if available in
    // the specified deserialization type
    Type parameterizedSrcType = TypeUtils.mergeTypes(src.getClass(), deserializationType);
    super.toJson(src, parameterizedSrcType, writer, isRootTypeRequired);
}

From source file:com.gilecode.yagson.YaGson.java

License:Apache License

/**
 * This method serializes the specified object into its equivalent Json representation as a tree of
 * {@link JsonElement}s, given that the de-serialization type is known.
 * <p/>/* ww  w.j  a v a 2s  . com*/
 * The root type information is emitted only if necessary, i.e. if the specified de-serialization type differs from
 * the actual object's class. It is guaranteed that the resulting string may be de-serialized to the similar object
 * using {@code fromJson(json, deserializationType)}, unless some YaGson's features are disabled.
 *
 * @param src       the object for which JSON representation is to be created
 * @param deserializationType The type which will be used for de-serialization of the resulting JSON representation
 * @return Json representation of {@code src}
 */
@Override
public JsonElement toJsonTree(Object src, Type deserializationType) {
    if (src == null) {
        return JsonNull.INSTANCE;
    }

    boolean isRootTypeRequired = TypeUtils.isTypeInfoRequired(src.getClass(), deserializationType, false);
    JsonTreeWriter writer = new JsonTreeWriter();
    super.toJson(src, src.getClass(), writer, isRootTypeRequired);
    return writer.get();
}

From source file:com.github.jneat.mybatis.JsonElementTypeHandler.java

License:Open Source License

@Override
public JsonElement getNullableResult(ResultSet rs, String columnName) throws SQLException {
    String jsonSource = rs.getString(columnName);
    if (jsonSource != null) {
        return fromString(jsonSource);
    }// w ww .  j  a v  a  2s. c  o  m
    return JsonNull.INSTANCE;
}