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

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

Introduction

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

Prototype

public String nextString() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#STRING string value of the next token, 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 .j av  a  2 s.c  o  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.owncloud.android.lib.resources.activities.models.RichElementTypeAdapter.java

License:Open Source License

private String getNextString(JsonReader in) {
    try {/*from   www .  j a v a2 s  . c  om*/
        return in.nextString();
    } catch (IllegalStateException | IOException e) {
        return "";
    }
}

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

License:Apache License

@Override
public Country read(JsonReader in) throws IOException {
    try {//from   w w w  . ja v a  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();//from   www  .j a  v  a 2s.c  om
        return null;
    } else {
        return reader.nextString();
    }
}

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();//ww w .  jav a2  s .  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();/*ww  w .j a v  a 2 s .c om*/

    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;
}

From source file:com.qdeve.oilprice.configuration.GsonMoneyTypeAdapter.java

License:Apache License

@Override
public Money read(JsonReader in) throws IOException {
    String currency = null;/*from w  w  w . j a  v  a2s. c om*/
    Double price = null;
    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case CURRENTY_TAG:
            currency = in.nextString();
            break;
        case PRICE_TAG:
            price = in.nextDouble();
            break;
        }
    }
    in.endObject();
    return MonetaryUtils.newMoneyFrom(price, currency);
}

From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java

License:Open Source License

