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

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

Introduction

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

Prototype

public void endObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current object.

Usage

From source file:persistance.JSONHandler.java

private static Colony parseColony(JsonReader jsonReader, ArrayList<Colony> colonies) throws IOException {

    int year = 0;
    Queen queen = null;//from w w  w . ja v  a  2  s. c om
    ArrayList<Bee> bees = new ArrayList<>();
    Colony parentColony = null;

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
        case "year":
            year = jsonReader.nextInt();
            break;
        case "queen":
            queen = parseQueen(jsonReader);
            break;
        case "parentColony":
            parentColony = parseColony(jsonReader, colonies);
            for (Colony colony : colonies) {
                if (colony.equals(parentColony)) {
                    parentColony = colony;
                }
            }

            break;
        case "bees":
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                bees.add(parseUnspecifiedBee(jsonReader));
            }
            jsonReader.endArray();
            break;
        default:
            jsonReader.skipValue();
            break;
        }
    }
    jsonReader.endObject();

    Colony colony = new Colony(year, queen, bees);

    if (parentColony != null) {
        colony.setParentColony(parentColony);
        parentColony.addChildColony(colony);
    }

    return colony;
}

From source file:pl.nask.hsn2.task.SignatureProcessor.java

License:Open Source License

public final void process(InputStream stream) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(stream));
    reader.beginObject();//ww  w  .  jav  a2  s . c  om
    while (reader.hasNext()) {
        String name = reader.nextName();
        if ("signatures".equals(name)) {
            reader.beginArray();
            while (reader.hasNext()) {
                Signature<Map<String, Object>> signature = new Gson().fromJson(reader, Signature.class);
                extractData(signature);
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
}

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 www.ja va2  s  .c o m*/
        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:plugins.commands.FactCommand.java

@Override
public String handleMessage(Channel channel, User user, List<String> arguments) {
    if (arguments.size() > 2)
        return Command.wrapErrorMessage("Usage: !fact or !fact <id>");
    else {/*from w  w w .  j a v a  2 s. c  o m*/
        // Base message
        String msg = Command.wrapErrorMessage("Sorry, your fact could not be retrieved.");
        try {
            // Prepare the url
            URL url = new URL(FACT_SOURCE);
            if (arguments.size() == 2) {
                try {
                    int id = Integer.parseInt(arguments.get(1));
                    url = new URL(FACT_SOURCE + "?id=" + id);
                } catch (NumberFormatException e) {
                }
            }

            // Fetch the url
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(((HttpURLConnection) url.openConnection()).getInputStream()));

            // Creative JSON reading
            JsonReader reader = new JsonReader(in);
            reader.beginObject();
            while (reader.hasNext()) {
                if (reader.nextName().equals("post_content")) {
                    msg = reader.nextString();
                    // Yay, this is ugly. But it works.
                    msg = msg.replace("<em>", "");
                    msg = msg.replace("</em>", "");
                    msg = msg.replace("<strong>", Colors.BOLD);
                    msg = msg.replace("</strong>", Colors.NORMAL);
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();

        } catch (IOException | IllegalStateException e) {
        }

        return msg;

    }
}

From source file:project.latex.balloon.BalloonController.java

static List<String> loadTransmittedDataKeys(String filePath) throws IOException {
    if (filePath == null) {
        throw new IllegalArgumentException("Cannot load keys from null file");
    }/*w  w  w.j  av  a2s  .  com*/

    JsonReader reader = null;
    try {
        List<String> dataKeys = new ArrayList<>();
        reader = new JsonReader(new FileReader(filePath));
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            reader.beginArray();
            while (reader.hasNext()) {
                dataKeys.add(reader.nextString());
            }
            reader.endArray();
        }
        reader.endObject();
        reader.close();

        return dataKeys;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:project.latex.balloon.TransmittedDataKeysResource.java

final void loadTransmittedDataKeys(String filePath) throws IOException {
    if (filePath == null) {
        throw new IllegalArgumentException("Cannot load keys from null file");
    }//  ww  w  . j ava 2  s.co  m

    JsonReader reader = null;
    try {
        List<String> dataKeys = new ArrayList<>();
        reader = new JsonReader(new FileReader(filePath));
        reader.beginObject();
        while (reader.hasNext()) {
            reader.nextName();
            reader.beginArray();
            while (reader.hasNext()) {
                dataKeys.add(reader.nextString());
            }
            reader.endArray();
        }
        reader.endObject();
        reader.close();

        this.transmittedDataKeys = dataKeys;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:ProSettingsGUI.ProSettingsPanel.java

public Objective readObjectiveProperties(JsonReader reader) throws IOException {
    //initialise with some defaults
    String name = "NULL";
    Double magnification = 1.0;/*w ww .j a  v a 2s  .  c om*/
    Double Xoffset = 0.0;
    Double Yoffset = 0.0;
    Double Zoffset = 0.0;

    reader.beginObject();
    while (reader.hasNext()) {
        String itemname = reader.nextName();
        if (itemname.equals("name")) {
            name = reader.nextString();
        } else if (itemname.equals("magnification")) {
            magnification = reader.nextDouble();
        } else if (itemname.equals("Xoffset")) {
            Xoffset = reader.nextDouble();
        } else if (itemname.equals("Yoffset")) {
            Yoffset = reader.nextDouble();
        } else if (itemname.equals("Zoffset")) {
            Zoffset = reader.nextDouble();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return new Objective(name, magnification, Xoffset, Yoffset, Zoffset);
}

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 w  w . ja  v a2s .  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();//from www .  jav  a 2 s . com
    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:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java

License:Open Source License

public void readDelete(JsonReader reader) throws IOException {
    reader.nextName();//from   w w  w .  ja  v a2  s. c  o  m
    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();
}