Example usage for android.util JsonReader nextBoolean

List of usage examples for android.util JsonReader nextBoolean

Introduction

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

Prototype

public boolean nextBoolean() throws IOException 

Source Link

Document

Returns the JsonToken#BOOLEAN boolean value of the next token, consuming it.

Usage

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

/**
 * @param reader//from   w  w w.  j  a va 2  s. c o  m
 * @return the generated BaseChapter object from the JSON
 * @throws IOException
 */
public static Chapter readChapter(JsonReader reader) throws IOException {
    long time = -1;
    boolean hasActView = false;
    BaseChapter chapter = new BaseChapter();

    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("duration"))
            time = reader.nextLong();
        else if (name.equals("has_act_view"))
            hasActView = reader.nextBoolean();
        else if (name.equals("act"))
            chapter.setAct(readAct(reader));
    }
    reader.endObject();

    chapter.setTime(time);
    chapter.setHasActView(hasActView);
    return chapter;
}

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();/* w  ww  . j  a v  a  2  s  .  com*/
    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:com.murrayc.galaxyzoo.app.LoginUtils.java

public static LoginResult parseLoginResponseContent(final InputStream content) throws IOException {
    //A failure by default.
    LoginResult result = new LoginResult(false, null, null);

    final InputStreamReader streamReader = new InputStreamReader(content, Utils.STRING_ENCODING);
    final JsonReader reader = new JsonReader(streamReader);
    reader.beginObject();/*from  w  w w.j  av  a  2 s.c  o m*/
    boolean success = false;
    String apiKey = null;
    String userName = null;
    String message = null;
    while (reader.hasNext()) {
        final String name = reader.nextName();
        switch (name) {
        case "success":
            success = reader.nextBoolean();
            break;
        case "api_key":
            apiKey = reader.nextString();
            break;
        case "name":
            userName = reader.nextString();
            break;
        case "message":
            message = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }

    if (success) {
        result = new LoginResult(true, userName, apiKey);
    } else {
        Log.info("Login failed.");
        Log.info("Login failure message: " + message);
    }

    reader.endObject();
    reader.close();

    streamReader.close();

    return result;
}

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();//from  w  w  w  .j a v a2s.com
    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

public static Boolean nextBoolean(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return false;
    }/*from ww w. j a  v a2  s.  c  om*/
    assertType(reader, name, JsonToken.BOOLEAN);
    return reader.nextBoolean();
}

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;
    }/* w w w.  ja  va 2  s.  com*/
    assertType(reader, name, JsonToken.STRING, JsonToken.NUMBER, JsonToken.BOOLEAN);

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

    return reader.nextString();
}

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

/**
 * @param reader/*from   w  ww  .  ja  v  a2s.com*/
 * @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:dk.cafeanalog.AnalogDownloader.java

public AnalogStatus isOpen() {
    HttpURLConnection connection = null;
    JsonReader reader = null;

    try {/*from   w w  w  .ja  v a2 s  .  c o m*/
        URL url = new URL("http", "cafeanalog.dk", "api/open");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        reader = new JsonReader(new InputStreamReader(connection.getInputStream()));
        reader.beginObject();
        while (!reader.nextName().equals("open")) {
            reader.skipValue();
        }
        return reader.nextBoolean() ? AnalogStatus.OPEN : AnalogStatus.CLOSED;
    } catch (IOException e) {
        return AnalogStatus.UNKNOWN;
    } finally {
        if (connection != null)
            connection.disconnect();
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}

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

private void handleResponse(@NotNull HttpResponse response) throws IOException, ParseException {
    JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent()));

    //noinspection TryFinallyCanBeTryWithResources
    try {/*from  w ww  .j  av a  2s. c  o  m*/
        reader.beginObject();

        BuildInfoData data = new BuildInfoData();
        SimpleDateFormat dateFormat = new SimpleDateFormat(Common.TEAMCITY_DATE_FORMAT);

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "status":
                if (data.status == null) {
                    data.status = com.tcity.android.Status.valueOf(reader.nextString());
                }
                break;
            case "running":
                if (reader.nextBoolean()) {
                    data.status = com.tcity.android.Status.RUNNING;
                }
                break;
            case "branchName":
                data.branch = reader.nextString();
                break;
            case "defaultBranch":
                data.isBranchDefault = reader.nextBoolean();
                break;
            case "statusText":
                data.result = reader.nextString();
                break;
            case "waitReason":
                data.waitReason = reader.nextString();
                break;
            case "queuedDate":
                data.queued = dateFormat.parse(reader.nextString());
                break;
            case "startDate":
                data.started = dateFormat.parse(reader.nextString());
                break;
            case "finishDate":
                data.finished = dateFormat.parse(reader.nextString());
                break;
            case "agent":
                data.agent = getAgentName(reader);
                break;
            default:
                reader.skipValue();
            }
        }

        myResult = data;

        reader.endObject();
    } finally {
        reader.close();
    }
}