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

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

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

From source file:persistance.JSONHandler.java

private static Female parsePrincess(JsonReader jsonReader) throws IOException {
    int primaryFertility = 0;
    double primaryLifespan = 0;
    double primaryPollination = 0;
    String primarySpecies = null;
    double primaryWorkspeed = 0;
    int secondaryFertility = 0;
    double secondaryLifespan = 0;
    double secondaryPollination = 0;
    String secondarySpecies = null;
    double secondaryWorkspeed = 0;

    jsonReader.beginObject();//from   w w  w.  ja va2s .c  o m
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
        case "primaryFertility":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryFertility = jsonReader.nextInt();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primaryLifespan":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryLifespan = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primaryPollination":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryPollination = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primarySpecies":
            primarySpecies = jsonReader.nextString();
            break;
        case "primaryWorkspeed":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryWorkspeed = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryFertility":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryFertility = jsonReader.nextInt();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryLifespan":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryLifespan = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryPollination":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryPollination = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondarySpecies":
            secondarySpecies = jsonReader.nextString();
            break;
        case "secondaryWorkspeed":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryWorkspeed = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        default:
            jsonReader.skipValue();
            break;
        }
    }
    jsonReader.endObject();

    return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed,
            secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed);
}

From source file:persistance.JSONHandler.java

private static Queen parseQueen(JsonReader jsonReader) throws IOException {
    int primaryFertility = 0;
    double primaryLifespan = 0;
    double primaryPollination = 0;
    String primarySpecies = null;
    double primaryWorkspeed = 0;
    int secondaryFertility = 0;
    double secondaryLifespan = 0;
    double secondaryPollination = 0;
    String secondarySpecies = null;
    double secondaryWorkspeed = 0;
    Female princess = null;//from ww  w  .  j  a v a 2  s.c  o  m
    Male drone = null;

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
        case "drone":
            drone = parseDrone(jsonReader);
            break;
        case "princess":
            princess = parsePrincess(jsonReader);
            break;
        case "primaryFertility":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryFertility = jsonReader.nextInt();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primaryLifespan":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryLifespan = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primaryPollination":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryPollination = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "primarySpecies":
            primarySpecies = jsonReader.nextString();
            break;
        case "primaryWorkspeed":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    primaryWorkspeed = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryFertility":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryFertility = jsonReader.nextInt();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryLifespan":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryLifespan = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondaryPollination":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryPollination = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        case "secondarySpecies":
            secondarySpecies = jsonReader.nextString();
            break;
        case "secondaryWorkspeed":
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                switch (jsonReader.nextName()) {
                case "modifier":
                    secondaryWorkspeed = jsonReader.nextDouble();
                    break;
                default:
                    jsonReader.skipValue();
                    break;
                }
            }
            jsonReader.endObject();
            break;
        default:
            jsonReader.skipValue();
            break;
        }
    }
    jsonReader.endObject();

    Queen newQueen = new Queen(primaryFertility, primaryLifespan, primaryPollination, primarySpecies,
            primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies,
            secondaryWorkspeed);
    newQueen.setPrincess(princess);
    newQueen.setDrone(drone);

    return newQueen;
}

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  ww  .  j a va 2  s  .  co  m*/
    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();/*from w w  w .j  a v a 2 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  w w w  .  j ava 2  s. co 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 {/*  w w w .  j a v  a  2s  . c om*/
        // 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  ww . j a  v a 2s  .c  o m

    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");
    }/*from  ww w  .  j a v  a  2  s  .c  o  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;/*ww  w  . j  a va  2 s  .  c  o  m*/
    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;/*from   ww w.j  av a 2s.  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;
}