Example usage for android.util JsonReader hasNext

List of usage examples for android.util JsonReader hasNext

Introduction

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

Prototype

public boolean hasNext() throws IOException 

Source Link

Document

Returns true if the current array or object has another element.

Usage

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

/**
 * @param reader/*w ww  . j  ava 2s. c om*/
 * @return the generated Act object from the JSON
 * @throws IOException
 */
public static Act readAct(JsonReader reader) throws IOException {
    int id = -1;
    String message = null;
    int messageResId = -1;
    int graphResId = -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;
    String activityName = null;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("id"))
            id = 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"))
            graphResId = 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();
        else if (name.equals("activity_name"))
            activityName = reader.nextString();
        else
            reader.skipValue();
    }
    reader.endObject();

    Act act = new Act();
    act.setId(id);
    act.setMessage(message);
    act.setMessageResID(messageResId);
    act.setGraphicResID(graphResId);
    act.setIsActionBarItem(isActionBarItem);
    act.setDisplacement(xOffset, yOffset);
    act.setTextColor(textColor);
    act.setTextBackgroundColor(textBackgroundColor);
    act.setTextSize(textSize);
    act.setTransparentBackground(textBackgroundTransparent);
    act.setAnimation(animation);
    act.setActivityName(activityName);
    act.getLayout();

    return act;
}

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

/**
 * @param reader// w ww .  j  a v a2s  .c  o  m
 * @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.workday.autoparse.json.demo.UnannotatedObjectParser.java

private void parseFromReader(UnannotatedObject out, JsonReader reader) throws IOException {
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
        case "string": {
            out.string = reader.nextString();
            break;
        }/* w  ww  . j  a  v a  2  s  . c  o m*/
        default: {
            reader.skipValue();
        }
        }
    }
}

From source file:com.workday.autoparse.json.demo.ParserAnnotatedObjectParser.java

private void parseFromReader(ParserAnnotatedObject out, JsonReader reader) throws IOException {
    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (discriminationName.equals(name)) {
            out.discriminationValue = JsonParserUtils.nextString(reader, discriminationName);
            continue;
        }//from  w  w w  .j av a2 s.  co  m

        switch (name) {
        case "string": {
            out.string = reader.nextString();
            break;
        }
        default: {
            reader.skipValue();
        }
        }
    }
}

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

/**
 * Parse the next value as an array, but do not attempt to convert it into a {@link Collection},
 * or to convert any children into known types. The returned object will be a {@link JSONArray}
 * and all children will be JSONObjects, JSONArrays, and primitives.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginArray()} and {@link
 * JsonReader#endArray()} 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./* ww w.  ja  v a 2 s.  co m*/
 */
public static JSONArray parseAsJsonArray(JsonReader reader, String key) throws IOException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_ARRAY);

    JSONArray jsonArray = new JSONArray();
    reader.beginArray();
    while (reader.hasNext()) {
        jsonArray.put(parseNextValue(reader, false));
    }
    reader.endArray();
    return jsonArray;
}

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./* www  . j  a  v  a  2 s .c o  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.tcity.android.ui.info.BuildInfoTask.java

@Nullable
private String getAgentName(@NotNull JsonReader reader) throws IOException {
    reader.beginObject();//from   w  w w .  j a v a 2 s  . c o m

    String result = null;

    while (reader.hasNext()) {
        switch (reader.nextName()) {
        case "name":
            result = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }

    reader.endObject();

    return result;
}

From source file:com.tcity.android.ui.info.BuildArtifactsTask.java

@Nullable
private String getHref(@NotNull JsonReader reader) throws IOException {
    reader.beginObject();/*from   w ww. j  a v  a  2  s.  co  m*/

    String result = null;

    while (reader.hasNext()) {
        switch (reader.nextName()) {
        case "href":
            result = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }

    reader.endObject();

    return result;
}

From source file:pedromendes.tempodeespera.HospitalDetailActivity.java

private List<Emergency> readHospitalDetailGetResponse(JsonReader reader) throws IOException {
    List<Emergency> result = null;
    reader.beginObject();/*from   w w  w. j ava 2s  .  co  m*/
    while (reader.hasNext()) {
        result = readResult(reader);
    }
    reader.endObject();
    return result;
}

From source file:pedromendes.tempodeespera.HospitalDetailActivity.java

private void readEmergency(JsonReader reader, Emergency hospitalEmergencyDetail) throws IOException {
    while (reader.hasNext()) {
        String fieldDame = reader.nextName();
        if (fieldDame.equals("Emergency")) {
            reader.beginObject();//from   w w w . j av  a 2  s.com
            reader.nextName();
            reader.nextString();
            reader.nextName();
            hospitalEmergencyDetail.setDescription(reader.nextString());
            reader.endObject();
        } else if (fieldDame.equals("Queue") && reader.peek() != JsonToken.NULL) {
            reader.beginObject();
            reader.nextName();
            reader.nextString();
            reader.nextName();
            hospitalEmergencyDetail.setName(reader.nextString());
            reader.endObject();
        } else if (fieldDame.equals("Red")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getRedQueue());
            reader.endObject();
        } else if (fieldDame.equals("Orange")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getOrangeQueue());
            reader.endObject();
        } else if (fieldDame.equals("Yellow")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getYellowQueue());
            reader.endObject();
        } else if (fieldDame.equals("Green")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getGreenQueue());
            reader.endObject();
        } else if (fieldDame.equals("Blue")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getBlueQueue());
            reader.endObject();
        } else if (fieldDame.equals("LastUpdate")) {
            hospitalEmergencyDetail.setLastUpdate(reader.nextString());
        } else {
            reader.skipValue();
        }
    }
}