Example usage for com.google.gson.stream JsonToken END_ARRAY

List of usage examples for com.google.gson.stream JsonToken END_ARRAY

Introduction

In this page you can find the example usage for com.google.gson.stream JsonToken END_ARRAY.

Prototype

JsonToken END_ARRAY

To view the source code for com.google.gson.stream JsonToken END_ARRAY.

Click Source Link

Document

The closing of a JSON array.

Usage

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;/*  w ww .ja  v  a  2  s  .  c om*/
    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.gd.misc.test.JsonReader.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *//* w  w  w .  j  av a  2s  .  c  o m*/
public JsonToken peek() throws IOException {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case PEEKED_END_OBJECT:
        return JsonToken.END_OBJECT;
    case PEEKED_BEGIN_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case PEEKED_END_ARRAY:
        return JsonToken.END_ARRAY;
    case PEEKED_SINGLE_QUOTED_NAME:
    case PEEKED_DOUBLE_QUOTED_NAME:
    case PEEKED_UNQUOTED_NAME:
        return JsonToken.NAME;
    case PEEKED_TRUE:
    case PEEKED_FALSE:
        return JsonToken.BOOLEAN;
    case PEEKED_NULL:
        return JsonToken.NULL;
    case PEEKED_SINGLE_QUOTED:
    case PEEKED_DOUBLE_QUOTED:
    case PEEKED_UNQUOTED:
    case PEEKED_BUFFERED:
        return JsonToken.STRING;
    case PEEKED_LONG:
    case PEEKED_NUMBER:
        return JsonToken.NUMBER;
    case PEEKED_EOF:
        return JsonToken.END_DOCUMENT;
    default:
        throw new AssertionError();
    }
}

From source file:com.getperka.flatpack.Unpacker.java

License:Apache License

