Example usage for com.google.gson.stream JsonReader nextNull

List of usage examples for com.google.gson.stream JsonReader nextNull

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextNull.

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

Usage

From source file:DateMidnightTypeAdapter.java

License:Apache License

@Override
public DateMidnight read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }//from w ww  . j  a v a 2 s . c om
    return new DateMidnight(in.nextString());
}

From source file:MonthDayTypeAdapter.java

License:Apache License

@Override
public MonthDay read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*  www . ja  v  a 2s  . c  o m*/
    return MonthDay.parse(in.nextString());
}

From source file:LocalDateTimeTypeAdapter.java

License:Apache License

@Override
public LocalDateTime read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from ww  w .j a va2  s  .  c o m*/
    return new LocalDateTime(in.nextString());
}

From source file:PeriodTypeAdapter.java

License:Apache License

@Override
public Period read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }//  www .j  a  v  a 2s  .  com
    return new Period(in.nextString());
}

From source file:InstantTypeAdapter.java

License:Apache License

@Override
public Instant read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }//ww w  .  j  a va  2s. c o m
    return new Instant(in.nextString());
}

From source file:LocalDateTypeAdapter.java

License:Apache License

@Override
public LocalDate read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from w  w w .j  a v  a2 s  .  c  om*/
    return new LocalDate(in.nextString());
}

From source file:LocalTimeTypeAdapter.java

License:Apache License

@Override
public LocalTime read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*ww w.  ja  v a2  s .  c  o  m*/
    return new LocalTime(in.nextString());
}

From source file:YearMonthTypeAdapter.java

License:Apache License

@Override
public YearMonth read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }/*from   w  w w . ja  v  a2 s .  c o m*/
    return new YearMonth(in.nextString());
}

From source file:BundleTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from   w w w .  j av  a  2  s  .co  m
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
    if (!Bundle.class.isAssignableFrom(type.getRawType())) {
        return null;
    }
    return (TypeAdapter<T>) new TypeAdapter<Bundle>() {
        @Override
        public void write(JsonWriter out, Bundle bundle) throws IOException {
            if (bundle == null) {
                out.nullValue();
                return;
            }
            out.beginObject();
            for (String key : bundle.keySet()) {
                out.name(key);
                Object value = bundle.get(key);
                if (value == null) {
                    out.nullValue();
                } else {
                    gson.toJson(value, value.getClass(), out);
                }
            }
            out.endObject();
        }

        @Override
        public Bundle read(JsonReader in) throws IOException {
            switch (in.peek()) {
            case NULL:
                in.nextNull();
                return null;
            case BEGIN_OBJECT:
                return toBundle(readObject(in));
            default:
                throw new IOException("expecting object: " + in.getPath());
            }
        }

        private Bundle toBundle(List<Pair<String, Object>> values) throws IOException {
            Bundle bundle = new Bundle();
            for (Pair<String, Object> entry : values) {
                String key = entry.first;
                Object value = entry.second;
                if (value instanceof String) {
                    bundle.putString(key, (String) value);
                } else if (value instanceof Integer) {
                    bundle.putInt(key, ((Integer) value).intValue());
                } else if (value instanceof Long) {
                    bundle.putLong(key, ((Long) value).longValue());
                } else if (value instanceof Double) {
                    bundle.putDouble(key, ((Double) value).doubleValue());
                } else if (value instanceof Parcelable) {
                    bundle.putParcelable(key, (Parcelable) value);
                } else if (value instanceof List) {
                    List<Pair<String, Object>> objectValues = (List<Pair<String, Object>>) value;
                    Bundle subBundle = toBundle(objectValues);
                    bundle.putParcelable(key, subBundle);
                } else {
                    throw new IOException("Unparcelable key, value: " + key + ", " + value);
                }
            }
            return bundle;
        }

        private List<Pair<String, Object>> readObject(JsonReader in) throws IOException {
            List<Pair<String, Object>> object = new ArrayList<Pair<String, Object>>();
            in.beginObject();
            while (in.peek() != JsonToken.END_OBJECT) {
                switch (in.peek()) {
                case NAME:
                    String name = in.nextName();
                    Object value = readValue(in);
                    object.add(new Pair<String, Object>(name, value));
                    break;
                case END_OBJECT:
                    break;
                default:
                    throw new IOException("expecting object: " + in.getPath());
                }
            }
            in.endObject();
            return object;
        }

        private Object readValue(JsonReader in) throws IOException {
            switch (in.peek()) {
            case BEGIN_ARRAY:
                return readArray(in);
            case BEGIN_OBJECT:
                return readObject(in);
            case BOOLEAN:
                return in.nextBoolean();
            case NULL:
                in.nextNull();
                return null;
            case NUMBER:
                return readNumber(in);
            case STRING:
                return in.nextString();
            default:
                throw new IOException("expecting value: " + in.getPath());
            }
        }

        private Object readNumber(JsonReader in) throws IOException {
            double doubleValue = in.nextDouble();
            if (doubleValue - Math.ceil(doubleValue) == 0) {
                long longValue = (long) doubleValue;
                if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
                    return (int) longValue;
                }
                return longValue;
            }
            return doubleValue;
        }

        @SuppressWarnings("rawtypes")
        private List readArray(JsonReader in) throws IOException {
            List list = new ArrayList();
            in.beginArray();
            while (in.peek() != JsonToken.END_ARRAY) {
                Object element = readValue(in);
                list.add(element);
            }
            in.endArray();
            return list;
        }
    };
}

From source file:DateTimeTypeAdapter.java

License:Apache License

@Override
public DateTime read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
    }//  w  ww .j  a va 2s  .c om
    return new DateTime(in.nextString());
}