Example usage for com.mongodb.util JSONParseException JSONParseException

List of usage examples for com.mongodb.util JSONParseException JSONParseException

Introduction

In this page you can find the example usage for com.mongodb.util JSONParseException JSONParseException.

Prototype

public JSONParseException(final String jsonString, final int position) 

Source Link

Document

Creates a new instance.

Usage

From source file:com.xinqihd.sns.gameserver.util.JSON.java

License:Apache License

/**
 * Parse an unknown type.//from   w w  w  .  j  ava2 s .  c  o m
 * 
 * @return Object the next item
 * @throws JSONParseException
 *           if invalid JSON is found
 */
protected Object parse(String name) {
    Object value = null;
    char current = get();

    switch (current) {
    // null
    case 'n':
        read('n');
        read('u');
        read('l');
        read('l');
        value = null;
        break;
    // NaN
    case 'N':
        read('N');
        read('a');
        read('N');
        value = Double.NaN;
        break;
    // true
    case 't':
        read('t');
        read('r');
        read('u');
        read('e');
        value = true;
        break;
    // false
    case 'f':
        read('f');
        read('a');
        read('l');
        read('s');
        read('e');
        value = false;
        break;
    // string
    case '\'':
    case '\"':
        value = parseString(true);
        break;
    // number
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '+':
    case '-':
        value = parseNumber();
        break;
    // array
    case '[':
        value = parseArray(name);
        break;
    // object
    case '{':
        value = parseObject(name);
        break;
    default:
        throw new JSONParseException(s, pos);
    }
    return value;
}

From source file:com.xinqihd.sns.gameserver.util.JSON.java

License:Apache License

/**
 * Read the current character, making sure that it is the expected character.
 * Advances the pointer to the next character.
 * /*from  w w  w. j  a  v  a  2  s  .  co m*/
 * @param ch
 *          the character expected
 * 
 * @throws JSONParseException
 *           if the current character does not match the given character
 */
public void read(char ch) {
    if (!check(ch)) {
        throw new JSONParseException(s, pos);
    }
    pos++;
}

From source file:com.xinqihd.sns.gameserver.util.JSON.java

License:Apache License

/**
 * Read the current character, making sure that it is a hexidecimal character.
 * //from  w  ww . j  a  va  2 s.c o m
 * @throws JSONParseException
 *           if the current character is not a hexidecimal character
 */
public void readHex() {
    if (pos < s.length()
            && ((s.charAt(pos) >= '0' && s.charAt(pos) <= '9') || (s.charAt(pos) >= 'A' && s.charAt(pos) <= 'F')
                    || (s.charAt(pos) >= 'a' && s.charAt(pos) <= 'f'))) {
        pos++;
    } else {
        throw new JSONParseException(s, pos);
    }
}

From source file:com.xinqihd.sns.gameserver.util.JSON.java

License:Apache License

/**
 * Parses a string./*w  w w .  ja  va  2  s  .  com*/
 * 
 * @return the next string.
 * @throws JSONParseException
 *           if invalid JSON is found
 */
public String parseString(boolean needQuote) {
    char quot = 0;
    if (check('\''))
        quot = '\'';
    else if (check('\"'))
        quot = '\"';
    else if (needQuote)
        throw new JSONParseException(s, pos);

    char current;

    if (quot > 0)
        read(quot);
    StringBuilder buf = new StringBuilder();
    int start = pos;
    while (pos < s.length()) {
        current = s.charAt(pos);
        if (quot > 0) {
            if (current == quot)
                break;
        } else {
            if (current == ':' || current == ' ')
                break;
        }

        if (current == '\\') {
            pos++;

            char x = get();

            char special = 0;

            switch (x) {

            case 'u': { // decode unicode
                buf.append(s.substring(start, pos - 1));
                pos++;
                int tempPos = pos;

                readHex();
                readHex();
                readHex();
                readHex();

                int codePoint = Integer.parseInt(s.substring(tempPos, tempPos + 4), 16);
                buf.append((char) codePoint);

                start = pos;
                continue;
            }
            case 'n':
                special = '\n';
                break;
            case 'r':
                special = '\r';
                break;
            case 't':
                special = '\t';
                break;
            case 'b':
                special = '\b';
                break;
            case '"':
                special = '\"';
                break;
            case '\\':
                special = '\\';
                break;
            }

            buf.append(s.substring(start, pos - 1));
            if (special != 0) {
                pos++;
                buf.append(special);
            }
            start = pos;
            continue;
        }
        pos++;
    }
    buf.append(s.substring(start, pos));
    if (quot > 0)
        read(quot);
    return buf.toString();
}

From source file:com.xinqihd.sns.gameserver.util.JSON.java

License:Apache License

/**
 * Parses the next array.// w ww  .java  2s  . c o m
 * 
 * @return the array
 * @throws JSONParseException
 *           if invalid JSON is found
 */
protected Object parseArray(String name) {
    if (name != null) {
        _callback.arrayStart(name);
    } else {
        _callback.arrayStart();
    }

    read('[');

    int i = 0;
    char current = get();
    while (current != ']') {
        String elemName = String.valueOf(i++);
        Object elem = parse(elemName);
        doCallback(elemName, elem);

        if ((current = get()) == ',') {
            read(',');
        } else if (current == ']') {
            break;
        } else {
            throw new JSONParseException(s, pos);
        }
    }

    read(']');

    return _callback.arrayDone();
}