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

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

Introduction

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

Prototype

JsonToken NUMBER

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

Click Source Link

Document

A JSON number represented in this API by a Java double , long , or int .

Usage

From source file:org.eclipse.skalli.core.rest.JSONRestReader.java

License:Open Source License

@Override
public boolean isValue() throws IOException {
    if (state == STATE_FINAL) {
        return false;
    }//from www  . j a  v  a  2s .  c o m
    if (isKey("value")) { //$NON-NLS-1$
        return true;
    }
    JsonToken next = json.peek();
    return lookAhead == null && (next == JsonToken.STRING || next == JsonToken.NUMBER
            || next == JsonToken.BOOLEAN || next == JsonToken.BEGIN_ARRAY || next == JsonToken.BEGIN_OBJECT
            || next == JsonToken.NULL);
}

From source file:org.eclipse.smarthome.storage.json.PropertiesTypeAdapter.java

License:Open Source License

private Object getValue(JsonReader in) throws IOException {
    Object value = null;/*from   w  ww .j a v a  2  s .co  m*/

    // if the next json token is a number we read it as a BigDecimal,
    // otherwise use the default adapter to read it
    if (JsonToken.NUMBER.equals(in.peek())) {
        String inString = in.nextString();
        if (inString.endsWith(".0")) {
            value = new BigDecimal(inString).setScale(0);
        } else {
            value = new BigDecimal(inString);
        }
    } else {
        value = valueAdapter.read(in);
    }

    return value;

}

From source file:org.eclipse.smarthome.storage.mapdb.internal.PropertiesTypeAdapter.java

License:Open Source License

private Object getValue(JsonReader in) throws IOException {
    Object value = null;//from  w w w . j  a  va 2  s.co m

    // if the next json token is a number we read it as a BigDecimal,
    // otherwise use the default adapter to read it
    if (JsonToken.NUMBER.equals(in.peek())) {
        value = new BigDecimal(in.nextString());
    } else {
        value = valueAdapter.read(in);
    }

    return value;
}

From source file:org.eclipse.smarthome.storage.mapdb.PropertiesTypeAdapter.java

License:Open Source License

private Object getValue(JsonReader in) throws IOException {
    Object value = null;//from   w w  w. ja  v a2  s  .  c  om

    // if the next json token is a number we read it as a BigDecimal,
    // otherwise use the default adapter to read it
    if (JsonToken.NUMBER.equals(in.peek())) {
        value = new BigDecimal(in.nextString());
    } else {
        value = valueAdapter.read(in);
    }

    return value;

}

From source file:org.geotools.data.arcgisrest.GeoJSONParser.java

License:Open Source License

/**
 * Utility methof that parses a Point GeoJSON coordinates array and adds them
 * to coords/*  www .  j av a2  s .com*/
 * 
 * @param coords
 *          List to add coordinates to
 * @throws IOException,
 *           JsonSyntaxException, IllegalStateException
 */
protected void parsePointCoordinates(List<Double> coords)
        throws JsonSyntaxException, IOException, IllegalStateException {

    this.reader.beginArray();

    // Reads the point/vertex coordinates
    while (this.reader.hasNext()) {

        // Read X and Y
        coords.add(this.reader.nextDouble());
        coords.add(this.reader.nextDouble());

        // TODO: for the time being it discards Z
        if (this.reader.peek() == JsonToken.NUMBER) {
            this.reader.skipValue();
        }
    }

    this.reader.endArray();
}

From source file:org.immutables.gson.stream.JsonParserReader.java

License:Apache License

private static JsonToken toGsonToken(com.fasterxml.jackson.core.JsonToken token) {
    switch (token) {
    case START_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case END_ARRAY:
        return JsonToken.END_ARRAY;
    case START_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case END_OBJECT:
        return JsonToken.END_OBJECT;
    case FIELD_NAME:
        return JsonToken.NAME;
    case VALUE_FALSE:
        return JsonToken.BOOLEAN;
    case VALUE_TRUE:
        return JsonToken.BOOLEAN;
    case VALUE_NULL:
        return JsonToken.NULL;
    case VALUE_NUMBER_INT:
        return JsonToken.NUMBER;
    case VALUE_NUMBER_FLOAT:
        return JsonToken.NUMBER;
    case VALUE_STRING:
        return JsonToken.STRING;
    default: // Not semantically equivalent
        return JsonToken.NULL;
    }/*  w  w  w  .  ja  v  a 2 s.  c o m*/
}

From source file:org.immutables.mongo.bson4gson.BsonReader.java

License:Apache License

private static JsonToken toGsonToken(BsonType type) {
    switch (type) {
    case END_OF_DOCUMENT:
        return JsonToken.END_DOCUMENT;
    case DOCUMENT:
        return JsonToken.BEGIN_OBJECT;
    case ARRAY://from w w w .  jav a  2 s. co  m
        return JsonToken.BEGIN_ARRAY;
    case BOOLEAN:
        return JsonToken.BOOLEAN;
    case STRING:
    case SYMBOL:
    case OBJECT_ID:
    case BINARY:
    case REGULAR_EXPRESSION:
        return JsonToken.STRING;
    case DATE_TIME:
    case DOUBLE:
    case INT32:
    case INT64:
    case TIMESTAMP:
    case DECIMAL128:
        return JsonToken.NUMBER;
    case NULL:
        return JsonToken.NULL;
    default:
        // not really sure what to do with this type
        return JsonToken.NULL;
    }
}

From source file:org.jetbrains.io.JsonReaderEx.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *///from w  ww  .  j a v  a  2  s. c  o  m
public JsonToken peek() {
    int p = peeked;
    if (p == PEEKED_NONE) {
        p = doPeek();
    }

    switch (p) {
    case PEEKED_BEGIN_OBJECT:
        return JsonToken.BEGIN_OBJECT;
    case PEEKED_END_OBJECT:
        return JsonToken.END_OBJECT;
    case PEEKED_BEGIN_ARRAY:
        return JsonToken.BEGIN_ARRAY;
    case PEEKED_END_ARRAY:
        return JsonToken.END_ARRAY;
    case PEEKED_SINGLE_QUOTED_NAME:
    case PEEKED_DOUBLE_QUOTED_NAME:
    case PEEKED_UNQUOTED_NAME:
        return JsonToken.NAME;
    case PEEKED_TRUE:
    case PEEKED_FALSE:
        return JsonToken.BOOLEAN;
    case PEEKED_NULL:
        return JsonToken.NULL;
    case PEEKED_SINGLE_QUOTED:
    case PEEKED_DOUBLE_QUOTED:
    case PEEKED_UNQUOTED:
    case PEEKED_BUFFERED:
        return JsonToken.STRING;
    case PEEKED_LONG:
    case PEEKED_NUMBER:
        return JsonToken.NUMBER;
    case PEEKED_EOF:
        return JsonToken.END_DOCUMENT;
    default:
        throw new AssertionError();
    }
}

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  w w w.  j a v a 2s  . 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 readJsnArr(JsonReader reader, String tableName, Class<T> clazz)
        throws IOException, JSONException, Exception {
    JSONObject o = new JSONObject();
    JsonToken peek = reader.peek();// w w  w  .ja v a  2 s.  co m
    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;
}