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:se.sics.datamodel.gson.ValueHolderAdapter.java

License:Open Source License

@Override
public ValueHolder read(JsonReader reader) throws IOException {
    Gson gson = GsonHelper.getGson();//from   w  w w.  jav a  2s. c  o m
    ValueHolder.Builder vhBuilder = new ValueHolder.Builder();

    if (reader.peek() != JsonToken.BEGIN_OBJECT) {
        throw new IOException();
    }
    reader.beginObject();

    if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("fieldMap")) {
        throw new IOException();
    }
    if (reader.peek() != JsonToken.BEGIN_ARRAY) {
        throw new IOException();
    }
    reader.beginArray();

    while (reader.peek() != JsonToken.END_ARRAY) {
        if (reader.peek() != JsonToken.BEGIN_OBJECT) {
            throw new IOException();
        }
        reader.beginObject();

        if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("id")) {
            throw new IOException();
        }
        ByteId fieldId = new ByteId((byte[]) gson.fromJson(reader, byte[].class));

        if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("field")) {
            throw new IOException();
        }
        JsonElement je = gson.fromJson(reader, JsonElement.class);

        vhBuilder.put(fieldId, je);

        if (reader.peek() != JsonToken.END_OBJECT) {
            throw new IOException();
        }
        reader.endObject();
    }

    reader.endArray();
    reader.endObject();
    return vhBuilder.build();
}

From source file:sf.net.experimaestro.manager.json.JsonConverter.java

License:Open Source License

private Json readNext(JsonReader jsonReader) throws IOException {
    final JsonToken token = jsonReader.peek();
    switch (token) {
    case BEGIN_ARRAY: {
        jsonReader.beginArray();/*from  ww w . j  a  v  a2 s .  com*/
        JsonArray array = new JsonArray();
        while (jsonReader.peek() != JsonToken.END_ARRAY) {
            array.add(readNext(jsonReader));
        }
        jsonReader.endArray();
        return array;
    }

    case BEGIN_OBJECT: {
        jsonReader.beginObject();
        JsonObject object = new JsonObject();
        while (jsonReader.peek() != JsonToken.END_OBJECT) {
            final String name = jsonReader.nextName();
            final Json value = readNext(jsonReader);
            object.put(name, value);
        }
        jsonReader.endObject();
        return object;
    }

    case BOOLEAN:
        return new JsonBoolean(jsonReader.nextBoolean());

    case STRING:
        return new JsonString(jsonReader.nextString());

    case NULL: {
        jsonReader.nextNull();
        return JsonNull.getSingleton();
    }

    case NUMBER:
        return new JsonReal(jsonReader.nextDouble());

    default:
        throw new RuntimeException("Cannot handle GSON token type: " + token.name());

    }
}

From source file:sf.net.experimaestro.utils.gson.JsonAdapter.java

License:Open Source License

private Json readValue(JsonReader in) throws IOException {
    final JsonToken peek = in.peek();
    switch (peek) {
    case BEGIN_ARRAY:
        in.beginArray();// w  w w .  jav  a  2s  .com
        JsonArray array = new JsonArray();
        while (in.peek() != JsonToken.END_ARRAY) {
            array.add(readValue(in));
        }
        in.endArray();
        return array;

    case BEGIN_OBJECT:
        in.beginObject();
        JsonObject object = new JsonObject();
        while (in.peek() != JsonToken.END_OBJECT) {
            final String name = in.nextName();
            final Json value = readValue(in);
            object.put(name, value);
        }
        in.endObject();
        return object;

    case BOOLEAN:
        return new JsonBoolean(in.nextBoolean());

    case NUMBER:
        final double value = in.nextDouble();
        if (value == Math.round(value))
            return new JsonInteger((long) value);
        return new JsonReal(value);

    case STRING:
        return new JsonString(in.nextString());

    case NULL:
        return JsonNull.getSingleton();

    case NAME:
    case END_OBJECT:
    case END_ARRAY:
    case END_DOCUMENT:
        throw new AssertionError("Not expecting " + peek);
    }
    return null;
}