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

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

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

From source file:com.flipkart.batching.gson.adapters.batch.TimeBatchTypeAdapter.java

License:Open Source License

@Override
public TimeBatch<T> read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();/*from  w ww  .ja  v a  2 s. c  om*/
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    long timeOut = 0L;
    DataCollection<T> dataCollection = null;

    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "timeOut":
            timeOut = BatchingTypeAdapters.LONG.read(reader);
            break;
        case "dataCollection":
            dataCollection = typeAdapter.read(reader);
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    return dataCollection == null ? null : new TimeBatch<>(dataCollection.dataCollection, timeOut);
}

From source file:com.flipkart.batching.gson.adapters.BatchImplTypeAdapter.java

License:Open Source License

@Override
public BatchImpl<T> read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();/*from www. j  av a  2s .  c  o m*/
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    DataCollection<T> dataCollection = null;
    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "dataCollection":
            dataCollection = typeAdapter.read(reader);
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    return dataCollection == null ? null : new BatchImpl<T>(dataCollection.dataCollection);
}

From source file:com.flipkart.batching.gson.adapters.data.EventDataTypeAdapter.java

License:Open Source License

@Override
public EventData read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();/* w  ww .j  ava2s .  c o m*/
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    EventData object = new EventData();
    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "eventId":
            object.setEventId(BatchingTypeAdapters.LONG.read(reader));
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    return object;
}

From source file:com.flipkart.batching.gson.adapters.data.TagDataTypeAdapter.java

License:Open Source License

@Override
public TagData read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();/*from   ww  w  . j  av a2 s  .  c  o m*/
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    Tag tag = null;
    long eventId = 0L;
    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "tag":
            tag = tagTypeAdapter.read(reader);
            break;
        case "eventId":
            eventId = BatchingTypeAdapters.LONG.read(reader);
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    TagData tagData = new TagData(tag);
    tagData.setEventId(eventId);
    return tagData;
}

From source file:com.flipkart.batching.gson.adapters.data.TagTypeAdapter.java

License:Open Source License

@Override
public Tag read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();/*w w w.ja  v a  2  s.co  m*/
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    String id = null;
    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "id":
            id = com.google.gson.internal.bind.TypeAdapters.STRING.read(reader);
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    return id == null ? null : new Tag(id);
}

From source file:com.flipkart.batching.gson.adapters.DataCollectionTypeAdapter.java

License:Open Source License

@Override
public DataCollection<T> read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();/* w  ww.ja v a  2  s.  c o m*/
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    Collection<T> collection = null;
    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "dataCollection":
            collection = collectionTypeAdapter.read(reader);
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    return null != collection ? new DataCollection<>(collection) : null;
}

From source file:com.flipkart.batching.gson.RuntimeTypeAdapterFactory.java

