Example usage for android.util JsonReader nextNull

List of usage examples for android.util JsonReader nextNull

Introduction

In this page you can find the example usage for android.util JsonReader nextNull.

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

Usage

From source file:android.support.test.espresso.web.model.ModelCodec.java

private static List<Object> decodeArray(JsonReader reader) throws IOException {
    List<Object> array = Lists.newArrayList();
    reader.beginArray();//from ww  w .  ja  v  a  2s. c  o m
    while (reader.hasNext()) {
        switch (reader.peek()) {
        case BEGIN_OBJECT:
            array.add(decodeObject(reader));
            break;
        case NULL:
            reader.nextNull();
            array.add(null);
            break;
        case STRING:
            array.add(reader.nextString());
            break;
        case BOOLEAN:
            array.add(reader.nextBoolean());
            break;
        case BEGIN_ARRAY:
            array.add(decodeArray(reader));
            break;
        case NUMBER:
            array.add(decodeNumber(reader.nextString()));
            break;
        default:
            throw new IllegalStateException(String.format("%s: bogus token", reader.peek()));
        }
    }

    reader.endArray();
    return array;
}

From source file:android.support.test.espresso.web.model.ModelCodec.java

private static Object decodeObject(JsonReader reader) throws IOException {
    Map<String, Object> obj = Maps.newHashMap();
    List<String> nullKeys = Lists.newArrayList();
    reader.beginObject();//ww w.j  a v a  2 s .c o  m
    while (reader.hasNext()) {
        String key = reader.nextName();
        Object value = null;
        switch (reader.peek()) {
        case BEGIN_OBJECT:
            obj.put(key, decodeObject(reader));
            break;
        case NULL:
            reader.nextNull();
            nullKeys.add(key);
            obj.put(key, JSONObject.NULL);
            break;
        case STRING:
            obj.put(key, reader.nextString());
            break;
        case BOOLEAN:
            obj.put(key, reader.nextBoolean());
            break;
        case NUMBER:
            obj.put(key, decodeNumber(reader.nextString()));
            break;
        case BEGIN_ARRAY:
            obj.put(key, decodeArray(reader));
            break;
        default:
            throw new IllegalStateException(String.format("%: bogus token.", reader.peek()));
        }
    }
    reader.endObject();
    Object replacement = maybeReplaceMap(obj);
    if (null != replacement) {
        return replacement;
    } else {
        for (String key : nullKeys) {
            obj.remove(key);
        }
    }
    return obj;
}

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

/**
 * If the next value is {@link JsonToken#NULL}, consume it and return {@code true}. Otherwise
 * return {@code false}.// ww w  . ja va  2  s  . c o m
 */
public static boolean handleNull(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return true;
    }
    return false;
}

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