private static JSONObject getJSONObjectFromReader(JsonReader jsonReader) {
    JSONObject jObj = new JSONObject();
    JsonToken tokenInstance;/*from   w w w  .j av  a 2 s.  c  o m*/
    try {
        tokenInstance = jsonReader.peek();
        if (tokenInstance == JsonToken.BEGIN_OBJECT)
            jsonReader.beginObject();
        else if (tokenInstance == JsonToken.BEGIN_ARRAY)
            jsonReader.beginArray();

        while (jsonReader.hasNext()) {
            JsonToken token;
            String name;
            try {
                name = jsonReader.nextName();
                token = jsonReader.peek();

                //Log.d(TAG, token.toString());

                switch (token) {
                case NUMBER:
                    jObj.put(name, jsonReader.nextLong());
                    break;
                case NULL:
                    jsonReader.skipValue();
                    break;
                case BOOLEAN:
                    jObj.put(name, jsonReader.nextBoolean());
                    break;
                case BEGIN_OBJECT:
                    //jsonReader.beginObject();
                    jObj.put(name, getJSONObjectFromReader(jsonReader));
                    //jsonReader.endObject();
                    break;
                case BEGIN_ARRAY:
                    jsonReader.skipValue();
                    break;
                default:
                    jObj.put(name, jsonReader.nextString());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                jsonReader.skipValue();
            }
        }

        if (tokenInstance == JsonToken.BEGIN_OBJECT)
            jsonReader.endObject();
        else if (tokenInstance == JsonToken.BEGIN_ARRAY)
            jsonReader.endArray();

        return jObj;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.replaymod.replaystudio.pathing.serialize.LegacyKeyframeSetAdapter.java

License:MIT License

@SuppressWarnings("unchecked")
@Override//from   ww  w. j a  va  2s .c  om
public KeyframeSet[] read(JsonReader in) throws IOException {
    List<KeyframeSet> sets = new ArrayList<>();

    in.beginArray();
    while (in.hasNext()) { //iterate over all array entries

        KeyframeSet set = new KeyframeSet();
        List<Keyframe> positionKeyframes = new ArrayList<>();
        List<Keyframe> timeKeyframes = new ArrayList<>();

        in.beginObject();
        while (in.hasNext()) { //iterate over all object entries
            String jsonTag = in.nextName();

            if ("name".equals(jsonTag)) {
                set.name = in.nextString();

                //TODO: Adapt to new Spectator Keyframe system
            } else if ("positionKeyframes".equals(jsonTag)) {
                in.beginArray();
                while (in.hasNext()) {
                    Keyframe<AdvancedPosition> newKeyframe = new Keyframe<>();
                    Integer spectatedEntityID = null;
                    in.beginObject();
                    while (in.hasNext()) {
                        String jsonKeyframeTag = in.nextName();
                        if ("value".equals(jsonKeyframeTag) || "position".equals(jsonKeyframeTag)) {
                            SpectatorData spectatorData = new Gson().fromJson(in, SpectatorData.class);
                            if (spectatorData.spectatedEntityID != null) {
                                newKeyframe.value = spectatorData;
                            } else {
                                newKeyframe.value = new AdvancedPosition();
                                newKeyframe.value.x = spectatorData.x;
                                newKeyframe.value.y = spectatorData.y;
                                newKeyframe.value.z = spectatorData.z;
                                newKeyframe.value.yaw = spectatorData.yaw;
                                newKeyframe.value.pitch = spectatorData.pitch;
                                newKeyframe.value.roll = spectatorData.roll;
                            }
                        } else if ("realTimestamp".equals(jsonKeyframeTag)) {
                            newKeyframe.realTimestamp = in.nextInt();
                        } else if ("spectatedEntityID".equals(jsonKeyframeTag)) {
                            spectatedEntityID = in.nextInt();
                        }
                    }

                    if (spectatedEntityID != null) {
                        AdvancedPosition pos = newKeyframe.value;
                        SpectatorData spectatorData = new SpectatorData();
                        spectatorData.spectatedEntityID = spectatedEntityID;
                        newKeyframe.value = spectatorData;
                        newKeyframe.value.x = pos.x;
                        newKeyframe.value.y = pos.y;
                        newKeyframe.value.z = pos.z;
                        newKeyframe.value.yaw = pos.yaw;
                        newKeyframe.value.pitch = pos.pitch;
                        newKeyframe.value.roll = pos.roll;
                    }

                    in.endObject();

                    positionKeyframes.add(newKeyframe);
                }
                in.endArray();

            } else if ("timeKeyframes".equals(jsonTag)) {
                in.beginArray();
                while (in.hasNext()) {
                    Keyframe<TimestampValue> newKeyframe = new Keyframe<>();

                    in.beginObject();
                    while (in.hasNext()) {
                        String jsonKeyframeTag = in.nextName();
                        if ("timestamp".equals(jsonKeyframeTag)) {
                            TimestampValue timestampValue = new TimestampValue();
                            timestampValue.value = in.nextInt();
                            newKeyframe.value = timestampValue;
                        } else if ("value".equals(jsonKeyframeTag)) {
                            newKeyframe.value = new Gson().fromJson(in, TimestampValue.class);
                        } else if ("realTimestamp".equals(jsonKeyframeTag)) {
                            newKeyframe.realTimestamp = in.nextInt();
                        }
                    }
                    in.endObject();

                    timeKeyframes.add(newKeyframe);
                }
                in.endArray();

            } else if ("customObjects".equals(jsonTag)) {
                set.customObjects = new Gson().fromJson(in, CustomImageObject[].class);
            }
        }
        in.endObject();

        set.positionKeyframes = positionKeyframes.toArray(new Keyframe[positionKeyframes.size()]);
        set.timeKeyframes = timeKeyframes.toArray(new Keyframe[timeKeyframes.size()]);
        sets.add(set);
    }
    in.endArray();

    return sets.toArray(new KeyframeSet[sets.size()]);
}

From source file:com.replaymod.replaystudio.pathing.serialize.TimelineSerialization.java

License:MIT License

public Map<String, Timeline> deserialize(String serialized) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(serialized));
    Map<String, Timeline> timelines = new LinkedHashMap<>();
    reader.beginObject();//from  w w  w .  ja va 2 s  . co  m
    while (reader.hasNext()) {
        Timeline timeline = registry.createTimeline();
        timelines.put(reader.nextName(), timeline);
        reader.beginArray();
        while (reader.hasNext()) {
            Path path = timeline.createPath();
            reader.beginObject();
            List<Integer> segments = new ArrayList<>();
            List<Interpolator> interpolators = new ArrayList<>();
            while (reader.hasNext()) {
                switch (reader.nextName()) {
                case "keyframes":
                    reader.beginArray();
                    while (reader.hasNext()) {
                        long time = 0;
                        Map<Property, Object> properties = new HashMap<>();
                        reader.beginObject();
                        while (reader.hasNext()) {
                            switch (reader.nextName()) {
                            case "time":
                                time = reader.nextLong();
                                break;
                            case "properties":
                                reader.beginObject();
                                while (reader.hasNext()) {
                                    String id = reader.nextName();
                                    Property property = timeline.getProperty(id);
                                    if (property == null) {
                                        throw new IOException("Unknown property: " + id);
                                    }
                                    Object value = property.fromJson(reader);
                                    properties.put(property, value);
                                }
                                reader.endObject();
                                break;
                            }
                        }
                        reader.endObject();
                        Keyframe keyframe = path.insert(time);
                        for (Map.Entry<Property, Object> entry : properties.entrySet()) {
                            keyframe.setValue(entry.getKey(), entry.getValue());
                        }
                    }
                    reader.endArray();
                    break;
                case "segments":
                    reader.beginArray();
                    while (reader.hasNext()) {
                        if (reader.peek() == JsonToken.NULL) {
                            reader.nextNull();
                            segments.add(null);
                        } else {
                            segments.add(reader.nextInt());
                        }
                    }
                    reader.endArray();
                    break;
                case "interpolators":
                    reader.beginArray();
                    while (reader.hasNext()) {
                        reader.beginObject();
                        Interpolator interpolator = null;
                        Set<String> properties = new HashSet<>();
                        while (reader.hasNext()) {
                            switch (reader.nextName()) {
                            case "type":
                                interpolator = registry.deserializeInterpolator(reader);
                                break;
                            case "properties":
                                reader.beginArray();
                                while (reader.hasNext()) {
                                    properties.add(reader.nextString());
                                }
                                reader.endArray();
                                break;
                            }
                        }
                        if (interpolator == null) {
                            throw new IOException("Missing interpolator type");
                        }
                        for (String propertyName : properties) {
                            Property property = timeline.getProperty(propertyName);
                            if (property == null) {
                                throw new IOException("Timeline does not know property '" + propertyName + "'");
                            }
                            interpolator.registerProperty(property);
                        }
                        interpolators.add(interpolator);
                        reader.endObject();
                    }
                    reader.endArray();
                    break;
                }
            }
            Iterator<Integer> iter = segments.iterator();
            for (PathSegment segment : path.getSegments()) {
                Integer next = iter.next();
                if (next != null) {
                    segment.setInterpolator(interpolators.get(next));
                }
            }
            reader.endObject();
        }
        reader.endArray();
    }
    reader.endObject();
    return timelines;
}