Example usage for org.json JSONTokener next

List of usage examples for org.json JSONTokener next

Introduction

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

Prototype

public char next() 

Source Link

Document

Get the next character in the source string.

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./* w  w  w .  j  a va2s .  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./*from ww w  .  jav  a 2 s  .co m*/
 *
 * @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.CDL.java

/**
 * Get the next value. The value can be wrapped in quotes. The value can
 * be empty.//from  ww w.  j a v  a 2  s  . c om
 * @param x A JSONTokener of the source text.
 * @return The value string, or null if empty.
 * @throws JSONException if the quoted string is badly formed.
 */
private static String getValue(JSONTokener x) throws JSONException {
    char c;
    char q;
    StringBuffer sb;
    do {
        c = x.next();
    } while (c == ' ' || c == '\t');
    switch (c) {
    case 0:
        return null;
    case '"':
    case '\'':
        q = c;
        sb = new StringBuffer();
        for (;;) {
            c = x.next();
            if (c == q) {
                break;
            }
            if (c == 0 || c == '\n' || c == '\r') {
                throw x.syntaxError("Missing close quote '" + q + "'.");
            }
            sb.append(c);
        }
        return sb.toString();
    case ',':
        x.back();
        return "";
    default:
        x.back();
        return x.nextTo(',');
    }
}

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

/**
 * Produce a JSONArray of strings from a row of comma delimited values.
 * @param x A JSONTokener of the source text.
 * @return A JSONArray of strings./*ww w  .j  av  a  2 s .  com*/
 * @throws JSONException
 */
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
    JSONArray ja = new JSONArray();
    for (;;) {
        String value = getValue(x);
        char c = x.next();
        if (value == null || (ja.length() == 0 && value.length() == 0 && c != ',')) {
            return null;
        }
        ja.put(value);
        for (;;) {
            if (c == ',') {
                break;
            }
            if (c != ' ') {
                if (c == '\n' || c == '\r' || c == 0) {
                    return ja;
                }
                throw x.syntaxError("Bad character '" + c + "' (" + (int) c + ").");
            }
            c = x.next();
        }
    }
}

From source file:org.apache.oltu.oauth2.ext.dynamicreg.server.request.JSONHttpServletRequestWrapper.java

public Map<String, String[]> getParameterMap() {
    if (!bodyRead) {
        String body = readJsonBody();

        final JSONTokener x = new JSONTokener(body);
        char c;//from w ww.  j a  va  2s.c  o m
        String key;

        if (x.nextClean() != '{') {
            throw new OAuthRuntimeException(format(
                    "String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
                    body));
        }
        for (;;) {
            c = x.nextClean();
            switch (c) {
            case 0:
                throw new OAuthRuntimeException(format(
                        "String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
                        body));
            case '}':
                return Collections.unmodifiableMap(parameters);
            default:
                x.back();
                key = x.nextValue().toString();
            }

            /*
             * The key is followed by ':'. We will also tolerate '=' or '=>'.
             */
            c = x.nextClean();
            if (c == '=') {
                if (x.next() != '>') {
                    x.back();
                }
            } else if (c != ':') {
                throw new OAuthRuntimeException(format(
                        "String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
                        body, key));
            }
            Object value = x.nextValue();

            // guard from null values
            if (value != null) {
                if (value instanceof JSONArray) { // only plain simple arrays in this version
                    JSONArray array = (JSONArray) value;
                    String[] values = new String[array.length()];
                    for (int i = 0; i < array.length(); i++) {
                        values[i] = String.valueOf(array.get(i));
                    }
                    parameters.put(key, values);
                } else {
                    parameters.put(key, new String[] { String.valueOf(value) });
                }
            }

            /*
             * Pairs are separated by ','. We will also tolerate ';'.
             */
            switch (x.nextClean()) {
            case ';':
            case ',':
                if (x.nextClean() == '}') {
                    return Collections.unmodifiableMap(parameters);
                }
                x.back();
                break;
            case '}':
                return Collections.unmodifiableMap(parameters);
            default:
                throw new OAuthRuntimeException(format(
                        "String '%s' is not a valid JSON object representation, Expected a ',' or '}", body));
            }
        }
    }

    return Collections.unmodifiableMap(parameters);
}