/**
 * Determines what the next value is and returns it as the appropriate basic type or a custom
 * object, a collection, a {@link JSONObject}, or {@link JSONArray}.
 *
 * @param reader The JsonReader to use. The next token ({@link JsonReader#peek()} must be a
 * value./*www.ja  v  a  2  s .co  m*/
 * @param convertJsonTypes If {@code true}, and the next value is a JSONArray, it will be
 * converted to a Collection, and if the next value is a JSONObject, it will be parsed into the
 * appropriate object type. If {@code false}, a raw JSONArray or JSONObject will be returned.
 *
 * @return The next value. If the next value is {@link JsonToken#NULL}, then {@code null} is
 * returned.
 */
public static Object parseNextValue(JsonReader reader, boolean convertJsonTypes) throws IOException {
    JsonToken nextToken = reader.peek();
    switch (nextToken) {
    case BEGIN_ARRAY:
        if (convertJsonTypes) {
            Collection<Object> collection = new ArrayList<>();
            parseJsonArray(reader, collection, null, Object.class, null, null);
            return collection;
        } else {
            return parseAsJsonArray(reader, null);
        }
    case BEGIN_OBJECT:
        if (convertJsonTypes) {
            return parseJsonObject(reader, null, null, null);
        } else {
            return parseAsJsonObject(reader, null);
        }
    case BOOLEAN:
        return reader.nextBoolean();
    case NUMBER:
    case STRING:
        return reader.nextString();
    case NULL:
        reader.nextNull();
        return null;

    default:
        throw new IllegalStateException("Unexpected token: " + nextToken);
    }
}

From source file:com.fuzz.android.limelight.util.JSONTool.java

/**
 * @param reader//from   w  w  w  .  j ava  2  s  . c  om
 * @return the generated ChapterTransition object from JSON
 * @throws IOException
 */
public static ChapterTransition readTransition(JsonReader reader) throws IOException {
    long time = -1;
    int itemPosition = -1;
    int childId = -1;
    int anchorId = -1;
    String message = null;
    int messageResId = -1;
    int grapicResID = -1;
    boolean isActionBarItem = false;
    double xOffset = -1;
    double yOffset = -1;
    int textColor = -1;
    int textBackgroundColor = -1;
    float textSize = -1;
    boolean textBackgroundTransparent = false;
    String animation = null;

    while (reader.hasNext()) {
        try {
            String name = reader.nextName();
            if (name.equals("time"))
                time = reader.nextLong();
            else if (name.equals("item_position"))
                itemPosition = reader.nextInt();
            else if (name.equals("child_id"))
                childId = reader.nextInt();
            else if (name.equals("id"))
                anchorId = reader.nextInt();
            else if (name.equals("message"))
                message = reader.nextString();
            else if (name.equals("message_res_id"))
                messageResId = reader.nextInt();
            else if (name.equals("graphic_res_id"))
                grapicResID = reader.nextInt();
            else if (name.equals("is_action_bar_item"))
                isActionBarItem = reader.nextBoolean();
            else if (name.equals("x_offset"))
                xOffset = reader.nextDouble();
            else if (name.equals("y_offset"))
                yOffset = reader.nextDouble();
            else if (name.equals("text_color"))
                textColor = reader.nextInt();
            else if (name.equals("text_background_color"))
                textBackgroundColor = reader.nextInt();
            else if (name.equals("text_size"))
                textSize = reader.nextLong();
            else if (name.equals("text_background_transparent"))
                textBackgroundTransparent = reader.nextBoolean();
            else if (name.equals("animation"))
                animation = reader.nextString();
        } catch (IllegalStateException e) {
            reader.nextNull();
            e.printStackTrace();
        }
    }

    reader.endObject();

    ChapterTransition transition = new ChapterTransition();
    transition.setTime(time);
    transition.setItemPosition(itemPosition);
    transition.setChildID(childId);
    transition.setId(anchorId);
    transition.setMessage(message);
    transition.setMessageResID(messageResId);
    transition.setGraphicResID(grapicResID);
    transition.setIsActionBarItem(isActionBarItem);
    transition.setDisplacement(xOffset, yOffset);
    transition.setTextColor(textColor);
    transition.setTextBackgroundColor(textBackgroundColor);
    transition.setTextSize(textSize);
    transition.setTransparentBackground(textBackgroundTransparent);
    transition.setAnimation(animation);

    return transition;
}

From source file:com.dalaran.async.task.http.AbstractHTTPService.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected List<ContentValues> parseJson(JsonReader reader) throws IOException {

    List<ContentValues> contentValueses = new ArrayList<ContentValues>();
    ContentValues values = new ContentValues();
    Long threadId = 0L;/*from   w  w  w.  jav a  2  s.  co m*/
    boolean notEnd = true;
    String name = "";
    if (reader.hasNext()) { //todo android.util.MalformedJsonException: Use JsonReader.setLenient(true)
        do {
            switch (reader.peek()) {
            case BEGIN_OBJECT:
                values = new ContentValues();
                if (threadId != 0) {
                    values.put("threadId", threadId);
                }
                reader.beginObject();
                break;
            case BEGIN_ARRAY:
                if (values != null && values.getAsLong("threadId") != null) {
                    threadId = values.getAsLong("threadId");
                }
                reader.beginArray();
                break;
            case BOOLEAN:
                values.put(name, reader.nextBoolean());
                break;
            case END_ARRAY:
                reader.endArray();
                break;
            case END_DOCUMENT:
                notEnd = false;
                break;
            case END_OBJECT:
                contentValueses.add(values);
                reader.endObject();
                break;
            case NAME:
                name = reader.nextName();
                break;
            case NULL:
                reader.nextNull();
                break;
            case NUMBER:
                values.put(name, reader.nextDouble());
                break;
            case STRING:
                values.put(name, reader.nextString());
                break;
            default:
                reader.skipValue();
            }
        } while (notEnd);
    }
    return contentValueses;
}