Example usage for org.json JSONTokener more

List of usage examples for org.json JSONTokener more

Introduction

In this page you can find the example usage for org.json JSONTokener more.

Prototype

public boolean more() 

Source Link

Document

Determine if the source string still contains characters that next() can consume.

Usage

From source file:com.nosoop.json.VDF.java

/**
 * Attempts to convert what is assumed to be a JSONTokener containing a
 * String with VDF text into the JSON format.
 *
 * @param string Input data, assumed to be in the Valve Data Format.
 * @param convertArrays Whether or not to convert VDF-formatted arrays into
 * JSONArrays.//from   w  ww.j a  v  a2  s .c  o  m
 * @return A JSON representation of the assumed-VDF data.
 * @throws JSONException Parse exception?
 */
public static JSONObject toJSONObject(JSONTokener x, boolean convertArrays) throws JSONException {
    JSONObject jo = new JSONObject();

    while (x.more()) {
        char c = x.nextClean();

        switch (c) {
        case QUOTE:
            // Case that it is a String key, expect its value next.
            String key = x.nextString(QUOTE);

            char ctl = x.nextClean();
            if (ctl == SLASH) {
                if (x.next() == SLASH) {
                    // Comment -- ignore the rest of the line.
                    x.skipTo(NEWLINE);
                    ctl = x.nextClean();
                }
            }

            // Case that the next thing is another String value; add.
            if (ctl == QUOTE) {
                String value = getVDFValue(x, QUOTE);
                jo.put(key, value);
            } // Or a nested KeyValue pair. Parse then add.
            else if (ctl == L_BRACE) {
                jo.put(key, toJSONObject(x, convertArrays));
            }

            // TODO Add support for bracketed tokens?

            break;
        case R_BRACE:
            // Case that we are done parsing this KeyValue collection.
            // Return it (back to the calling toJSONObject() method).
            return jo;
        case '\0':
            // Disregard null character.
            break;
        case SLASH:
            if (x.next() == SLASH) {
                // It's a comment. Skip to the next line.
                x.skipTo(NEWLINE);
                break;
            }
        default:
            String fmtError = "Unexpected character \'%s\'";
            throw x.syntaxError(String.format(fmtError, c));
        }
    }

    if (convertArrays) {
        return convertVDFArrays(jo);
    }

    return jo;
}

From source file:com.nosoop.json.VDF.java

/**
 * Utility method to parse a VDF value./*  www.j  a  va  2s. c  om*/
 *
 * @param x The JSONTokener to use.
 * @param delimiter The character that signals the end of the
 * @return
 * @throws JSONException
 */
private static String getVDFValue(JSONTokener x, final char delimiter) throws JSONException {
    StringBuilder sb = new StringBuilder();

    while (x.more()) {
        char c = x.next();
        if (c == delimiter)
            return sb.toString();
        else
            sb.append(c);
    }
    return sb.toString();
}

From source file:org.official.json.CookieList.java

/**
 * Convert a cookie list into a JSONObject. A cookie list is a sequence
 * of name/value pairs. The names are separated from the values by '='.
 * The pairs are separated by ';'. The names and the values
 * will be unescaped, possibly converting '+' and '%' sequences.
 *
 * To add a cookie to a cooklist,//from  w  ww  .  ja  v  a 2s.  c  o  m
 * cookielistJSONObject.put(cookieJSONObject.getString("name"),
 *     cookieJSONObject.getString("value"));
 * @param string  A cookie list string
 * @return A JSONObject
 * @throws JSONException
 */
public static JSONObject toJSONObject(String string) throws JSONException {
    JSONObject jo = new JSONObject();
    JSONTokener x = new JSONTokener(string);
    while (x.more()) {
        String name = Cookie.unescape(x.nextTo('='));
        x.next('=');
        jo.put(name, Cookie.unescape(x.nextTo(';')));
        x.next();
    }
    return jo;
}

From source file:com.tnc.android.graphite.utils.GraphiteConnection.java

private static void getTargetsRecursive(ArrayList<Target> targets, String serverUrl, String targetFilter,
        int levels) throws Exception {
    BufferedReader reader = getReader(serverUrl + TARGETS_PARAM_STRING + targetFilter);

    String line;/*from   w  ww.  j  a v  a  2 s .c o m*/
    if ((line = reader.readLine()) != null) {
        JSONTokener jTok = new JSONTokener(line);
        if (jTok.more()) {
            JSONArray array = (JSONArray) jTok.nextValue();
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                String id = obj.getString("id");
                int exp = obj.getInt("expandable");
                Target t = new Target();
                t.setHash(hash);
                t.setName(id);
                if (1 == exp) {
                    t.setExpandable(true);
                }
                targets.add(t);
                if (1 == exp && 1 < levels) {
                    // Recursively parse the branch
                    getTargetsRecursive(targets, serverUrl, id + ".*", levels - 1);
                }
            }
        }
    }
}

From source file:com.auth0.api.internal.ApplicationInfoRequest.java

@Override
public void onResponse(Response response) throws IOException {
    if (!response.isSuccessful()) {
        String message = "Received app info failed response with code " + response.code() + " and body "
                + response.body().string();
        postOnFailure(new IOException(message));
        return;//from w w  w .java 2 s .  c  o  m
    }
    try {
        String json = response.body().string();
        JSONTokener tokenizer = new JSONTokener(json);
        tokenizer.skipPast("Auth0.setClient(");
        if (!tokenizer.more()) {
            postOnFailure(tokenizer.syntaxError("Invalid App Info JSONP"));
            return;
        }
        Object nextValue = tokenizer.nextValue();
        if (!(nextValue instanceof JSONObject)) {
            tokenizer.back();
            postOnFailure(tokenizer.syntaxError("Invalid JSON value of App Info"));
        }
        JSONObject jsonObject = (JSONObject) nextValue;
        Log.d(TAG, "Obtained JSON object from JSONP: " + jsonObject);
        Application app = getReader().readValue(jsonObject.toString());
        postOnSuccess(app);
    } catch (JSONException | IOException e) {
        postOnFailure(new APIClientException("Failed to parse JSONP", e));
    }
}

From source file:org.official.json.Cookie.java

/**
 * Convert a cookie specification string into a JSONObject. The string
 * will contain a name value pair separated by '='. The name and the value
 * will be unescaped, possibly converting '+' and '%' sequences. The
 * cookie properties may follow, separated by ';', also represented as
 * name=value (except the secure property, which does not have a value).
 * The name will be stored under the key "name", and the value will be
 * stored under the key "value". This method does not do checking or
 * validation of the parameters. It only converts the cookie string into
 * a JSONObject./* ww  w .java  2s .co  m*/
 * @param string The cookie specification string.
 * @return A JSONObject containing "name", "value", and possibly other
 *  members.
 * @throws JSONException
 */
public static JSONObject toJSONObject(String string) throws JSONException {
    String name;
    JSONObject jo = new JSONObject();
    Object value;
    JSONTokener x = new JSONTokener(string);
    jo.put("name", x.nextTo('='));
    x.next('=');
    jo.put("value", x.nextTo(';'));
    x.next();
    while (x.more()) {
        name = unescape(x.nextTo("=;"));
        if (x.next() != '=') {
            if (name.equals("secure")) {
                value = Boolean.TRUE;
            } else {
                throw x.syntaxError("Missing '=' in cookie parameter.");
            }
        } else {
            value = unescape(x.nextTo(';'));
            x.next();
        }
        jo.put(name, value);
    }
    return jo;
}