Example usage for android.util JsonToken BEGIN_OBJECT

List of usage examples for android.util JsonToken BEGIN_OBJECT

Introduction

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

Prototype

JsonToken BEGIN_OBJECT

To view the source code for android.util JsonToken BEGIN_OBJECT.

Click Source Link

Document

The opening of a JSON object.

Usage

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

/**
 * Parse the next value as an object, but do not attempt to convert it or any children to a
 * known type. The returned object will be a {@link JSONObject} and all children will be
 * JSONObjects, JSONArrays, and primitives.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.//  w w w.j ava 2  s. co  m
 */
public static JSONObject parseAsJsonObject(JsonReader reader, String key) throws IOException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_OBJECT);
    JSONObject result = new JSONObject();
    reader.beginObject();
    while (reader.hasNext()) {
        try {
            result.put(reader.nextName(), parseNextValue(reader, false));
        } catch (JSONException e) {
            throw new RuntimeException("This should be impossible.", e);
        }
    }
    reader.endObject();
    return result;
}

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

/**
 * Parse the next value as a {@link Map}. Children will be converted to a known type. In
 * general, this method does not handle {@link Set}s as children.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param map The Map to populate.// w  ww .  java 2 s.co  m
 * @param valueClass The type of the Map value, corresponding to V in Map{@literal<}K,
 * V{@literal>}.
 * @param parser The parser to use, or null if this method should find an appropriate one on its
 * own.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 * @param <T> The value type of the Map, corresponding to V in Map{@literal<}K, V{@literal>}.
 */
public static <T> void parseAsMap(JsonReader reader, Map<String, T> map, Class<T> valueClass,
        JsonObjectParser<T> parser, String key) throws IOException {
    if (handleNull(reader)) {
        return;
    }

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    assertType(reader, key, JsonToken.BEGIN_OBJECT);
    reader.beginObject();
    while (reader.hasNext()) {
        T value;
        String name = reader.nextName();
        if (parser != null) {
            reader.beginObject();
            value = parser.parseJsonObject(null, reader, discriminationName, null);
            reader.endObject();
        } else {
            Object o = parseNextValue(reader, true);
            if (!valueClass.isInstance(o)) {
                throwMapException(name, key, valueClass, o);
            }
            value = cast(o);

        }
        map.put(name, value);
    }
    reader.endObject();
}

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

/**
 * Parse the next value as an object. If the next value is {@link JsonToken#NULL}, returns
 * null.//from   ww w  . j  a  v a  2  s .c  o  m
 * <p/>
 * This method will use the provide parser, or if none is provided, will attempt find an
 * appropriate parser based on the discrimination value found in the next object. If none is
 * found, then this method returns a {@link JSONObject}.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param parser The parser to use, or null if this method should find an appropriate one on its
 * own.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 * @param expectedType The expected class of the resulting object. If the result is not an
 * instance of this class, an exception is thrown.
 *
 * @throws IllegalStateException if the resulting object is not an instance of {@code
 * expectedType}.
 */
public static Object parseJsonObject(JsonReader reader, JsonObjectParser<?> parser, String key,
        Class<?> expectedType) throws IOException, IllegalStateException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_OBJECT);

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    String discriminationValue = null;
    Object result = null;
    reader.beginObject();
    if (parser != null) {
        result = parser.parseJsonObject(null, reader, discriminationName, null);
    } else if (reader.hasNext()) {
        String firstName = reader.nextName();
        final String discriminationKeyName = ContextHolder.getContext().getSettings().getDiscriminationName();
        if (discriminationKeyName.equals(firstName)) {
            discriminationValue = reader.nextString();
            parser = ContextHolder.getContext().getJsonObjectParserTable().get(discriminationValue);
            if (parser != null) {
                result = parser.parseJsonObject(null, reader, discriminationName, discriminationValue);
            } else {
                result = parseSpecificJsonObjectDelayed(reader, discriminationKeyName, discriminationValue);
            }
        } else {
            result = parseSpecificJsonObjectDelayed(reader, firstName, null);
        }

    }
    reader.endObject();

    if (result == null) {
        result = new JSONObject();
    }

    JsonObjectParser<?> unknownObjectParser = ContextHolder.getContext().getSettings().getUnknownObjectParser();
    if (result instanceof JSONObject && unknownObjectParser != null) {
        result = unknownObjectParser.parseJsonObject((JSONObject) result, null, discriminationName,
                discriminationValue);
    }

    if (expectedType != null && !(expectedType.isInstance(result))) {
        throw new IllegalStateException(
                String.format(Locale.US, "Could not convert value at \"%s\" to %s from %s.", key,
                        expectedType.getCanonicalName(), result.getClass().getCanonicalName()));
    }
    return result;
}

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}./*from   w  ww.j ava2  s  . co  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();
}