Example usage for android.util JsonToken NUMBER

List of usage examples for android.util JsonToken NUMBER

Introduction

In this page you can find the example usage for android.util JsonToken NUMBER.

Prototype

JsonToken NUMBER

To view the source code for android.util 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.workday.autoparse.json.parser.JsonParserUtils.java

/**
 * Parse an array that has only non-array children into a {@link Collection}.
 *
 * @param reader The reader to use, whose next token should either be {@link JsonToken#NULL} or
 * {@link JsonToken#BEGIN_ARRAY}.//www  . j a  v a 2  s . c  o m
 * @param collection The Collection to populate. The parametrization should match {@code
 * typeClass}.
 * @param itemParser The parser to use for items of the array. May be null.
 * @param typeClass The type of items to expect in the array. May not be null, but may be
 * Object.class.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 */
private static <T> void parseFlatJsonArray(JsonReader reader, Collection<T> collection,
        JsonObjectParser<T> itemParser, Class<T> typeClass, String key) throws IOException {
    if (handleNull(reader)) {
        return;
    }

    Converter<T> converter = null;
    if (Converters.isConvertibleFromString(typeClass)) {
        converter = Converters.getConverter(typeClass);
    }

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();

    reader.beginArray();
    while (reader.hasNext()) {
        Object nextValue;
        final JsonToken nextToken = reader.peek();
        if (itemParser != null && nextToken == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
            nextValue = itemParser.parseJsonObject(null, reader, discriminationName, null);
            reader.endObject();
        } else if (converter != null && (nextToken == JsonToken.NUMBER || nextToken == JsonToken.STRING)) {
            nextValue = converter.convert(reader.nextString());
        } else {
            nextValue = parseNextValue(reader);
        }

        if (typeClass.isInstance(nextValue)) {
            // This is safe since we are calling class.isInstance()
            @SuppressWarnings("unchecked")
            T toAdd = (T) nextValue;
            collection.add(toAdd);
        } else {
            throw new IllegalStateException(
                    String.format(Locale.US, "Could not convert value in array at \"%s\" to %s from %s.", key,
                            typeClass.getCanonicalName(), getClassName(nextValue)));
        }
    }
    reader.endArray();
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static BigDecimal nextBigDecimal(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return BigDecimal.ZERO;
    }//w w w  . j a v a  2  s. co  m
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return new BigDecimal(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static BigInteger nextBigInteger(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return BigInteger.ZERO;
    }/*from   w  ww  .j  ava 2  s.c o  m*/
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return new BigInteger(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static Byte nextByte(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return 0;
    }//from  w  w  w  . j  a  va  2 s. co  m
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return Byte.valueOf(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static Double nextDouble(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return 0d;
    }//from  ww w.j  av a 2 s.c om
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return Double.valueOf(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static Float nextFloat(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return 0f;
    }//from  w ww  .  j a v a 2s. c  o m
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return Float.valueOf(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static Integer nextInt(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return 0;
    }//from  w  ww. ja va  2s  . com
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return Integer.valueOf(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static Long nextLong(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return 0L;
    }/*from   w  w w.  j ava2s . c o m*/
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return Long.valueOf(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static Short nextShort(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return 0;
    }//from  w  w w.  j av  a2s .c o m
    assertType(reader, name, JsonToken.NUMBER, JsonToken.STRING);
    return Short.valueOf(reader.nextString());
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

public static String nextString(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return null;
    }//from   w w w  .j av a  2 s.  c  o m
    assertType(reader, name, JsonToken.STRING, JsonToken.NUMBER, JsonToken.BOOLEAN);

    if (reader.peek() == JsonToken.BOOLEAN) {
        return String.valueOf(reader.nextBoolean());
    }

    return reader.nextString();
}