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

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

Introduction

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

Prototype

JsonToken END_OBJECT

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

Click Source Link

Document

The closing of a JSON object.

Usage

From source file:org.wso2.carbon.governance.rest.api.internal.JSONMessageBodyReader.java

License:Open Source License

/**
 * Traverses through a json object and maps the keys and values to a {@link Map}
 *
 * @param reader        {@link JsonReader}
 * @param map           map that the values to be added.
 * @param token         {@link JsonToken}
 * @param isArray       whether the object is inside a json array
 * @throws IOException  If unable to parse the json object
 *//*from   w  w w .j a  v  a2 s.com*/
protected void handleObject(JsonReader reader, Map<String, Object> map, JsonToken token, boolean isArray)
        throws IOException {
    String key = null;
    while (true) {
        if (token == null) {
            token = reader.peek();
        }

        if (JsonToken.BEGIN_OBJECT.equals(token)) {
            reader.beginObject();

        } else if (JsonToken.END_OBJECT.equals(token)) {
            reader.endObject();

        } else if (JsonToken.NAME.equals(token)) {
            key = reader.nextName();

        } else if (JsonToken.STRING.equals(token)) {
            String value = reader.nextString();
            handleValue(key, value, map, isArray);
            key = null;

        } else if (JsonToken.NUMBER.equals(token)) {
            Double value = reader.nextDouble();
            handleValue(key, value, map, isArray);
            key = null;

        } else if (token.equals(JsonToken.BEGIN_ARRAY)) {
            Map<String, Object> values = handleArray(reader);
            if (key != null) {
                map.put(key, values);
            }

        } else {
            reader.skipValue();
        }

        if (reader.hasNext()) {
            token = reader.peek();
        } else {
            break;
        }
    }
}

From source file:pl.softech.gw.json.TaskGsonTypeAdapter.java

License:Apache License

@Override
public ITask read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from w ww  .j  a v a 2  s  .  c om*/
        return null;
    }

    reader.beginObject();

    TaskInstanceCreator instanceCreator = new TaskInstanceCreator();
    while (reader.peek() != JsonToken.END_OBJECT) {

        String name = reader.nextName();
        Object value;
        if (reader.peek() == JsonToken.BEGIN_OBJECT && !name.equals("module")) {
            value = read(reader);
        } else {
            if (name.equals("module")) {
                value = gson.getAdapter(ProjectModule.class).read(reader);
            } else {
                value = reader.nextString();
            }
        }

        if (name.equals("class")) {
            instanceCreator.className = value.toString();
        } else {
            instanceCreator.params.put(name, new Param(name, value));
        }

    }

    reader.endObject();
    return instanceCreator.create();
}

From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts)
        throws IOException, JSONException, Exception {
    String n = null;/* w  ww  .  j  ava  2  s.  c o 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();
        }
    }
    return i;
}

From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public <T> int readJsnArr(JsonReader reader, String tableName, Class<T> clazz)
        throws IOException, JSONException, Exception {
    JSONObject o = new JSONObject();
    JsonToken peek = reader.peek();/*w  ww  .j  ava  2s.  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) {
                flowzrSyncActivity.notifyUser(
                        flowzrSyncActivity.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". "
                                + flowzrSyncActivity.getString(R.string.hint_run_background),
                        (int) (Math.round(j)));
            }
        }
    }
    reader.endArray();
    return i;
}

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();/*w w w .ja v  a  2  s . 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();//  www.  j  av a  2 s  .  c  om
        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();//from www .jav a2s  .  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;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private DataBlock parseDataBlock(JsonReader reader) throws IOException {
    DataBlock output = new DataBlock();
    reader.beginObject();/*  w  ww  .  ja v a  2s . c  om*/
    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        if (token == JsonToken.END_OBJECT) {
            break;
        }
        getDataBlockProperty(reader, output);
    }
    reader.endObject();
    return output;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private DataPoint parseDataPoint(JsonReader reader) throws IOException {
    DataPoint output = new DataPoint();
    reader.beginObject();/*w ww  . jav  a2s  .  com*/
    boolean exit = false;
    while (!exit && reader.hasNext()) {
        JsonToken token = reader.peek();
        if (token == JsonToken.END_OBJECT) {
            break;
        }
        getDataPointProperty(reader, output);
    }
    reader.endObject();
    return output;
}