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.ChainToJsonElementToStringTranslator.java

License:Apache License

/**
 * Translates the received object into a json string.
 * If the object is of the wrong type, a TranslationException will occur.
 */// w  w  w  .  ja  v a  2 s.c  o  m
@Override
public Object translate(Object anObject) {
    try {
        anObject = chainedTranslator.translate(anObject);
        if (anObject == null) {
            anObject = JsonNull.INSTANCE;
        }

        return gson.toJson((JsonElement) anObject);
    } catch (ClassCastException e) {
        throw new TranslationException(e);
    }
}

From source file:com.talvish.tales.serialization.json.translators.ChainToStringToJsonPrimitiveTranslator.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 w  w  .  ja  v a 2s.  com*/
@Override
public Object translate(Object anObject) {
    try {
        anObject = chainedTranslator.translate(anObject);
        if (anObject == null) {
            return JsonNull.INSTANCE;
        } else {
            return new JsonPrimitive((String) anObject);
        }
    } catch (ClassCastException e) {
        throw new TranslationException(e);
    }
}

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

License:Apache License

/**
 * Translates the received object into a json array.
 * If the object is of the wrong type, a TranslationException will occur.
 *///from   ww w.j a v a 2 s .c  om
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null) {
        returnValue = JsonNull.INSTANCE;
    } else {
        try {
            Collection<?> array = (Collection<?>) anObject;
            JsonArray jsonArray = new JsonArray();

            for (Object object : array) {
                jsonArray.add((JsonElement) elementTranslator.translate(object));
            }
            returnValue = jsonArray;

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

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

License:Apache License

/**
 * Translates the received object into an array;
 * If the object is of the wrong type, a TranslationException will occur.
 *//*from www.  ja v a2s .com*/
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        returnValue = null;
    } else {
        try {
            // things to look at later
            JsonElement jsonElement = (JsonElement) anObject;
            if (jsonElement.isJsonArray()) {
                JsonArray jsonArray = (JsonArray) anObject;
                Object array = Array.newInstance(elementType, jsonArray.size());

                for (int count = 0; count < jsonArray.size(); count += 1) {
                    Array.set(array, count, elementTranslator.translate(jsonArray.get(count)));
                }

                returnValue = array;
            } else if (readSingles) {
                Object array = Array.newInstance(elementType, 1);
                Array.set(array, 0, elementTranslator.translate(jsonElement));
                returnValue = array;
            } else {
                throw new TranslationException(
                        String.format("Attempt to translate an array but a single object was sent instead."));
            }
        } catch (IllegalArgumentException e) {
            throw new TranslationException(e);
        } catch (ClassCastException e) {
            throw new TranslationException(e);
        }
    }
    return returnValue;
}

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

License:Apache License

/**
 * Translates the received object into a list.
 * If the object is of the wrong type, a TranslationException will occur.
 *//*from w ww .  j  a  v a  2s. c om*/
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        returnValue = null;
    } else {
        try {
            JsonArray jsonArray = (JsonArray) anObject;
            @SuppressWarnings("unchecked")
            Collection<Object> collection = (Collection<Object>) constructor.newInstance();

            for (JsonElement element : jsonArray) {
                collection.add(elementTranslator.translate(element));
            }

            returnValue = collection;

        } catch (ClassCastException e) {
            throw new TranslationException(e);
        } catch (NullPointerException e) {
            throw new TranslationException(String.format("Unable to use null in the collection of type '%s'",
                    collectionType.getName()), e);
        } catch (InstantiationException e) {
            throw new TranslationException(
                    String.format("Unable to create a collection of type '%s'", collectionType.getName()), e);
        } catch (IllegalArgumentException e) {
            throw new TranslationException(
                    String.format("Unable to create a collection of type '%s'", collectionType.getName()), e);
        } catch (InvocationTargetException e) {
            throw new TranslationException(
                    String.format("Unable to create a collection of type '%s'", collectionType.getName()), e);
        } catch (IllegalAccessException e) {
            throw new TranslationException(
                    String.format("Unable to create a collection of type '%s'", collectionType.getName()), e);
        }
    }
    return returnValue;
}

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

License:Apache License

