Example usage for com.google.gson.stream JsonReader nextString

List of usage examples for com.google.gson.stream JsonReader nextString

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextString.

Prototype

public String nextString() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#STRING string value of the next token, consuming it.

Usage

From source file:com.dracade.ember.core.adapters.ClassAdapter.java

License:Open Source License

@Override
public Class read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        return null;
    }//from www. j  av  a2 s. c  o m
    in.beginObject();
    in.nextName();
    in.nextString();
    in.nextName();

    Class c = null;
    try {
        c = Class.forName(in.nextString());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    in.endObject();

    return c;
}

From source file:com.dracade.ember.core.adapters.WorldAdapter.java

License:Open Source License

@Override
public World read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        return null;
    }/*from ww  w  . j av  a2  s . com*/

    in.beginObject();
    in.nextName();
    in.nextString();
    in.nextName();

    Optional<World> optional = Ember.game().getServer().getWorld(UUID.fromString(in.nextString()));

    in.endObject();

    return optional.isPresent() ? optional.get() : null;
}

From source file:com.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java

License:Apache License

@Override
public MailChimpObject read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//w  w w  .  java  2s  .  co m
        return null;
    }

    MailChimpObject result;
    try {
        result = constructor.newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Failed to invoke " + constructor + " with no args", e);
    }

    in.beginObject();
    while (in.hasNext()) {
        final String key;
        if (in.peek() == JsonToken.NAME) {
            key = in.nextName();
        } else {
            key = in.nextString();
        }

        final Object value;

        Type valueType = result.getReflectiveMappingTypes().get(key);
        if (valueType != null) {
            value = gson.getAdapter(TypeToken.get(valueType)).read(in);
        } else {
            if (in.peek() == JsonToken.BEGIN_OBJECT) {
                value = gson.getAdapter(MailChimpObject.class).read(in);
            } else if (in.peek() == JsonToken.BEGIN_ARRAY) {
                value = readList(in);
            } else {
                value = gson.getAdapter(Object.class).read(in);
            }
        }

        if (result.put(key, value) != null) {
            throw new JsonSyntaxException("duplicate key: " + key);
        }
    }
    in.endObject();

    return result;
}

From source file:com.epickrram.romero.testing.server.web.CompletedJobRunIdentifierTypeAdapter.java

License:Apache License

@Override
public CompletedJobRunIdentifier read(final JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();//from  w ww . j ava 2s.co m
    final CompletedJobRunIdentifier.Builder builder = new CompletedJobRunIdentifier.Builder();
    while (jsonReader.hasNext()) {
        final String name = jsonReader.nextName();
        if ("jobRunIdentifier".equals(name)) {
            builder.jobRunIdentifier(jsonReader.nextString());
        } else if ("startTimestamp".equals(name)) {
            builder.startTimestamp(jsonReader.nextLong());
        }
    }
    jsonReader.endObject();
    return builder.create();
}

From source file:com.facebook.buck.worker.WorkerProcessProtocolZero.java

License:Apache License

private static void receiveHandshake(JsonReader reader, int messageId, Optional<Path> stdErr)
        throws IOException {
    int id = -1;/*from   w w  w.j  av  a  2  s .co  m*/
    String type = "";
    String protocolVersion = "";

    try {
        reader.beginArray();
        reader.beginObject();
        while (reader.hasNext()) {
            String property = reader.nextName();
            if (property.equals("id")) {
                id = reader.nextInt();
            } else if (property.equals("type")) {
                type = reader.nextString();
            } else if (property.equals("protocol_version")) {
                protocolVersion = reader.nextString();
            } else if (property.equals("capabilities")) {
                try {
                    reader.beginArray();
                    reader.endArray();
                } catch (IllegalStateException e) {
                    throw new HumanReadableException(
                            "Expected handshake response's \"capabilities\" to " + "be an empty array.");
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    } catch (IOException e) {
        throw new HumanReadableException(e, "Error receiving handshake response from external process.\n"
                + "Stderr from external process:\n%s", getStdErrorOutput(stdErr));
    }

    if (id != messageId) {
        throw new HumanReadableException(String.format(
                "Expected handshake response's \"id\" value " + "to be \"%d\", got \"%d\" instead.", messageId,
                id));
    }
    if (!type.equals(TYPE_HANDSHAKE)) {
        throw new HumanReadableException(
                String.format("Expected handshake response's \"type\" " + "to be \"%s\", got \"%s\" instead.",
                        TYPE_HANDSHAKE, type));
    }
    if (!protocolVersion.equals(PROTOCOL_VERSION)) {
        throw new HumanReadableException(String.format(
                "Expected handshake response's " + "\"protocol_version\" to be \"%s\", got \"%s\" instead.",
                PROTOCOL_VERSION, protocolVersion));
    }
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public static <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts)
        throws IOException, JSONException, Exception {
    String n = null;/*from  w ww . java  2s. co 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();
        } else if (peek == JsonToken.STRING) {
            reader.skipValue();
        } else {
            reader.skipValue();
        }
    }
    return i;
}

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();//from  ww w  . ja  v a 2  s . 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) {
                //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.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public static void readDelete(JsonReader reader) throws IOException {
    reader.nextName();//from  w  w  w. j ava  2s  . c om
    reader.beginArray();
    while (reader.hasNext()) {
        reader.beginObject();
        reader.nextName(); //tablename
        String t = reader.nextString();
        reader.nextName(); //key
        execDelete(t, reader.nextString());
        reader.endObject();
    }
    reader.endArray();
}

From source file:com.flowzr.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public static void readDelete(JsonReader reader) throws IOException {
    reader.nextName();/*ww  w.  j  av a 2  s  . com*/
    reader.beginArray();
    while (reader.hasNext()) {
        reader.beginObject();
        reader.nextName(); //tablename
        String t = reader.nextString();
        reader.nextName(); //key                
        execDelete(t, reader.nextString());
        reader.endObject();
    }
    reader.endArray();
}

From source file:com.funambol.android.activities.AndroidHomeScreen.java

License:Open Source License

/**
 * json//from w w w  .j  av  a 2s . c om
 * @param in
 * @throws IOException
 */
public void processJsonStream(InputStream in) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginObject();
    boolean result = false;
    int contactsCount = 0; //
    int smsCount = 0; //
    long smsLastBakTime = 0; //

    while (reader.hasNext()) {
        String name = reader.nextName();
        if ("result".equals(name)) {
            result = "ok".equals(reader.nextString());
        } else if ("contacts_count".equals(name)) {
            contactsCount = Integer.valueOf(reader.nextString());
        } else if ("sms_count".equals(name)) {
            smsCount = Integer.valueOf(reader.nextString());
        } else if ("sms_last_baktime".equals(name)) {
            try {
                smsLastBakTime = Long.valueOf(reader.nextString());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        } else {
            reader.skipValue();
        }
    }

    reader.endObject();

    if (result) {
        //prefStore
        prefStore.setSmsCountInServer(this, smsCount);
        prefStore.setContactsCountInServer(this, contactsCount);

        //
        overviewInfoHandler.sendEmptyMessage(MSG_REFRESH_OVERVIEW_INFO);

        //0
        if (0 == contactsCount) {
            contactsImportHandler.sendEmptyMessage(0);
            Log.debug("shawnqiu", "homeScreen : processJsonStream : showContactsImporter");
        }
    }

}