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: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();/*ww w.  j  a  v  a  2s. co 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.gd.misc.test.JsonReader.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *//*from   ww w. j a  v a  2 s .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.github.gv2011.hprops.Converter.java

License:Open Source License

private void doObject(final JsonReader json, final Consumer<Property> target, final String prefix) {
    call(json::beginObject);//w  w w.  ja v a  2  s  .co  m
    JsonToken t = call(json::peek);
    while (!t.equals(JsonToken.END_OBJECT)) {
        final String key = (prefix.isEmpty() ? "" : prefix + ".") + encodeKey(call(json::nextName));
        t = call(json::peek);
        dispatch(json, target, t, key);
        t = call(json::peek);
    }
    call(json::endObject);
}

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

License:Open Source License

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

    w.writeStartElement(NS, "object");
    if (label != null)
        w.writeAttribute("name", label);
    for (;;) {/*from  w ww  .j a  v  a  2  s.c o  m*/
        if (r.peek() == JsonToken.END_OBJECT)
            break;
        if (r.peek() != JsonToken.NAME)
            throw new IllegalStateException(r.peek().name());
        String s = r.nextName();
        parse(s, r);
    }
    w.writeEndElement();
    r.endObject();
}

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

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *///from w w w .  j  a v  a  2 s.co  m
@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;/* ww  w.j ava 2s.  c  o  m*/
    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;//from  ww  w  .  j a  v  a2s  .  com
    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 object 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  va2 s.  c  o m
private void parseObject(JsonReader in, HashMap<String, Object> objMap, String name) throws IOException {
    HashMap<String, Object> innerObject = new HashMap<>();
    in.beginObject();

    while (in.peek() != JsonToken.END_OBJECT) {
        parseNext(in, innerObject);
    }

    in.endObject();
    objMap.put(name, innerObject);
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java

License:Apache License

public BasicDBObject parseDocument() throws IOException {

    // Different cases:
    // {} //from   w w  w.  j  av a  2  s  .c  om
    // ^^ many of these
    // [ {}, {}, {} ]
    // For each of these 2/3 cases, you might either want to grab the entire object, or a field
    // within the object

    try {
        while (true) { // (use exceptions to get outta here) 

            try {
                tok = reader.peek();
            } catch (Exception e) {
                // EOF or end of object, keep going and find out...
                tok = reader.peek();
            }
            //TESTED

            if (JsonToken.BEGIN_ARRAY == tok) {
                if (!_inTopLevelArray) {
                    reader.beginArray();
                    _inTopLevelArray = true;
                }
                if (objectIdentifiers.isEmpty()) {
                    while (reader.hasNext()) {
                        JsonElement meta = parser.parse(reader);
                        BasicDBObject currObj = convertJsonToDocument(meta);

                        if (null != currObj) {
                            return currObj;
                        } //(else carry on...)
                    }
                } //TESTED
                else {
                    while (reader.hasNext()) {
                        BasicDBObject currObj = getDocumentFromJson(false);
                        if (null != currObj) {
                            return currObj;
                        } //(else carry on...)
                    }
                } //TESTED
            } else if (JsonToken.BEGIN_OBJECT == tok) {
                if (objectIdentifiers.isEmpty()) {
                    JsonElement meta = parser.parse(reader);
                    BasicDBObject currObj = convertJsonToDocument(meta);

                    if (null != currObj) {
                        return currObj;
                    } //(else carry on...)

                } //TESTED (single and multiple doc case)
                else {
                    BasicDBObject currObj = getDocumentFromJson(false);
                    if (null != currObj) {
                        return currObj;
                    } //(else carry on...)

                } //TESTED (single and multiple doc case)   
            } else if ((JsonToken.END_DOCUMENT == tok) || (JsonToken.END_ARRAY == tok)
                    || (JsonToken.END_OBJECT == tok)) {
                return null;
            } else { // Must be recursing through the next level(s)
                BasicDBObject currObj = getDocumentFromJson(false);
                if (null != currObj) {
                    return currObj;
                } //(else carry on...)               
            }
        } // (end loop forever - exception out)
    } catch (Exception e) {
    } // This is our EOF

    return null;
}

From source file:com.magnet.android.mms.request.marshall.GsonStreamReader.java

License:Open Source License

@Override
public int getTokenType() throws MarshallingException {
    int result = TOKEN_TYPE_FIELD_VALUE;
    JsonToken tokenType;/*w ww.  j  a v a  2 s .c  om*/
    try {
        tokenType = jr.peek();
    } catch (Exception e) {
        throw new MarshallingException(e);
    }
    if (tokenType == JsonToken.BEGIN_OBJECT) {
        result = TOKEN_TYPE_START_OBJECT;
    } else if (tokenType == JsonToken.BEGIN_ARRAY) {
        result = TOKEN_TYPE_START_ARRAY;
    } else if (tokenType == JsonToken.END_OBJECT) {
        result = TOKEN_TYPE_END_OBJECT;
    } else if (tokenType == JsonToken.END_ARRAY) {
        result = TOKEN_TYPE_END_ARRAY;
    } else if (tokenType == JsonToken.NAME) {
        result = TOKEN_TYPE_FIELD_NAME;
    } else if (tokenType == JsonToken.NULL) {
        result = TOKEN_TYPE_NULL;
    } else if (tokenType == JsonToken.END_DOCUMENT) {
        result = TOKEN_TYPE_END_DOCUMENT;
    }
    return result;
}