/**
 * Translates the received object into a map;
 * If the object is of the wrong type, a TranslationException will occur.
 *///  w w w.  j ava2  s.co  m
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        returnValue = null;
    } else {
        try {
            // things to look at later
            JsonArray jsonArray = (JsonArray) anObject;
            @SuppressWarnings("unchecked")
            Map<Object, Object> map = (Map<Object, Object>) constructor.newInstance();
            JsonObject entry;
            JsonElement key;
            JsonElement keyType;
            JsonElement value;
            JsonElement valueType;
            TypeFormatAdapter typeAdapter;

            Translator selectedKeyTranslator;
            Translator selectedValueTranslator;

            for (JsonElement element : jsonArray) {
                entry = (JsonObject) element;
                key = entry.get("key");
                keyType = entry.get("key_type");
                value = entry.get("value");
                valueType = entry.get("value_type");

                selectedKeyTranslator = keyTranslator; // set a default, though it could be null
                selectedValueTranslator = valueTranslator; // set a default, though it could be null

                if (key == null) {
                    throw new TranslationException("Could not find the key to create a proper map.");
                } else if (value == null) {
                    throw new TranslationException("Could not find the value to create a proper map.");
                } else {
                    if (keyType != null) {
                        String keyTypeString = keyType.getAsString();
                        typeAdapter = keyTypeAdapters.get(keyTypeString);

                        if (typeAdapter == null) {
                            throw new TranslationException(
                                    String.format("Json is referring to a key type '%s' that isn't supported.",
                                            keyTypeString));
                        } else {
                            selectedKeyTranslator = typeAdapter.getFromFormatTranslator();
                        }
                    }
                    if (valueType != null) {
                        String valueTypeString = valueType.getAsString();
                        typeAdapter = valueTypeAdapters.get(valueTypeString);

                        if (typeAdapter == null) {
                            throw new TranslationException(String.format(
                                    "Json is referring to a value type '%s' that isn't supported.",
                                    valueTypeString));
                        } else {
                            selectedValueTranslator = typeAdapter.getFromFormatTranslator();
                        }
                    }
                    if (selectedKeyTranslator == null) {
                        throw new TranslationException("An appropriate key type was not provided.");
                    }
                    if (selectedValueTranslator == null) {
                        throw new TranslationException("An appropriate value type was not provided.");
                    }
                    map.put(selectedKeyTranslator.translate(key), selectedValueTranslator.translate(value));
                }
            }

            returnValue = map;

        } catch (ClassCastException e) {
            throw new TranslationException(e);
        } catch (NullPointerException e) {
            throw new TranslationException(
                    String.format("Unable to use null in the map of type '%s'", mapType.getName()), e);
        } catch (InstantiationException e) {
            throw new TranslationException(
                    String.format("Unable to create a map of type '%s'", mapType.getName()), e);
        } catch (IllegalArgumentException e) {
            throw new TranslationException(
                    String.format("Unable to create a map of type '%s'", mapType.getName()), e);
        } catch (InvocationTargetException e) {
            throw new TranslationException(
                    String.format("Unable to create a map of type '%s'", mapType.getName()), e);
        } catch (IllegalAccessException e) {
            throw new TranslationException(
                    String.format("Unable to create a map of type '%s'", mapType.getName()), e);
        }
    }
    return returnValue;
}

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

License:Apache License

/**
 * Translates the object as a string first and then into the chained translator.
 *//*ww w.j  av  a  2s  .  c om*/
@Override
public Object translate(Object anObject) {
    Object returnValue;
    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        returnValue = chainedTranslator.translate(null);
    } else {
        try {
            returnValue = chainedTranslator.translate(((JsonElement) anObject).getAsString());
        } catch (ClassCastException | IllegalStateException | UnsupportedOperationException e) {
            throw new TranslationException(e);
        }
    }
    return returnValue;
}

From source file:com.talvish.tales.serialization.json.translators.JsonObjectToObjectTranslator.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 w  w  . j  a  va  2 s.  c om
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        returnValue = null;
    } else {
        try {
            JsonObject jsonObject = (JsonObject) anObject;

            returnValue = typeMap.getReflectedType().newInstance();
            typeMap.setData(returnValue, jsonObject);

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

From source file:com.talvish.tales.serialization.json.translators.JsonObjectToPolymorphicObjectTranslator.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.
 *///ww w  .  ja v  a  2  s  .  c o  m
@Override
public Object translate(Object anObject) {
    Object returnValue;

    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        returnValue = null;
    } else {
        try {
            // need to extract two things, the "value" and the "value_type"
            // and if they don't exist then we have a problem
            JsonObject jsonObject = (JsonObject) anObject;
            JsonPrimitive valueTypeJson = jsonObject.getAsJsonPrimitive("value_type");
            JsonElement valueJson = jsonObject.get("value");

            if (valueTypeJson == null || !valueTypeJson.isString()) {
                throw new TranslationException(String.format("The associate value type is missing."));
            } else if (valueJson == null) {
                throw new TranslationException(String.format("The associate value for type '%s' is missing.",
                        valueTypeJson.getAsString()));
            } else {
                String valueTypeString = valueTypeJson.getAsString();
                TypeFormatAdapter typeAdapter = typeAdapters.get(valueTypeString);

                if (typeAdapter == null) {
                    throw new TranslationException(String
                            .format("Json is referring to a type '%s' that isn't supported.", valueTypeString));
                } else {
                    returnValue = typeAdapter.getFromFormatTranslator().translate(valueJson);
                }
            }

            // TODO: catch the various gson json exceptions
        } catch (ClassCastException e) {
            throw new TranslationException(e);
        }
    }
    return returnValue;
}

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

License:Apache License

/**
 * Called when a translation is to occur for void, but generally
 * it will only ever receive null, JsonNull or empty JsonObject's
 * and that is fine, anything else is a problem (since this is for
 * void)//w ww .j a  v a  2 s  .co m
 */
@Override
public Object translate(Object anObject) {
    if (anObject == null || anObject.equals(JsonNull.INSTANCE)) {
        return null;
    } else {
        try {
            JsonObject jsonObject = (JsonObject) anObject;

            if (jsonObject.entrySet().size() != 0) {
                throw new TranslationException("should not be receiving void data");
            } else {
                return null; // no data here, so let's return null
            }

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