@SuppressWarnings("unchecked") public <T> T deserializeDefault(JsonElement json, Type typeOfT) throws JsonParseException { if (json == null || json.isJsonNull()) { return null; } else if (json.isJsonArray()) { Object array = fromJsonArray(typeOfT, json.getAsJsonArray(), this, true); return (T) array; } else if (json.isJsonObject()) { Object object = fromJsonObject(typeOfT, json.getAsJsonObject(), this, true); return (T) object; } else if (json.isJsonPrimitive()) { Object primitive = fromJsonPrimitive(typeOfT, json.getAsJsonPrimitive(), this, true); return (T) primitive; } else { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json"); } } }
/** * Invokes default deserialization on the specified object. It should never be invoked on * the element received as a parameter of the * {@link JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)} method. Doing * so will result in an infinite loop since Gson will in-turn call the custom deserializer again. * @param json the parse tree. * @param typeOfT type of the expected return value. * @param <T> The type of the deserialized object. * @return An object of type typeOfT. * @throws JsonParseException if the parse tree does not contain expected data. */ @SuppressWarnings("unchecked") public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { if (json == null || json.isJsonNull()) { return null; } else if (json.isJsonArray()) { Object array = fromJsonArray(typeOfT, json.getAsJsonArray(), this, false); return (T) array; } else if (json.isJsonObject()) { Object object = fromJsonObject(typeOfT, json.getAsJsonObject(), this, false); return (T) object; } else if (json.isJsonPrimitive()) { Object primitive = fromJsonPrimitive(typeOfT, json.getAsJsonPrimitive(), this, false); return (T) primitive; } else { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json"); } }