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:com.github.gv2011.hprops.Converter.java

License:Open Source License

private void dispatch(final JsonReader json, final Consumer<Property> target, final JsonToken t,
        final String key) {
    if (t.equals(JsonToken.BOOLEAN))
        write(target, key, getBoolean(json));
    else if (t.equals(JsonToken.STRING))
        write(target, key, getString(json));
    else if (t.equals(JsonToken.NUMBER))
        write(target, key, getNumber(json));
    else if (t.equals(JsonToken.BEGIN_OBJECT))
        doObject(json, target, key);/*  w w w  . j av  a2s. c  o  m*/
    else if (t.equals(JsonToken.BEGIN_ARRAY))
        doList(json, target, key);
    else if (t.equals(JsonToken.NULL))
        getNull(json);
    else
        throw new IllegalArgumentException(t.toString());
}

From source file:com.goide.dlv.JsonReaderEx.java

License:Apache License

/**
 * Returns the type of the next token without consuming it.
 *//*from   w ww  .j a  v a 2s.co m*/
@NotNull
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:com.google.maps.internal.DayOfWeekAdapter.java

License:Open Source License

@Override
public DayOfWeek read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from   ww w. j  a v a 2 s. c o  m*/
        return null;
    }

    if (reader.peek() == JsonToken.NUMBER) {
        int day = reader.nextInt();

        switch (day) {
        case 0:
            return DayOfWeek.SUNDAY;
        case 1:
            return DayOfWeek.MONDAY;
        case 2:
            return DayOfWeek.TUESDAY;
        case 3:
            return DayOfWeek.WEDNESDAY;
        case 4:
            return DayOfWeek.THURSDAY;
        case 5:
            return DayOfWeek.FRIDAY;
        case 6:
            return DayOfWeek.SATURDAY;
        }
    }

    return DayOfWeek.UNKNOWN;
}

From source file:com.google.maps.internal.InstantAdapter.java

License:Open Source License

/**
 * Read a time from the Places API and convert to a {@link Instant}
 *///www .j  a  v a2  s.c  o m
@Override
public Instant read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }

    if (reader.peek() == JsonToken.NUMBER) {
        // Number is the number of seconds since Epoch.
        return new Instant(reader.nextLong() * 1000L);
    }

    throw new UnsupportedOperationException("Unsupported format");
}

From source file:com.google.maps.internal.PriceLevelAdapter.java

License:Open Source License

@Override
public PriceLevel read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//w  w w.ja v  a2s . c  om
        return null;
    }

    if (reader.peek() == JsonToken.NUMBER) {
        int priceLevel = reader.nextInt();

        switch (priceLevel) {
        case 0:
            return PriceLevel.FREE;
        case 1:
            return PriceLevel.INEXPENSIVE;
        case 2:
            return PriceLevel.MODERATE;
        case 3:
            return PriceLevel.EXPENSIVE;
        case 4:
            return PriceLevel.VERY_EXPENSIVE;
        }
    }

    return PriceLevel.UNKNOWN;
}

From source file:com.gs.reladomo.serial.gson.GsonWrappedListTypedAdapter.java

License:Apache License

@Override
public SerializedList<U, T> read(JsonReader jsonReader) throws IOException {
    ReladomoDeserializer deserializer;//from   w w w  .  jav  a  2 s.co m
    if (this.typeClass == null) {
        deserializer = new ReladomoDeserializer();
    } else {
        deserializer = new ReladomoDeserializer(typeClass);
    }
    deserializer.setIgnoreUnknown();
    JsonDeserializerState state = JsonDeserializerState.ListStartState.INSTANCE;
    while (true) {
        JsonToken nextToken = jsonReader.peek();
        //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT;
        if (JsonToken.BEGIN_OBJECT == nextToken) {
            jsonReader.beginObject();
            state = state.startObject(deserializer);
        } else if (JsonToken.END_OBJECT == nextToken) {
            jsonReader.endObject();
            state = state.endObject(deserializer);
        } else if (JsonToken.BEGIN_ARRAY == nextToken) {
            jsonReader.beginArray();
            state = state.startArray(deserializer);
        } else if (JsonToken.END_ARRAY == nextToken) {
            jsonReader.endArray();
            state = state.endArray(deserializer);
        } else if (JsonToken.BOOLEAN == nextToken) {
            if (jsonReader.nextBoolean()) {
                state = state.valueTrue(deserializer);
            } else {
                state = state.valueFalse(deserializer);
            }
        } else if (JsonToken.NAME == nextToken) {
            String name = jsonReader.nextName();
            state = state.fieldName(name, deserializer);
        } else if (JsonToken.NUMBER == nextToken) {
            String value = jsonReader.nextString();
            state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss
        } else if (JsonToken.STRING == nextToken) {
            String value = jsonReader.nextString();
            Attribute attribute = deserializer.getCurrentAttribute();
            if (attribute instanceof TimestampAttribute) {
                Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value);
                state = state.valueTimestamp(timestamp, deserializer);
            } else {
                state = state.valueString(value, deserializer);
            }
        } else if (JsonToken.NULL == nextToken) {
            jsonReader.nextNull();
            state = state.valueNull(deserializer);
        } else if (JsonToken.END_DOCUMENT == nextToken) {
            break;
        }
    }
    return deserializer.getDeserializedResultAsList();
}

