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

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

Introduction

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

Prototype

JsonToken NAME

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

Click Source Link

Document

A JSON property name.

Usage

From source file:org.translator.java.AppInventorScreen.java

License:Apache License

private void parseComponentJSON(JsonReader reader) throws IOException {
    reader.beginObject();//w ww.ja va 2 s .c om

    while (reader.peek() == JsonToken.NAME) {
        String name = reader.nextName();
        if (name.equals("Properties"))
            form = new AppInventorProperties(reader);
        else {
            String d = reader.nextString();
            if (d.matches("True|False"))
                d = d.toLowerCase();
            data.put(name, d);
        }
    }

    reader.endObject();
}

From source file:org.ttrssreader.net.JSONConnector.java

License:Open Source License

private boolean parseArticle(final Article a, final JsonReader reader,
        final Set<Article.ArticleField> skipNames, final IArticleOmitter filter) throws IOException {

    boolean skipObject = false;
    while (reader.hasNext() && reader.peek().equals(JsonToken.NAME)) {
        if (skipObject) {
            // field name
            reader.skipValue();/*from ww  w .  ja  va2  s  .  c  o  m*/
            // field value
            reader.skipValue();
            continue;
        }

        String name = reader.nextName();
        Article.ArticleField field = Article.ArticleField.valueOf(name);

        try {

            if (skipNames != null && skipNames.contains(field)) {
                reader.skipValue();
                continue;
            }

            switch (field) {
            case id:
                a.id = reader.nextInt();
                break;
            case title:
                a.title = reader.nextString();
                break;
            case unread:
                a.isUnread = reader.nextBoolean();
                break;
            case updated:
                a.updated = new Date(reader.nextLong() * 1000);
                break;
            case feed_id:
                if (reader.peek() == JsonToken.NULL)
                    reader.nextNull();
                else
                    a.feedId = reader.nextInt();
                break;
            case content:
                a.content = reader.nextString();
                break;
            case link:
                a.url = reader.nextString();
                break;
            case comments:
                a.commentUrl = reader.nextString();
                break;
            case attachments:
                a.attachments = parseAttachments(reader);
                break;
            case marked:
                a.isStarred = reader.nextBoolean();
                break;
            case published:
                a.isPublished = reader.nextBoolean();
                break;
            case labels:
                a.labels = parseLabels(reader);
                break;
            case author:
                a.author = reader.nextString();
                break;
            default:
                reader.skipValue();
                continue;
            }

            if (filter != null)
                skipObject = filter.omitArticle(field, a);

        } catch (IllegalArgumentException | StopJsonParsingException | IOException e) {
            Log.w(TAG, "Result contained illegal value for entry \"" + field + "\".");
            reader.skipValue();
        }
    }
    return skipObject;
}

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 ww w .  ja v a2s  .  c  o m*/
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: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;//from  w w w  . ja v  a  2s .com
    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();/*from   ww w  .  j  ava 2s  .  c om*/
    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.client.gson.CGetObjAdapter.java

License:Open Source License

@Override
public CGetObj read(JsonReader reader) throws IOException {
    Gson gson = CGsonHelper.getGson();/*  w ww .  j  a  va 2  s  .  c  om*/

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

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

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

    reader.endObject();
    return new CGetObj(Triplet.with(dbId, typeId, objId));
}

From source file:se.sics.datamodel.client.gson.CGetTypeAdapter.java

License:Open Source License

@Override
public CGetType read(JsonReader reader) throws IOException {
    Gson gson = CGsonHelper.getGson();/*  w w  w  .  j a  va 2  s. co m*/

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

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

    reader.endObject();
    return new CGetType(Pair.with(dbId, typeId));
}

From source file:se.sics.datamodel.client.gson.CPutObjAdapter.java

License:Open Source License

@Override
public CPutObj read(JsonReader reader) throws IOException {
    Gson gson = CGsonHelper.getGson();//from w ww .  jav  a 2  s  .c  o  m

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

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

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

    if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("obj")) {
        throw new IOException();
    }
    ValueHolder vh = gson.fromJson(reader, ValueHolder.class);
    reader.endObject();
    return new CPutObj(Triplet.with(dbId, typeId, objId), vh);
}

From source file:se.sics.datamodel.client.gson.CPutTypeAdapter.java

License:Open Source License

@Override
public CPutType read(JsonReader reader) throws IOException {
    Gson gson = CGsonHelper.getGson();//from  ww w. j  a v a 2s .  c  o  m

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

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

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

    return new CPutType(Pair.with(dbId, typeId), typeInfo);
}

From source file:se.sics.datamodel.client.gson.CQueryObjAdapter.java

License:Open Source License

@Override
public CQueryObj read(JsonReader reader) throws IOException {
    Gson gson = CGsonHelper.getGson();/*from   w  w w  .  ja  va 2 s  . c o m*/

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

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

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

    if (reader.peek() != JsonToken.NAME || !reader.nextName().equals("indexVal")) {
        throw new IOException();
    }
    QueryTypeHolder indexVal = gson.fromJson(reader, QueryTypeHolder.class);
    reader.endObject();
    return new CQueryObj(Pair.with(dbId, typeId), indexId, indexVal);
}