License:Open Source License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }//from w w w.j  a  v  a 2 s.c  om

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();

    for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = labelToTypeAdapter.get(entry.getKey());
        if (delegate == null) {
            delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        }
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
    }

    return new TypeAdapter<R>() {
        @Override
        public R read(JsonReader reader) throws IOException {

            if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
                reader.nextNull();
                return null;
            }
            if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
                reader.skipValue();
                return null;
            }
            reader.beginObject();

            String label = null;
            R result = null;
            while (reader.hasNext()) {
                String name = reader.nextName();
                com.google.gson.stream.JsonToken jsonToken = reader.peek();
                if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
                    reader.skipValue();
                    continue;
                }
                switch (name) {
                case "type":
                    label = TypeAdapters.STRING.read(reader);
                    break;
                case "value":
                    @SuppressWarnings("unchecked") // registration requires that subtype extends T
                    TypeAdapter<R> delegate = label == null ? null
                            : (TypeAdapter<R>) labelToDelegate.get(label);
                    if (delegate == null) {
                        throw new JsonParseException("cannot deserialize " + baseType + " subtype named "
                                + label + "; did you forget to register a subtype?");
                    }
                    result = delegate.read(reader);
                    break;
                default:
                    reader.skipValue();
                    break;
                }
            }

            reader.endObject();
            return result;
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
            Class<?> srcType = value.getClass();
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
            if (delegate == null) {
                throw new JsonParseException(
                        "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
            }
            String label = subtypeToLabel.get(srcType);
            out.beginObject();

            out.name("type");
            out.value(label);

            out.name("value");
            delegate.write(out, value);

            out.endObject();
        }
    }.nullSafe();
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public static <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts)
        throws IOException, JSONException, Exception {
    String n = null;/*from  w w  w  . j av a  2 s.  co  m*/
    int i = 0;

    while (reader.hasNext()) {
        JsonToken peek = reader.peek();

        String v = null;
        if (peek == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
        } else if (peek == JsonToken.NAME) {
            n = reader.nextName();
        } else if (peek == JsonToken.BEGIN_ARRAY) {
            if (n.equals(tableName)) {
                i = readJsnArr(reader, tableName, clazz);

            } else {
                if (n.equals("params")) {
                    reader.beginArray();
                    if (reader.hasNext()) {
                        reader.beginObject();
                        if (reader.hasNext()) {
                            n = reader.nextName();
                            v = reader.nextString();
                        }
                        reader.endObject();
                    }
                    reader.endArray();
                } else {
                    reader.skipValue();
                }
            }
        } else if (peek == JsonToken.END_OBJECT) {
            reader.endObject();
        } else if (peek == JsonToken.END_ARRAY) {
            reader.endArray();
        } else if (peek == JsonToken.STRING) {
            reader.skipValue();
        } else {
            reader.skipValue();
        }
    }
    return i;
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public static <T> int readJsnArr(JsonReader reader, String tableName, Class<T> clazz)
        throws IOException, JSONException, Exception {
    JSONObject o = new JSONObject();
    JsonToken peek = reader.peek();/*from  www  .  ja  v a2  s  .  c  o  m*/
    String n = null;
    reader.beginArray();
    int j = 0;
    int i = 0;
    while (reader.hasNext()) {
        peek = reader.peek();
        if (reader.peek() == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
        } else if (reader.peek() == JsonToken.END_OBJECT) {
            reader.endObject();
        }
        o = new JSONObject();
        while (reader.hasNext()) {
            peek = reader.peek();
            if (peek == JsonToken.NAME) {
                n = reader.nextName();
            } else if (peek == JsonToken.BEGIN_OBJECT) {
                reader.beginObject();
            } else if (peek == JsonToken.END_OBJECT) {
                reader.endObject();
            } else if (peek == JsonToken.BOOLEAN) {
                try {
                    o.put(n, reader.nextBoolean());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            } else if (peek == JsonToken.STRING) {
                try {
                    o.put(n, reader.nextString());

                } catch (JSONException e) {

                    e.printStackTrace();
                }
            } else if (peek == JsonToken.NUMBER) {
                try {
                    o.put(n, reader.nextDouble());

                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        }
        reader.endObject();
        if (o.has("key")) {
            i = i + 1;
            j = j + 1;
            if (j % 100 == 0) {
                j = 2;
            }
            saveEntityFromJson(o, tableName, clazz, i);
            if (i % 10 == 0) {
                //notifyUser(context.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". " + context.getString(R.string.hint_run_background), (int)(Math.round(j)));
            }
        }
    }
    reader.endArray();
    return i;
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public static void readDelete(JsonReader reader) throws IOException {
    reader.nextName();
    reader.beginArray();//  w  w w.  j ava  2 s .co  m
    while (reader.hasNext()) {
        reader.beginObject();
        reader.nextName(); //tablename
        String t = reader.nextString();
        reader.nextName(); //key
        execDelete(t, reader.nextString());
        reader.endObject();
    }
    reader.endArray();
}