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.talvish.tales.serialization.json.translators.MapToJsonArrayTranslator.java

License:Apache License

/**
 * Translates the received object into a json array with a key/value objects.
 * If the object is of the wrong type or translator doesn't return JsonElements, a TranslationException will occur.
 *///from   ww w  .  ja  va 2s .c  om
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null) {
        returnValue = JsonNull.INSTANCE;
    } else {
        try {
            Map<?, ?> map = (Map<?, ?>) anObject;
            JsonArray jsonArray = new JsonArray();
            JsonObject jsonEntry;
            TypeFormatAdapter typeAdapter;

            for (Entry<?, ?> entry : map.entrySet()) {
                jsonEntry = new JsonObject();

                // translate the key side
                if (keyTranslator == null) { // meaning we have more than one so didn't pull out the only translator
                    // we need to find the translator to use
                    // then save out the key type
                    // and the key value
                    typeAdapter = keyTypeAdapters.get(entry.getKey().getClass());
                    if (typeAdapter == null) {
                        throw new TranslationException(String.format(
                                "An object of type '%s' was attempting to be converted to a json object as a key in a map, but this object isn't supported",
                                entry.getKey().getClass().getName()));
                    } else {
                        jsonEntry.addProperty("key_type", typeAdapter.getName());
                        jsonEntry.add("key",
                                (JsonElement) typeAdapter.getToFormatTranslator().translate(entry.getKey()));
                    }
                } else {
                    jsonEntry.add("key", (JsonElement) keyTranslator.translate(entry.getKey()));
                }

                // translate the value side
                if (valueTranslator == null) { // meaning we have more than one so didn't pull out the only translator
                    // we need to find the translator to use
                    // then save out the key type
                    // and the key value
                    typeAdapter = valueTypeAdapters.get(entry.getValue().getClass());
                    if (typeAdapter == null) {
                        throw new TranslationException(String.format(
                                "An object of type '%s' was attempting to be converted to a json object as a value in a map, but this object isn't supported",
                                entry.getValue().getClass().getName()));
                    } else {
                        jsonEntry.addProperty("value_type", typeAdapter.getName());
                        jsonEntry.add("value",
                                (JsonElement) typeAdapter.getToFormatTranslator().translate(entry.getValue()));
                    }
                } else {
                    jsonEntry.add("value", (JsonElement) valueTranslator.translate(entry.getValue()));
                }
                jsonArray.add(jsonEntry);
            }
            returnValue = jsonArray;

        } catch (ClassCastException e) {
            throw new TranslationException(e);
        }
    }
    return returnValue;
}

From source file:com.talvish.tales.serialization.json.translators.NumberToJsonPrimitiveTranslator.java

License:Apache License

/**
 * Translates the received object into a json primitive.
 * If the object is of the wrong type, a TranslationException will occur.
 *//*from   w  ww  . j ava 2s.  com*/
@Override
public Object translate(Object anObject) {
    try {
        if (anObject == null) {
            return JsonNull.INSTANCE;
        } else {
            return new JsonPrimitive((Number) anObject);
        }
    } catch (ClassCastException e) {
        throw new TranslationException(e);
    }
}

From source file:com.talvish.tales.serialization.json.translators.ObjectToJsonObjectTranslator.java

License:Apache License

/**
 * Translates the received object into a json primitive.
 * If the object is of the wrong type, a TranslationException will occur.
 *//* w w  w. ja  v a 2s  . c  o m*/
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null) {
        returnValue = JsonNull.INSTANCE;
    } else {
        returnValue = typeMap.getData(anObject);
    }
    return returnValue;
}

From source file:com.talvish.tales.serialization.json.translators.ObjectToJsonPrimitiveTranslator.java

License:Apache License

/**
 * Translates the received object into a json primitive.
 * If the object is of the wrong type, a TranslationException will occur.
 *//*from  ww  w.j  av a  2  s . co  m*/
@Override
public Object translate(Object anObject) {
    if (anObject == null) {
        return JsonNull.INSTANCE;
    } else {
        return new JsonPrimitive(anObject.toString());
    }
}

From source file:com.talvish.tales.serialization.json.translators.PolymorphicObjectToJsonObjectTranslator.java

License:Apache License

/**
 * Translates the received object into the appropriate json representation.
 * If the object is of the wrong type or the translator used doesn't produce
 * JsonElements's then a TranslationException will occur.
 *///from   ww  w. j  a  v  a 2s.co  m
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null) {
        returnValue = JsonNull.INSTANCE;
    } else {
        TypeFormatAdapter typeAdapter = typeAdapters.get(anObject.getClass());
        if (typeAdapter == null) {
            throw new TranslationException(String.format(
                    "An object of type '%s' was attempting to be converted to a json object, but this object isn't supported",
                    anObject.getClass().getName()));
        } else {
            try {
                JsonObject jsonEntry = new JsonObject();

                jsonEntry.addProperty("value_type", typeAdapter.getName());
                jsonEntry.add("value", (JsonElement) typeAdapter.getToFormatTranslator().translate(anObject));

                returnValue = jsonEntry;

            } catch (ClassCastException e) {
                throw new TranslationException(e);
            }
        }
    }
    return returnValue;
}

From source file:com.talvish.tales.serialization.json.translators.StringToJsonElementToChainTranslator.java

