Skip to content

Commit

Permalink
[java] optimize JsonToBeanConverter
Browse files Browse the repository at this point in the history
In case the input is a JsonNull, return null asap.

Signed-off-by: Alexei Barantsev <barancev@gmail.com>
  • Loading branch information
alb-i986 authored and barancev committed Feb 3, 2016
1 parent 64b76a4 commit 2021f28
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -47,7 +48,7 @@ public <T> T convert(Class<T> clazz, Object source) throws JsonException {

@SuppressWarnings("unchecked")
private <T> T convert(Class<T> clazz, Object source, int depth) {
if (source == null) {
if (source == null || source instanceof JsonNull) {
return null;
}

Expand Down Expand Up @@ -197,6 +198,10 @@ private <T> T convert(Class<T> clazz, Object source, int depth) {
if (source instanceof JsonElement) {
JsonElement element = (JsonElement) source;

if (element.isJsonNull()) {
return null;
}

if (element.isJsonPrimitive()) {
return (T) convertJsonPrimitive(element.getAsJsonPrimitive());
}
Expand All @@ -205,10 +210,6 @@ private <T> T convert(Class<T> clazz, Object source, int depth) {
return (T) convertList(element.getAsJsonArray(), depth);
}

if (element.isJsonNull()) {
return null;
}

if (element.isJsonObject()) {
if (Map.class.isAssignableFrom(clazz)) {
return (T) convertMap(element.getAsJsonObject(), depth);
Expand Down

0 comments on commit 2021f28

Please sign in to comment.