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

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

Introduction

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

Prototype

public double nextDouble() throws IOException 

Source Link

Document

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

Usage

From source file:persistance.JSONHandler.java

private static Male parseDrone(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.j  a 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 Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed,
            secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed);
}

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   ww w .jav a2  s .  com*/
    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;/*w w w.ja  v a  2s.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:ProSettingsGUI.ProSettingsPanel.java

public Objective readObjectiveProperties(JsonReader reader) throws IOException {
    //initialise with some defaults
    String name = "NULL";
    Double magnification = 1.0;//w  w w  .ja  va 2s .  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 readJsnArr(JsonReader reader, String tableName, Class<T> clazz)
        throws IOException, JSONException, Exception {
    JSONObject o = new JSONObject();
    JsonToken peek = reader.peek();/*  ww w .  j ava 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) {
                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:sf.net.experimaestro.manager.json.JsonConverter.java

License:Open Source License

private Json readNext(JsonReader jsonReader) throws IOException {
    final JsonToken token = jsonReader.peek();
    switch (token) {
    case BEGIN_ARRAY: {
        jsonReader.beginArray();//from w w  w . j  a v a 2 s  .c  o m
        JsonArray array = new JsonArray();
        while (jsonReader.peek() != JsonToken.END_ARRAY) {
            array.add(readNext(jsonReader));
        }
        jsonReader.endArray();
        return array;
    }

    case BEGIN_OBJECT: {
        jsonReader.beginObject();
        JsonObject object = new JsonObject();
        while (jsonReader.peek() != JsonToken.END_OBJECT) {
            final String name = jsonReader.nextName();
            final Json value = readNext(jsonReader);
            object.put(name, value);
        }
        jsonReader.endObject();
        return object;
    }

    case BOOLEAN:
        return new JsonBoolean(jsonReader.nextBoolean());

    case STRING:
        return new JsonString(jsonReader.nextString());

    case NULL: {
        jsonReader.nextNull();
        return JsonNull.getSingleton();
    }

    case NUMBER:
        return new JsonReal(jsonReader.nextDouble());

    default:
        throw new RuntimeException("Cannot handle GSON token type: " + token.name());

    }
}

From source file:sf.net.experimaestro.utils.gson.JsonAdapter.java

License:Open Source License

private Json readValue(JsonReader in) throws IOException {
    final JsonToken peek = in.peek();
    switch (peek) {
    case BEGIN_ARRAY:
        in.beginArray();//from ww  w  . j  a v  a 2  s .  c o m
        JsonArray array = new JsonArray();
        while (in.peek() != JsonToken.END_ARRAY) {
            array.add(readValue(in));
        }
        in.endArray();
        return array;

    case BEGIN_OBJECT:
        in.beginObject();
        JsonObject object = new JsonObject();
        while (in.peek() != JsonToken.END_OBJECT) {
            final String name = in.nextName();
            final Json value = readValue(in);
            object.put(name, value);
        }
        in.endObject();
        return object;

    case BOOLEAN:
        return new JsonBoolean(in.nextBoolean());

    case NUMBER:
        final double value = in.nextDouble();
        if (value == Math.round(value))
            return new JsonInteger((long) value);
        return new JsonReal(value);

    case STRING:
        return new JsonString(in.nextString());

    case NULL:
        return JsonNull.getSingleton();

    case NAME:
    case END_OBJECT:
    case END_ARRAY:
    case END_DOCUMENT:
        throw new AssertionError("Not expecting " + peek);
    }
    return null;
}

From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifier.java

License:Apache License

public static NaiveBayesClassifier readJson(JsonReader reader, FeatureExtractionPipeline pipeline)
        throws IOException {
    NaiveBayesClassifier nb = new NaiveBayesClassifier();
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) { // Don't worry; this is okay in Java 7 onwards
        case "labelSmoothing":
            nb.labelSmoothing = reader.nextDouble();
            break;
        case "featureSmoothing":
            nb.featureSmoothing = reader.nextDouble();
            break;
        case "labelMultipliers":
            nb.labelMultipliers = readJsonInt2DoubleMap(reader, pipeline, false);
            break;
        case "labels":
            nb.labels = readJsonIntSet(reader, pipeline, false);
            break;
        case "vocab":
            nb.vocab = readJsonIntSet(reader, pipeline, true);
            break;
        case "docCounts":
            nb.docCounts = readJsonInt2DoubleMap(reader, pipeline, false);
            break;
        case "labelCounts":
            nb.labelCounts = readJsonInt2DoubleMap(reader, pipeline, false);
            break;
        case "jointCounts":
            nb.jointCounts = readJsonInt2ObjectMap(reader, pipeline);
            break;
        case "labelFeatureAlphas":
            nb.labelFeatureAlphas = readJsonInt2ObjectMap(reader, pipeline);
            break;
        case "featureAlphaTotals":
            nb.featureAlphaTotals = readJsonInt2DoubleMap(reader, pipeline, false);
            break;
        case "labelAlphas":
            nb.labelAlphas = readJsonInt2DoubleMap(reader, pipeline, false);
            break;
        case "empiricalLabelPriors":
            nb.empiricalLabelPriors = reader.nextBoolean();
            break;
        }//from   w w w.  j av a 2s.c  om
    }

    return nb;
}

From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifier.java

License:Apache License

protected static Int2DoubleOpenHashMap readJsonInt2DoubleMap(JsonReader reader,
        FeatureExtractionPipeline pipeline, boolean areFeatures) throws IOException {
    Int2DoubleOpenHashMap map = new Int2DoubleOpenHashMap();
    reader.beginObject();// w w w .ja  v  a 2  s  .  c  o  m
    while (reader.hasNext()) {
        map.put(areFeatures ? pipeline.featureIndex(reader.nextName()) : pipeline.labelIndex(reader.nextName()),
                reader.nextDouble());
    }
    reader.endObject();
    return map;
}

From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifierPreComputed.java

License:Apache License

private static Int2DoubleMap readJsonInt2DoubleMap(JsonReader reader, FeatureExtractionPipeline pipeline,
        boolean areFeatures, IntSet vocab) throws IOException {
    Int2DoubleMap map = new Int2DoubleOpenHashMap();
    reader.beginObject();/*from  w ww  . ja v  a2 s  . c o m*/
    while (reader.hasNext()) {
        String name = reader.nextName();
        int index = areFeatures ? pipeline.featureIndex(name) : pipeline.labelIndex(name);
        map.put(index, reader.nextDouble());
        vocab.add(index);
    }
    reader.endObject();
    return map;
}