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:io.grpc.internal.JsonParser.java

License:Apache License

private static List<?> parseJsonArray(JsonReader jr) throws IOException {
    jr.beginArray();/*from www  .  j  a  v a2  s. co m*/
    List<Object> array = new ArrayList<>();
    while (jr.hasNext()) {
        Object value = parseRecursive(jr);
        array.add(value);
    }
    checkState(jr.peek() == JsonToken.END_ARRAY, "Bad token: " + jr.getPath());
    jr.endArray();
    return Collections.unmodifiableList(array);
}

From source file:json.tests.reader.GsonPerfTestWithCustomReader.java

License:Open Source License

/**
 * Read the first element of a array// w  ww  . j  a v  a2 s. com
 */
public JsonElement printFirstElement() throws IOException {
    JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements);
    JsonParser jp = new JsonParser();
    boolean isInArray = false;
    JsonElement element = null;

    // consume the first token
    jsonReader.beginObject();

    while (jsonReader.hasNext()) {
        JsonToken token = jsonReader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            jsonReader.beginArray();
            isInArray = true;
        } else if (isInArray && token.equals(JsonToken.END_ARRAY)) {
            jsonReader.endArray();
        } else if (isInArray && token.equals(JsonToken.BEGIN_OBJECT)) {
            element = jp.parse(jsonReader);
            break;
        } else {
            jsonReader.skipValue();
        }
    }
    return element;
}

From source file:json.tests.reader.GsonPerfTestWithCustomReader.java

License:Open Source License

/**
 * Read last element of a array//from w  w  w  .  j  a  v a2 s  .c om
 */
@Benchmark
public JsonElement printLastElement() throws IOException {
    JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements);
    JsonParser jp = new JsonParser();
    boolean isInArray = false;
    JsonElement element = null;
    int arrayIndex = 0;

    // consume the first token
    jsonReader.beginObject();

    while (jsonReader.hasNext()) {
        JsonToken token = jsonReader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            jsonReader.beginArray();
            isInArray = true;
        } else if (isInArray && token.equals(JsonToken.END_ARRAY)) {
            jsonReader.endArray();
        } else if (isInArray && token.equals(JsonToken.BEGIN_OBJECT)) {
            element = jp.parse(jsonReader);
            arrayIndex++;
        } else {
            jsonReader.skipValue();
        }
    }
    return element;
}

From source file:json.tests.reader.GsonPerfTestWithCustomReader.java

License:Open Source License

/**
 * Read nth element of a array/*from w ww .  j a v  a  2s.c  o m*/
 */
public JsonElement printNthElement() throws IOException {
    JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements);
    JsonParser jp = new JsonParser();
    boolean isInArray = false;
    JsonElement element = null;
    int arrayIndex = 0;

    // consume the first token
    jsonReader.beginObject();

    while (jsonReader.hasNext()) {
        JsonToken token = jsonReader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            jsonReader.beginArray();
            isInArray = true;
        } else if (isInArray && token.equals(JsonToken.END_ARRAY)) {
            jsonReader.endArray();
        } else if (isInArray && token.equals(JsonToken.BEGIN_OBJECT)) {
            arrayIndex++;
            if (arrayIndex == n) {
                element = jp.parse(jsonReader);
                break;
            } else {
                jsonReader.skipValue();
            }
        } else {
            jsonReader.skipValue();
        }
    }
    return element;
}

From source file:json.tests.reader.JSONStreamReader.java

License:Open Source License

/**
 * Move the pointer to the next required element of the json stream.
 * //from   w ww .  j  av  a  2 s.c o m
 * @param skip              Flag indicating whether the current object is a required element
 * @param isStartElement    Flag indicating whether to traverse inside the object or not. Value for this  is 
 *                          'true' when this method is called by {@link #beginObject()} or {@link #beginArray()}
 *                          methods. 
 * @throws IOException
 */
