Example usage for org.json JSONTokener syntaxError

List of usage examples for org.json JSONTokener syntaxError

Introduction

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

Prototype

public JSONException syntaxError(String message) 

Source Link

Document

Make a JSONException to signal a syntax error.

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  .  ja  v  a 2s .  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:org.araqne.confdb.file.Importer.java

private Map<String, Object> parseMetadata(JSONTokener x) throws JSONException, IOException {
    if (x.nextClean() != '{') {
        throw x.syntaxError("A JSONObject text must begin with '{'");
    }/*from   www.ja  va 2 s.c o  m*/

    Object key = x.nextValue();
    if (!key.equals("metadata"))
        throw x.syntaxError("confdb metadata should be placed first");

    x.nextClean();
    return parse((JSONObject) x.nextValue());
}

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.ja  v  a  2 s.  co  m*/
 * @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./*w w  w  .ja va 2  s  . c  om*/
 * @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:com.jskaleel.xml.JSONObject.java

/**
 * Construct a JSONObject from a JSONTokener.
 *
 * @param x// w  ww.ja  v  a 2 s. c o m
 *            A JSONTokener object containing the source string.
 * @throws JSONException
 *             If there is a syntax error in the source string or a
 *             duplicated key.
 * @throws org.json.JSONException 
 */
public JSONObject(JSONTokener x) throws JSONException, org.json.JSONException {
    this();
    char c;
    String key;

    if (x.nextClean() != '{') {
        throw x.syntaxError("A JSONObject text must begin with '{'");
    }
    for (;;) {
        c = x.nextClean();
        switch (c) {
        case 0:
            throw x.syntaxError("A JSONObject text must end with '}'");
        case '}':
            return;
        default:
            x.back();
            key = x.nextValue().toString();
        }

        // The key is followed by ':'.

        c = x.nextClean();
        if (c != ':') {
            throw x.syntaxError("Expected a ':' after a key");
        }
        this.putOnce(key, x.nextValue());

        // Pairs are separated by ','.

        switch (x.nextClean()) {
        case ';':
        case ',':
            if (x.nextClean() == '}') {
                return;
            }
            x.back();
            break;
        case '}':
            return;
        default:
            throw x.syntaxError("Expected a ',' or '}'");
        }
    }
}

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;// w  ww. ja v a2  s .  c om
    }
    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 .j  a  v a  2  s. c  om
 * @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;
}