From source file:com.gs.reladomo.serial.gson.GsonWrappedTypedAdapter.java

License:Apache License

@Override
public Serialized<T> read(JsonReader jsonReader) throws IOException {
    ReladomoDeserializer deserializer;/*from w  ww  . ja  v a 2  s. c  o  m*/
    if (this.typeClass == null) {
        deserializer = new ReladomoDeserializer();
    } else {
        deserializer = new ReladomoDeserializer(typeClass);
    }
    deserializer.setIgnoreUnknown();
    JsonDeserializerState state = JsonDeserializerState.NormalParserState.INSTANCE;
    while (true) {
        JsonToken nextToken = jsonReader.peek();
        //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT;
        if (JsonToken.BEGIN_OBJECT == nextToken) {
            jsonReader.beginObject();
            state = state.startObject(deserializer);
        } else if (JsonToken.END_OBJECT == nextToken) {
            jsonReader.endObject();
            state = state.endObject(deserializer);
        } else if (JsonToken.BEGIN_ARRAY == nextToken) {
            jsonReader.beginArray();
            state = state.startArray(deserializer);
        } else if (JsonToken.END_ARRAY == nextToken) {
            jsonReader.endArray();
            state = state.endArray(deserializer);
        } else if (JsonToken.BOOLEAN == nextToken) {
            if (jsonReader.nextBoolean()) {
                state = state.valueTrue(deserializer);
            } else {
                state = state.valueFalse(deserializer);
            }
        } else if (JsonToken.NAME == nextToken) {
            String name = jsonReader.nextName();
            state = state.fieldName(name, deserializer);
        } else if (JsonToken.NUMBER == nextToken) {
            String value = jsonReader.nextString();
            state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss
        } else if (JsonToken.STRING == nextToken) {
            String value = jsonReader.nextString();
            Attribute attribute = deserializer.getCurrentAttribute();
            if (attribute instanceof TimestampAttribute) {
                Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value);
                state = state.valueTimestamp(timestamp, deserializer);
            } else {
                state = state.valueString(value, deserializer);
            }
        } else if (JsonToken.NULL == nextToken) {
            jsonReader.nextNull();
            state = state.valueNull(deserializer);
        } else if (JsonToken.END_DOCUMENT == nextToken) {
            break;
        }
    }
    return deserializer.getDeserializedResult();
}

From source file:com.ibm.og.json.type.FilesizeConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!FilesizeConfig.class.equals(rawType)) {
        return null;
    }//w  w  w. j a  va  2 s. c om

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.NUMBER == in.peek()) {
                return (T) new FilesizeConfig(in.nextDouble());
            }

            return delegate.read(in);
        }

    }.nullSafe();
}

From source file:com.ibm.og.json.type.OperationConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!OperationConfig.class.equals(rawType)) {
        return null;
    }// w  w w . j  a  va 2s .  c om

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.NUMBER == in.peek()) {
                return (T) new OperationConfig(in.nextDouble());
            }

            return delegate.read(in);
        }
    }.nullSafe();
}

From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechTimestampTypeAdapter.java

License:Open Source License

@Override
public SpeechTimestamp read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from   w  w  w .  jav  a2 s.co  m*/
        return null;
    }

    final SpeechTimestamp speechTimestamp = new SpeechTimestamp();
    reader.beginArray();

    if (reader.peek() == JsonToken.STRING) {
        speechTimestamp.setWord(reader.nextString());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechTimestamp.setStartTime(reader.nextDouble());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechTimestamp.setEndTime(reader.nextDouble());
    }

    reader.endArray();
    return speechTimestamp;
}