Code example for CharacterIterator

Methods: currentfirstnextprevious

0
 
    public Object read(CharacterIterator ci, int start) {
        it = ci;
        switch (start) {
        case FIRST:
            c = it.first();
            break; 
        case CURRENT:
            c = it.current();
            break; 
        case NEXT:
            c = it.next();
            break; 
        } 
        return read(); 
    } 
 
    public Object read(CharacterIterator it) {
        return read(it, NEXT);
    } 
 
    public Object read(String string) {
        return read(new StringCharacterIterator(string), FIRST);
    } 
 
    private Object read() {
        skipWhiteSpace(); 
        char ch = c;
        next(); 
        switch (ch) {
            case '"': token = string(); break;
            case '[': token = array(); break;
            case ']': token = ARRAY_END; break;
            case ',': token = COMMA; break;
            case '{': token = object(); break;
            case '}': token = OBJECT_END; break;
            case ':': token = COLON; break;
            case 't': 
                if (c != 'r' || next() != 'u' || next() != 'e')
                	throw new RuntimeException("Invalid JSON token: expected 'true' keyword.");
                token = Boolean.TRUE;
                break; 
            case'f': 
                if (c != 'a' || next() != 'l' || next() != 's' || next() != 'e')
                	throw new RuntimeException("Invalid JSON token: expected 'false' keyword.");
                token = Boolean.FALSE;
                break; 
            case 'n': 
                if (c != 'u' || next() != 'l' || next() != 'l')
                	throw new RuntimeException("Invalid JSON token: expected 'null' keyword.");
                token = null;
                break; 
            default: 
                c = it.previous();
                if (Character.isDigit(c) || c == '-') {
                    token = number();
                } 
        } 
        // System.out.println("token: " + token); // enable this line to see the token stream