private <T> FlatPackEntity<T> unpack(Type returnType, JsonReader reader, Principal principal)
        throws IOException {
    // Hold temporary state for deserialization
    DeserializationContext context = contexts.get();

    /*//from  w ww  .jav a2s  . co m
     * Decoding is done as a two-pass operation since the runtime type of an allocated object cannot
     * be swizzled. The per-entity data is held as a semi-reified JsonObject to be passed off to a
     * Codex.
     */
    Map<HasUuid, JsonObject> entityData = FlatPackCollections.mapForIteration();
    // Used to populate the entityData map
    JsonParser jsonParser = new JsonParser();
    /*
     * The reader is placed in lenient mode as an aid to developers, however all output will be
     * strictly formatted.
     */
    reader.setLenient(true);

    // The return value
    @SuppressWarnings("unchecked")
    FlatPackEntity<T> toReturn = (FlatPackEntity<T>) FlatPackEntity.create(returnType, null, principal);
    // Stores the single, top-level value in the payload for two-pass reification
    JsonElement value = null;

    if (reader.peek().equals(JsonToken.NULL)) {
        return toReturn;
    }

    reader.beginObject();

    while (JsonToken.NAME.equals(reader.peek())) {
        String name = reader.nextName();
        if ("data".equals(name)) {
            // data : { "fooEntity" : [ { ... }, { ... } ]
            reader.beginObject();
            while (JsonToken.NAME.equals(reader.peek())) {
                // Turn "fooEntity" into the actual FooEntity class
                String simpleName = reader.nextName();
                Class<? extends HasUuid> clazz = typeContext.getClass(simpleName);
                if (clazz == null) {
                    if (ignoreUnresolvableTypes) {
                        reader.skipValue();
                        continue;
                    } else {
                        throw new UnsupportedOperationException("Cannot resolve type " + simpleName);
                    }
                } else if (Modifier.isAbstract(clazz.getModifiers())) {
                    throw new UnsupportedOperationException(
                            "A subclass of " + simpleName + " must be used instead");
                }
                context.pushPath("allocating " + simpleName);
                try {
                    // Find the Codex for the requested entity type
                    EntityCodex<?> codex = (EntityCodex<?>) typeContext.getCodex(clazz);

                    // Take the n-many property objects and stash them for later decoding
                    reader.beginArray();
                    while (!JsonToken.END_ARRAY.equals(reader.peek())) {
                        JsonObject chunk = jsonParser.parse(reader).getAsJsonObject();
                        HasUuid entity = codex.allocate(chunk, context);
                        toReturn.addExtraEntity(entity);
                        entityData.put(entity, chunk.getAsJsonObject());
                    }
                    reader.endArray();
                } finally {
                    context.popPath();
                }
            }
            reader.endObject();
        } else if ("errors".equals(name)) {
            // "errors" : { "path" : "problem", "path2" : "problem2" }
            reader.beginObject();
            while (JsonToken.NAME.equals(reader.peek())) {
                String path = reader.nextName();
                if (JsonToken.STRING.equals(reader.peek()) || JsonToken.NUMBER.equals(reader.peek())) {
                    toReturn.addError(path, reader.nextString());
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } else if ("metadata".equals(name)) {
            reader.beginArray();

            while (!JsonToken.END_ARRAY.equals(reader.peek())) {
                EntityMetadata meta = new EntityMetadata();
                JsonObject metaElement = jsonParser.parse(reader).getAsJsonObject();
                metaCodex.get().readProperties(meta, metaElement, context);
                toReturn.addMetadata(meta);
            }

            reader.endArray();
        } else if ("value".equals(name)) {
            // Just stash the value element in case it occurs first
            value = jsonParser.parse(reader);
        } else if (JsonToken.STRING.equals(reader.peek()) || JsonToken.NUMBER.equals(reader.peek())) {
            // Save off any other simple properties
            toReturn.putExtraData(name, reader.nextString());
        } else {
            // Ignore random other entries
            reader.skipValue();
        }
    }

    reader.endObject();
    reader.close();

    for (Map.Entry<HasUuid, JsonObject> entry : entityData.entrySet()) {
        HasUuid entity = entry.getKey();
        EntityCodex<HasUuid> codex = (EntityCodex<HasUuid>) typeContext.getCodex(entity.getClass());
        codex.readProperties(entity, entry.getValue(), context);
    }

    @SuppressWarnings("unchecked")
    Codex<T> returnCodex = (Codex<T>) typeContext.getCodex(toReturn.getType());
    toReturn.withValue(returnCodex.read(value, context));

    for (Map.Entry<UUID, String> entry : context.getWarnings().entrySet()) {
        toReturn.addWarning(entry.getKey().toString(), entry.getValue());
    }

    // Process metadata
    for (EntityMetadata meta : toReturn.getMetadata()) {
        if (meta.isPersistent()) {
            HasUuid entity = context.getEntity(meta.getUuid());
            if (entity instanceof PersistenceAware) {
                ((PersistenceAware) entity).markPersistent();
            }
        }
    }

    context.runPostWork();
    context.close();

    return toReturn;
}

From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java

License:Apache License

private Positions parseSinglePosition(JsonReader in) throws IOException {
    Positions parsed;//from   w w w  . ja v  a 2s.  c o  m
    double lon = in.nextDouble();
    double lat = in.nextDouble();
    double alt = 0.0;

    if (in.hasNext()) {
        alt = in.nextDouble();
    }

    // Skip eventual altitude or other dimensions
    while (in.peek() != JsonToken.END_ARRAY) {
        in.skipValue();
    }

    parsed = new SinglePosition(Coordinates.of(lon, lat, alt));
    return parsed;
}

From source file:com.github.gv2011.hprops.Converter.java

License:Open Source License

private void doList(final JsonReader json, final Consumer<Property> target, final String prefix) {
    call(json::beginArray);/*from   w  w w. j  a v  a  2  s. c om*/
    int i = 1;
    JsonToken t = call(json::peek);
    while (!t.equals(JsonToken.END_ARRAY)) {
        final String key = (prefix.isEmpty() ? "" : prefix + ".") + i;
        dispatch(json, target, t, key);
        i++;
        t = call(json::peek);
    }
    call(json::endArray);
}

From source file:com.github.lindenb.gatkui.Json2Xml.java

License:Open Source License

private void parseArray(String label, JsonReader r) throws Exception {

    w.writeStartElement(NS, "array");
    if (label != null)
        w.writeAttribute("name", label);
    for (;;) {//from  www .j a  v  a 2  s . com
        if (r.peek() == JsonToken.END_ARRAY)
            break;
        parse(null, r);
    }
    w.writeEndElement();
    r.endArray();
}

From source file:com.goide.dlv.JsonReaderEx.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *///from   w ww .  j av a2 s.c om
@NotNull
public JsonToken peek() {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case PEEKED_END_OBJECT:
        return JsonToken.END_OBJECT;
    case PEEKED_BEGIN_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case PEEKED_END_ARRAY:
        return JsonToken.END_ARRAY;
    case PEEKED_SINGLE_QUOTED_NAME:
    case PEEKED_DOUBLE_QUOTED_NAME:
    case PEEKED_UNQUOTED_NAME:
        return JsonToken.NAME;
    case PEEKED_TRUE:
    case PEEKED_FALSE:
        return JsonToken.BOOLEAN;
    case PEEKED_NULL:
        return JsonToken.NULL;
    case PEEKED_SINGLE_QUOTED:
    case PEEKED_DOUBLE_QUOTED:
    case PEEKED_UNQUOTED:
    case PEEKED_BUFFERED:
        return JsonToken.STRING;
    case PEEKED_LONG:
    case PEEKED_NUMBER:
        return JsonToken.NUMBER;
    case PEEKED_EOF:
        return JsonToken.END_DOCUMENT;
    default:
        throw new AssertionError();
    }
}

From source file:com.gs.reladomo.serial.gson.GsonWrappedListTypedAdapter.java

License:Apache License

@Override
public SerializedList<U, T> read(JsonReader jsonReader) throws IOException {
    ReladomoDeserializer deserializer;/*from   w  ww . j  a v a 2s. com*/
    if (this.typeClass == null) {
        deserializer = new ReladomoDeserializer();
    } else {
        deserializer = new ReladomoDeserializer(typeClass);
    }
    deserializer.setIgnoreUnknown();
    JsonDeserializerState state = JsonDeserializerState.ListStartState.INSTANCE;
    while (true) {
        JsonToken nextToken = jsonReader.peek();
        //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT;
        if (JsonToken.BEGIN_OBJECT == nextToken) {
            jsonReader.beginObject();
            state = state.startObject(deserializer);
        } else if (JsonToken.END_OBJECT == nextToken) {
            jsonReader.endObject();
            state = state.endObject(deserializer);
        } else if (JsonToken.BEGIN_ARRAY == nextToken) {
            jsonReader.beginArray();
            state = state.startArray(deserializer);
        } else if (JsonToken.END_ARRAY == nextToken) {
            jsonReader.endArray();
            state = state.endArray(deserializer);
        } else if (JsonToken.BOOLEAN == nextToken) {
            if (jsonReader.nextBoolean()) {
                state = state.valueTrue(deserializer);
            } else {
                state = state.valueFalse(deserializer);
            }
        } else if (JsonToken.NAME == nextToken) {
            String name = jsonReader.nextName();
            state = state.fieldName(name, deserializer);
        } else if (JsonToken.NUMBER == nextToken) {
            String value = jsonReader.nextString();
            state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss
        } else if (JsonToken.STRING == nextToken) {
            String value = jsonReader.nextString();
            Attribute attribute = deserializer.getCurrentAttribute();
            if (attribute instanceof TimestampAttribute) {
                Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value);
                state = state.valueTimestamp(timestamp, deserializer);
            } else {
                state = state.valueString(value, deserializer);
            }
        } else if (JsonToken.NULL == nextToken) {
            jsonReader.nextNull();
            state = state.valueNull(deserializer);
        } else if (JsonToken.END_DOCUMENT == nextToken) {
            break;
        }
    }
    return deserializer.getDeserializedResultAsList();
}

