Example usage for org.json JSONTokener nextString

List of usage examples for org.json JSONTokener nextString

Introduction

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

Prototype

public String nextString(char quote) throws JSONException 

Source Link

Document

Return the characters up to the next close quote character.

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 w w .j av  a 2 s.c  om
 * @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;
}