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

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

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

From source file:com.owncloud.android.lib.resources.activities.models.RichElementTypeAdapter.java

License:Open Source License

@Override
public RichElement read(JsonReader in) throws IOException {
    RichElement richElement = new RichElement();
    in.beginArray();// w ww .java2  s .  co  m
    int count = 0;
    while (in.hasNext()) {
        if (count == 0) {
            richElement.setRichSubject(in.nextString());
        } else {
            JsonToken nextType = in.peek();

            switch (nextType) {
            case BEGIN_OBJECT:
                in.beginObject();
                read(richElement, in);
                in.endObject();
                break;

            case BEGIN_ARRAY:
                in.beginArray();
                in.endArray();
                break;

            default:
                // do nothing
                break;
            }

        }
        count++;
    }

    in.endArray();

    return richElement;
}

From source file:com.patloew.countries.util.CountryTypeAdapter.java

License:Apache License

@Override
public Country read(JsonReader in) throws IOException {
    try {/*w ww. ja  va 2  s. c om*/
        Country country = new Country();

        in.beginObject();

        while (in.hasNext()) {
            String name = in.nextName();

            switch (name) {
            case "alpha2Code": {
                // cannot be null, because it is the primary key
                country.alpha2Code = in.nextString();
                break;
            }
            case "alpha3Code": {
                country.alpha3Code = JsonUtils.readNullSafeString(in);
                break;
            }
            case "name": {
                country.name = JsonUtils.readNullSafeString(in);
                break;
            }
            case "nativeName": {
                country.nativeName = JsonUtils.readNullSafeString(in);
                break;
            }
            case "region": {
                country.region = JsonUtils.readNullSafeString(in);
                break;
            }
            case "capital": {
                country.capital = JsonUtils.readNullSafeString(in);
                break;
            }
            case "population": {
                country.population = JsonUtils.readNullSafeInteger(in);
                break;
            }
            case "latlng": {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                } else {
                    in.beginArray();
                    if (in.hasNext()) {
                        country.lat = JsonUtils.readNullSafeFloat(in);
                    }
                    if (in.hasNext()) {
                        country.lng = JsonUtils.readNullSafeFloat(in);
                    }
                    in.endArray();
                }
                break;
            }
            case "currencies": {
                country.currencies = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "borders": {
                country.borders = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "languages": {
                country.languages = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "translations": {
                country.translations = RealmStringMapEntryListTypeAdapter.INSTANCE.read(in);
                break;
            }
            default:
                in.skipValue();
                break;
            }
        }

        in.endObject();

        return country;
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static String readNullSafeString(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//  w  w  w . j  a  v a 2 s .  c o  m
        return null;
    } else {
        return reader.nextString();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Long readNullSafeLong(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from  w  ww .  j a  va  2 s .  co  m*/
        return null;
    } else {
        return reader.nextLong();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Integer readNullSafeInteger(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from   ww  w  . j  ava2 s. c  om
        return null;
    } else {
        return reader.nextInt();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Double readNullSafeDouble(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from   w  w  w  .j  a va  2 s. c om*/
        return null;
    } else {
        return reader.nextDouble();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Float readNullSafeFloat(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from w  ww .  java2s.  co  m
        return null;
    } else {
        return (float) reader.nextDouble();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Boolean readNullSafeBoolean(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*  w ww .ja  v a  2s.co  m*/
        return null;
    } else {
        return reader.nextBoolean();
    }
}

From source file:com.patloew.countries.util.RealmStringListTypeAdapter.java

License:Apache License

@Override
public RealmList<RealmString> read(JsonReader in) throws IOException {
    RealmList<RealmString> realmStrings = new RealmList<>();

    in.beginArray();//www.  j a v  a2s . com

    while (in.hasNext()) {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
        } else {
            RealmString realmString = new RealmString();
            realmString.value = in.nextString();
            realmStrings.add(realmString);
        }
    }

    in.endArray();

    return realmStrings;
}

From source file:com.patloew.countries.util.RealmStringMapEntryListTypeAdapter.java

License:Apache License

@Override
public RealmList<RealmStringMapEntry> read(JsonReader in) throws IOException {
    RealmList<RealmStringMapEntry> mapEntries = new RealmList<>();

    in.beginObject();/*from   w ww .ja  va  2  s  .c o m*/

    while (in.hasNext()) {
        RealmStringMapEntry realmStringMapEntry = new RealmStringMapEntry();
        realmStringMapEntry.key = in.nextName();

        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            realmStringMapEntry.value = null;
        } else {
            realmStringMapEntry.value = in.nextString();
        }

        mapEntries.add(realmStringMapEntry);
    }

    in.endObject();

    return mapEntries;
}