From source file:com.gs.reladomo.serial.gson.GsonWrappedTypedAdapter.java

License:Apache License

@Override
public Serialized<T> read(JsonReader jsonReader) throws IOException {
    ReladomoDeserializer deserializer;/*w  w  w  . j  a v a2s .  co m*/
    if (this.typeClass == null) {
        deserializer = new ReladomoDeserializer();
    } else {
        deserializer = new ReladomoDeserializer(typeClass);
    }
    deserializer.setIgnoreUnknown();
    JsonDeserializerState state = JsonDeserializerState.NormalParserState.INSTANCE;
    while (true) {
        JsonToken nextToken = jsonReader.peek();
        //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT;
        if (JsonToken.BEGIN_OBJECT == nextToken) {
            jsonReader.beginObject();
            state = state.startObject(deserializer);
        } else if (JsonToken.END_OBJECT == nextToken) {
            jsonReader.endObject();
            state = state.endObject(deserializer);
        } else if (JsonToken.BEGIN_ARRAY == nextToken) {
            jsonReader.beginArray();
            state = state.startArray(deserializer);
        } else if (JsonToken.END_ARRAY == nextToken) {
            jsonReader.endArray();
            state = state.endArray(deserializer);
        } else if (JsonToken.BOOLEAN == nextToken) {
            if (jsonReader.nextBoolean()) {
                state = state.valueTrue(deserializer);
            } else {
                state = state.valueFalse(deserializer);
            }
        } else if (JsonToken.NAME == nextToken) {
            String name = jsonReader.nextName();
            state = state.fieldName(name, deserializer);
        } else if (JsonToken.NUMBER == nextToken) {
            String value = jsonReader.nextString();
            state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss
        } else if (JsonToken.STRING == nextToken) {
            String value = jsonReader.nextString();
            Attribute attribute = deserializer.getCurrentAttribute();
            if (attribute instanceof TimestampAttribute) {
                Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value);
                state = state.valueTimestamp(timestamp, deserializer);
            } else {
                state = state.valueString(value, deserializer);
            }
        } else if (JsonToken.NULL == nextToken) {
            jsonReader.nextNull();
            state = state.valueNull(deserializer);
        } else if (JsonToken.END_DOCUMENT == nextToken) {
            break;
        }
    }
    return deserializer.getDeserializedResult();
}

From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java

License:Open Source License

/**
 * Parses a JSON array and adds it to the main object map.
 *
 * @param in {@link JsonReader} object used for parsing
 * @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object
 * @param name key value to go with the resulting value of this method pass
 * @throws IOException signals that there has been an IO exception
 *///from   www.  j a  va  2  s  .com
private void parseArray(JsonReader in, HashMap<String, Object> objMap, String name) throws IOException {
    List<HashMap<String, Object>> array = new ArrayList<>();
    in.beginArray();

    while (in.peek() != JsonToken.END_ARRAY) {
        HashMap<String, Object> arrayItem = new HashMap<>();
        parseNext(in, arrayItem);
        array.add(arrayItem);
    }

    in.endArray();
    objMap.put(name, array);
}