License:Apache License

@Override
public Object translate(Object anObject) {
    Object returnValue;//w ww  .  j  a v  a  2 s  . co m
    if (anObject == null) {
        returnValue = chainedTranslator.translate(JsonNull.INSTANCE);
    } else {
        try {
            // NOTE: there is a bug in GSON that if the string is missing an ending curly brace then it doesn't report a json parsing exception
            returnValue = chainedTranslator.translate(parser.parse((String) anObject));
        } catch (JsonParseException e) {
            throw new TranslationException(e);
        } catch (ClassCastException e) {
            throw new TranslationException(e);
        }
    }
    return returnValue;
}

From source file:com.talvish.tales.serialization.json.translators.StringToJsonPrimitiveTranslator.java

License:Apache License

/**
 * Translates the received object into a json primitive.
 * If the object is of the wrong type, a TranslationException will occur.
 *///w  w  w. j a  v a 2s. c  o m
@Override
public Object translate(Object anObject) {
    try {
        if (anObject == null) {
            return JsonNull.INSTANCE;
        } else {
            return new JsonPrimitive((String) anObject);
        }
    } catch (ClassCastException e) {
        throw new TranslationException(e);
    }
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.DataSeriesItemTypeAdapterFactory.java

private TypeAdapter<DataSeriesItem> customizeMyClassAdapter(Gson gson, TypeToken<DataSeriesItem> type) {
    final TypeAdapter<DataSeriesItem> delegate = gson.getDelegateAdapter(this, type);
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
    return new TypeAdapter<DataSeriesItem>() {
        @Override/*from  w  w w.j a  va  2  s. c  o m*/
        public void write(JsonWriter out, DataSeriesItem value) throws IOException {
            if (value.isCustomized()) {
                elementAdapter.write(out, delegate.toJsonTree(value));
            } else {
                Number x = value.getX();
                Number y = value.getY();
                if (x != null) {
                    JsonArray jsonArray = new JsonArray();
                    jsonArray.add(new JsonPrimitive(x));
                    if (y != null) {
                        jsonArray.add(new JsonPrimitive(y));
                    } else if (value.getLow() != null) {
                        jsonArray.add(new JsonPrimitive(value.getLow()));
                        jsonArray.add(new JsonPrimitive(value.getHigh()));
                    } else {
                        jsonArray.add(JsonNull.INSTANCE);
                        jsonArray.add(JsonNull.INSTANCE);
                    }
                    elementAdapter.write(out, jsonArray);
                } else {
                    // If no x set, make it like list series, just number or
                    // min-max pairs
                    if (y != null) {
                        elementAdapter.write(out, new JsonPrimitive(y));
                    } else {
                        JsonArray jsonArray = new JsonArray();
                        jsonArray.add(new JsonPrimitive(value.getLow()));
                        jsonArray.add(new JsonPrimitive(value.getHigh()));
                        elementAdapter.write(out, jsonArray);
                    }
                }
            }
        }

        // This is never used
        @Override
        public DataSeriesItem read(JsonReader in) throws IOException {
            JsonElement tree = elementAdapter.read(in);
            return delegate.fromJsonTree(tree);
        }
    };
}

From source file:com.vmware.admiral.closures.drivers.nashorn.EmbeddedNashornJSDriver.java

License:Open Source License

@SuppressWarnings({ "restriction", "unchecked" })
private JsonElement convertToJsonElement(ScriptEngine engine, Object val) {

    if (val == null) {
        return JsonNull.INSTANCE;
    } else if (val instanceof String) {
        return new JsonPrimitive((String) val);
    } else if (val instanceof Number) {
        return new JsonPrimitive((Number) val);
    } else if (val instanceof Boolean) {
        return new JsonPrimitive((Boolean) val);
    } else if (val instanceof Object[]) {
        JsonArray jsArray = new JsonArray();
        Object[] nums = (Object[]) val;
        for (Object n : nums) {
            jsArray.add(convertToJsonElement(engine, n));
        }/*from ww w. j a va  2s. c  om*/
        return jsArray;
    } else if (val instanceof List) {
        JsonArray jsArray = new JsonArray();
        List<Object> objs = (List<Object>) val;
        for (Object o : objs) {
            jsArray.add(convertToJsonElement(engine, o));
        }
        return jsArray;
    } else if (val instanceof ScriptObjectMirror) {
        ScriptObjectMirror m = (ScriptObjectMirror) val;
        if (m.isArray()) {
            List<Object> list = m.values().stream().collect(Collectors.toList());
            return convertToJsonElement(engine, list);
        } else {
            JsDateWrap jsDate = ((Invocable) engine).getInterface(val, JsDateWrap.class);
            if (jsDate != null) {
                long timestampLocalTime = jsDate.getTime();
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(timestampLocalTime);
                val = calendar.getTime();
            }
            String stringified = Utils.toJson(val);
            return Utils.fromJson(stringified, JsonElement.class);
        }
    }

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

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

License:Open Source License

private static JsonElement getPrimitiveJsonElement(JsonNode node) {
    if (node.isNull()) {
        return JsonNull.INSTANCE;
    } else if (node.isNumber()) {
        return new JsonPrimitive(node.numberValue());
    }/*from   ww  w  . java2s. c  o m*/

    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    return parser.parse(node.asText());
}