private void goToNextToken(boolean skip, boolean isStartElement) throws IOException {
    while (super.hasNext()) {
        this.hasNext = true;
        this.lastToken = this.currentToken;
        this.currentToken = super.peek();
        switch (this.currentToken) {
        case BEGIN_OBJECT:
            if (skip) {
                super.skipValue();
                popNonAnonymousObject();
                break;
            }
            handleObject(skip, isStartElement);
            return;
        case BEGIN_ARRAY:
            if (skip) {
                super.skipValue();
                popNonAnonymousObject();
                break;
            }
            handleArray(skip, isStartElement);
            return;
        case NAME:
            this.currentElementName = super.nextName();
            skip = !isRequired(this.currentElementName);

            // Whenever a element name is read, add it to the stack.
            this.elementsStack.push(new JsonElement(this.currentElementName, null));
            this.isNextObjectAnonymous = false;

            if (!skip) {
                return;
            }
            break;
        case NUMBER:
            popPrimitive();
            if (!skip) {
                /*
                 * Number can be int/long/double. hence reading it as string, and then parsing it 
                 * to the corresponding type, during #next****() method.
                 */
                this.currentElementValue = super.nextString();
                return;
            }
            super.skipValue();
            break;
        case STRING:
            popPrimitive();
            if (!skip) {
                this.currentElementValue = super.nextString();
                return;
            }
            super.skipValue();
            break;
        case BOOLEAN:
            popPrimitive();
            if (!skip) {
                this.currentElementValue = super.nextBoolean();
                return;
            }
            super.skipValue();
            break;
        case END_DOCUMENT:
            this.hasNext = false;
            return;
        default:
            super.skipValue();
        }
        isStartElement = false;
    }

    // when exiting the current element, set the current token type to END_OBJECT/END_ARRAY
    if (!this.elementsStack.isEmpty()) {
        ElementType startedElementType = this.elementsStack.peek().getType();
        if (startedElementType == ElementType.OBJECT) {
            this.currentToken = JsonToken.END_OBJECT;
        } else if (startedElementType == ElementType.ARRAY) {
            this.currentToken = JsonToken.END_ARRAY;
        }
    }

    this.hasNext = false;
}

From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java

License:Apache License

private void readValue() throws IOException {
    nextValue();/*  w  w w.j  a  v  a2 s . c  o  m*/
    tokenType = jsonReader.peek();
    if (tokenType == JsonToken.NAME) {
        state = JsonState.ValueName_END;
    } else if (tokenType == JsonToken.STRING || tokenType == JsonToken.NUMBER || tokenType == JsonToken.BOOLEAN
            || tokenType == JsonToken.NULL) {
        state = JsonState.ValueValue_END;
    } else if (tokenType == JsonToken.END_ARRAY) {
        state = JsonState.ValueEndArray;
    } else if (tokenType == JsonToken.END_OBJECT) {
        state = JsonState.ValueEndObject_END_1;
    }
}

From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java

License:Apache License

private void readEndObject() throws IOException, XMLStreamException {
    endObject();//w  w w .j  ava  2s  . c o m
    tokenType = jsonReader.peek();
    if (tokenType == JsonToken.END_OBJECT) {
        removeStackObj();
        state = JsonState.EndObjectEndObject;
    } else if (tokenType == JsonToken.END_ARRAY) {
        readEndArray();
        removeStackObj();
    } else if (tokenType == JsonToken.NAME) {
        removeStackObj();
        state = JsonState.EndObjectName;
    } else if (tokenType == JsonToken.BEGIN_OBJECT) {
        state = JsonState.EndObjectBeginObject_END;
    } else if (tokenType == JsonToken.END_DOCUMENT) {
        removeStackObj();
        state = JsonState.EndObjectEndDocument;
    }
}

From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

private void parseMaintainers(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException {
    reader.beginArray();/*from w  w w  .j  av a2  s .co  m*/
    String name = null, email = null;
    JsonToken token = reader.peek();

    while (token != JsonToken.END_ARRAY) {
        switch (token) {
        case BEGIN_OBJECT:
            reader.beginObject();
            name = email = null;
            break;
        case END_OBJECT:
            reader.endObject();
            plugin.addMaintainer(email, name);
            break;
        case NAME:
            String tagName = reader.nextName();
            if ("name".equals(tagName)) {
                name = reader.nextString();
                break;
            }
            if ("email".equals(tagName)) {
                email = reader.nextString();
                break;
            }
        default:
            Assert.isTrue(false, "Unexpected token");
            break;
        }
        token = reader.peek();
    }
    reader.endArray();
}

From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java

License:Apache License

private void consumeVariants(JsonReader reader, MutableModuleComponentResolveMetadata metadata)
        throws IOException {
    reader.beginArray();/*from  w ww .  ja  v a2  s . com*/
    while (reader.peek() != JsonToken.END_ARRAY) {
        consumeVariant(reader, metadata);
    }
    reader.endArray();
}

From source file:org.gw2InfoViewer.services.json.JsonConversionService.java

License:Open Source License

public static EventList parseEventListWithoutNames(InputStream json) throws IOException {
    List<Event> events;

    events = new ArrayList<Event>();

    JsonReader reader = new JsonReader(new InputStreamReader(json));
    reader.beginObject();/*from   w  w  w. j a va  2 s  . c o  m*/
    if (reader.nextName().equals("events")) {
        reader.beginArray();
        while (reader.peek() != JsonToken.END_ARRAY) {
            Event event = gsonBuilder.create().fromJson(reader, Event.class);
            events.add(event);
        }
        reader.endArray();
        reader.endObject();
    }
    return new EventList(events);
}