@Test public void test() throws Throwable { FileUtils.openResource("/example.json") .map(JsonTokeniser::new) .map(jt -> { JsonEvent ev; while ((ev = jt.getNextEvent()) != JsonEvent.Type.EOF) { } return 0; }).orElseThrow(); }
public JsonEvent getNextEvent() { if (rdr == null) { return JsonEvent.Type.EOF; } try { int ic = nextChar(); char nc = (char)ic; while (ic != -1 && Character.isWhitespace(nc)) { ic = nextChar(); nc = (char)ic; } if (ic == -1) { rdr = null; return JsonEvent.Type.EOF; } else { switch (nc) { case '{': pushState(State.OBJECT_NAME); return JsonEvent.Type.OBJECT_START; case '}': popState(); return JsonEvent.Type.OBJECT_END; case '[': pushState(State.OTHER); return JsonEvent.Type.ARRAY_START; case ']': popState(); return JsonEvent.Type.ARRAY_END; case ',': if (state == State.OBJECT_VALUE) { state = State.OBJECT_NAME; } return JsonEvent.Type.COMMA; case ':': state = State.OBJECT_VALUE; return JsonEvent.Type.COLON; case '"': { while (true) { final char c2 = nextStringChar(); switch (c2) { case '"': return (state == State.OBJECT_NAME) ? new JsonEvent.FieldName(buffer.release()) : new JsonEvent.JString(buffer.release()); case '\\': { final char esc0 = nextStringChar(); switch (esc0) { case 'u': final byte esc1 = nextStringUniChar(); final byte esc2 = nextStringUniChar(); final byte esc3 = nextStringUniChar(); final byte esc4 = nextStringUniChar(); final int hc = (esc1 << 12) | (esc2 << 8) | (esc3 << 4) | (esc4); buffer.add((char)hc); break; case '\\': buffer.add('\\'); break; case '/': buffer.add('/'); break; case 'b': buffer.add('\b'); break; case 'f': buffer.add('\f'); break; case 'n': buffer.add('\n'); break; case 'r': buffer.add('\r'); break; case 't': buffer.add('\t'); break; case '"': buffer.add('"'); break; default: throw raiseError( "Unrecognised escape character in string - '" + esc0 + "'" ); } break; } case '\b': case '\f': case '\n': case '\r': case '\t': throw raiseError( "Control characters not allowed in strings" ); default: buffer.add(c2); break; } } } case 't': { parseSymbol(TRUE); return JsonEvent.Type.TRUE; } case 'f': { parseSymbol(FALSE); return JsonEvent.Type.FALSE; } case 'n': { parseSymbol(NULL); return JsonEvent.Type.NULL; } case '0': buffer.add(nc); return parseNumber(NumState.B); case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer.add(nc); return parseNumber(NumState.C); case '-': case '+': buffer.add(nc); return parseNumber(NumState.A); default: throw raiseError("Unexpected input '" + nc + "'"); } } } catch (IOException ex) { throw new JsonException(ex); } }
JsonTokeniser { public JsonEvent getNextEvent() { if (rdr == null) { return JsonEvent.Type.EOF; } try { int ic = nextChar(); char nc = (char)ic; while (ic != -1 && Character.isWhitespace(nc)) { ic = nextChar(); nc = (char)ic; } if (ic == -1) { rdr = null; return JsonEvent.Type.EOF; } else { switch (nc) { case '{': pushState(State.OBJECT_NAME); return JsonEvent.Type.OBJECT_START; case '}': popState(); return JsonEvent.Type.OBJECT_END; case '[': pushState(State.OTHER); return JsonEvent.Type.ARRAY_START; case ']': popState(); return JsonEvent.Type.ARRAY_END; case ',': if (state == State.OBJECT_VALUE) { state = State.OBJECT_NAME; } return JsonEvent.Type.COMMA; case ':': state = State.OBJECT_VALUE; return JsonEvent.Type.COLON; case '"': { while (true) { final char c2 = nextStringChar(); switch (c2) { case '"': return (state == State.OBJECT_NAME) ? new JsonEvent.FieldName(buffer.release()) : new JsonEvent.JString(buffer.release()); case '\\': { final char esc0 = nextStringChar(); switch (esc0) { case 'u': final byte esc1 = nextStringUniChar(); final byte esc2 = nextStringUniChar(); final byte esc3 = nextStringUniChar(); final byte esc4 = nextStringUniChar(); final int hc = (esc1 << 12) | (esc2 << 8) | (esc3 << 4) | (esc4); buffer.add((char)hc); break; case '\\': buffer.add('\\'); break; case '/': buffer.add('/'); break; case 'b': buffer.add('\b'); break; case 'f': buffer.add('\f'); break; case 'n': buffer.add('\n'); break; case 'r': buffer.add('\r'); break; case 't': buffer.add('\t'); break; case '"': buffer.add('"'); break; default: throw raiseError( "Unrecognised escape character in string - '" + esc0 + "'" ); } break; } case '\b': case '\f': case '\n': case '\r': case '\t': throw raiseError( "Control characters not allowed in strings" ); default: buffer.add(c2); break; } } } case 't': { parseSymbol(TRUE); return JsonEvent.Type.TRUE; } case 'f': { parseSymbol(FALSE); return JsonEvent.Type.FALSE; } case 'n': { parseSymbol(NULL); return JsonEvent.Type.NULL; } case '0': buffer.add(nc); return parseNumber(NumState.B); case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer.add(nc); return parseNumber(NumState.C); case '-': case '+': buffer.add(nc); return parseNumber(NumState.A); default: throw raiseError("Unexpected input '" + nc + "'"); } } } catch (IOException ex) { throw new JsonException(ex); } } }
JsonTokeniser { public JsonEvent getNextEvent() { if (rdr == null) { return JsonEvent.Type.EOF; } try { int ic = nextChar(); char nc = (char)ic; while (ic != -1 && Character.isWhitespace(nc)) { ic = nextChar(); nc = (char)ic; } if (ic == -1) { rdr = null; return JsonEvent.Type.EOF; } else { switch (nc) { case '{': pushState(State.OBJECT_NAME); return JsonEvent.Type.OBJECT_START; case '}': popState(); return JsonEvent.Type.OBJECT_END; case '[': pushState(State.OTHER); return JsonEvent.Type.ARRAY_START; case ']': popState(); return JsonEvent.Type.ARRAY_END; case ',': if (state == State.OBJECT_VALUE) { state = State.OBJECT_NAME; } return JsonEvent.Type.COMMA; case ':': state = State.OBJECT_VALUE; return JsonEvent.Type.COLON; case '"': { while (true) { final char c2 = nextStringChar(); switch (c2) { case '"': return (state == State.OBJECT_NAME) ? new JsonEvent.FieldName(buffer.release()) : new JsonEvent.JString(buffer.release()); case '\\': { final char esc0 = nextStringChar(); switch (esc0) { case 'u': final byte esc1 = nextStringUniChar(); final byte esc2 = nextStringUniChar(); final byte esc3 = nextStringUniChar(); final byte esc4 = nextStringUniChar(); final int hc = (esc1 << 12) | (esc2 << 8) | (esc3 << 4) | (esc4); buffer.add((char)hc); break; case '\\': buffer.add('\\'); break; case '/': buffer.add('/'); break; case 'b': buffer.add('\b'); break; case 'f': buffer.add('\f'); break; case 'n': buffer.add('\n'); break; case 'r': buffer.add('\r'); break; case 't': buffer.add('\t'); break; case '"': buffer.add('"'); break; default: throw raiseError( "Unrecognised escape character in string - '" + esc0 + "'" ); } break; } case '\b': case '\f': case '\n': case '\r': case '\t': throw raiseError( "Control characters not allowed in strings" ); default: buffer.add(c2); break; } } } case 't': { parseSymbol(TRUE); return JsonEvent.Type.TRUE; } case 'f': { parseSymbol(FALSE); return JsonEvent.Type.FALSE; } case 'n': { parseSymbol(NULL); return JsonEvent.Type.NULL; } case '0': buffer.add(nc); return parseNumber(NumState.B); case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer.add(nc); return parseNumber(NumState.C); case '-': case '+': buffer.add(nc); return parseNumber(NumState.A); default: throw raiseError("Unexpected input '" + nc + "'"); } } } catch (IOException ex) { throw new JsonException(ex); } } JsonTokeniser(Reader rdr, boolean allowComments);  JsonTokeniser(Reader rdr); }
JsonTokeniser { public JsonEvent getNextEvent() { if (rdr == null) { return JsonEvent.Type.EOF; } try { int ic = nextChar(); char nc = (char)ic; while (ic != -1 && Character.isWhitespace(nc)) { ic = nextChar(); nc = (char)ic; } if (ic == -1) { rdr = null; return JsonEvent.Type.EOF; } else { switch (nc) { case '{': pushState(State.OBJECT_NAME); return JsonEvent.Type.OBJECT_START; case '}': popState(); return JsonEvent.Type.OBJECT_END; case '[': pushState(State.OTHER); return JsonEvent.Type.ARRAY_START; case ']': popState(); return JsonEvent.Type.ARRAY_END; case ',': if (state == State.OBJECT_VALUE) { state = State.OBJECT_NAME; } return JsonEvent.Type.COMMA; case ':': state = State.OBJECT_VALUE; return JsonEvent.Type.COLON; case '"': { while (true) { final char c2 = nextStringChar(); switch (c2) { case '"': return (state == State.OBJECT_NAME) ? new JsonEvent.FieldName(buffer.release()) : new JsonEvent.JString(buffer.release()); case '\\': { final char esc0 = nextStringChar(); switch (esc0) { case 'u': final byte esc1 = nextStringUniChar(); final byte esc2 = nextStringUniChar(); final byte esc3 = nextStringUniChar(); final byte esc4 = nextStringUniChar(); final int hc = (esc1 << 12) | (esc2 << 8) | (esc3 << 4) | (esc4); buffer.add((char)hc); break; case '\\': buffer.add('\\'); break; case '/': buffer.add('/'); break; case 'b': buffer.add('\b'); break; case 'f': buffer.add('\f'); break; case 'n': buffer.add('\n'); break; case 'r': buffer.add('\r'); break; case 't': buffer.add('\t'); break; case '"': buffer.add('"'); break; default: throw raiseError( "Unrecognised escape character in string - '" + esc0 + "'" ); } break; } case '\b': case '\f': case '\n': case '\r': case '\t': throw raiseError( "Control characters not allowed in strings" ); default: buffer.add(c2); break; } } } case 't': { parseSymbol(TRUE); return JsonEvent.Type.TRUE; } case 'f': { parseSymbol(FALSE); return JsonEvent.Type.FALSE; } case 'n': { parseSymbol(NULL); return JsonEvent.Type.NULL; } case '0': buffer.add(nc); return parseNumber(NumState.B); case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer.add(nc); return parseNumber(NumState.C); case '-': case '+': buffer.add(nc); return parseNumber(NumState.A); default: throw raiseError("Unexpected input '" + nc + "'"); } } } catch (IOException ex) { throw new JsonException(ex); } } JsonTokeniser(Reader rdr, boolean allowComments);  JsonTokeniser(Reader rdr); long position(); JsonException raiseError(String msg); JsonEvent getNextEvent(); }
JsonTokeniser { public JsonEvent getNextEvent() { if (rdr == null) { return JsonEvent.Type.EOF; } try { int ic = nextChar(); char nc = (char)ic; while (ic != -1 && Character.isWhitespace(nc)) { ic = nextChar(); nc = (char)ic; } if (ic == -1) { rdr = null; return JsonEvent.Type.EOF; } else { switch (nc) { case '{': pushState(State.OBJECT_NAME); return JsonEvent.Type.OBJECT_START; case '}': popState(); return JsonEvent.Type.OBJECT_END; case '[': pushState(State.OTHER); return JsonEvent.Type.ARRAY_START; case ']': popState(); return JsonEvent.Type.ARRAY_END; case ',': if (state == State.OBJECT_VALUE) { state = State.OBJECT_NAME; } return JsonEvent.Type.COMMA; case ':': state = State.OBJECT_VALUE; return JsonEvent.Type.COLON; case '"': { while (true) { final char c2 = nextStringChar(); switch (c2) { case '"': return (state == State.OBJECT_NAME) ? new JsonEvent.FieldName(buffer.release()) : new JsonEvent.JString(buffer.release()); case '\\': { final char esc0 = nextStringChar(); switch (esc0) { case 'u': final byte esc1 = nextStringUniChar(); final byte esc2 = nextStringUniChar(); final byte esc3 = nextStringUniChar(); final byte esc4 = nextStringUniChar(); final int hc = (esc1 << 12) | (esc2 << 8) | (esc3 << 4) | (esc4); buffer.add((char)hc); break; case '\\': buffer.add('\\'); break; case '/': buffer.add('/'); break; case 'b': buffer.add('\b'); break; case 'f': buffer.add('\f'); break; case 'n': buffer.add('\n'); break; case 'r': buffer.add('\r'); break; case 't': buffer.add('\t'); break; case '"': buffer.add('"'); break; default: throw raiseError( "Unrecognised escape character in string - '" + esc0 + "'" ); } break; } case '\b': case '\f': case '\n': case '\r': case '\t': throw raiseError( "Control characters not allowed in strings" ); default: buffer.add(c2); break; } } } case 't': { parseSymbol(TRUE); return JsonEvent.Type.TRUE; } case 'f': { parseSymbol(FALSE); return JsonEvent.Type.FALSE; } case 'n': { parseSymbol(NULL); return JsonEvent.Type.NULL; } case '0': buffer.add(nc); return parseNumber(NumState.B); case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer.add(nc); return parseNumber(NumState.C); case '-': case '+': buffer.add(nc); return parseNumber(NumState.A); default: throw raiseError("Unexpected input '" + nc + "'"); } } } catch (IOException ex) { throw new JsonException(ex); } } JsonTokeniser(Reader rdr, boolean allowComments);  JsonTokeniser(Reader rdr); long position(); JsonException raiseError(String msg); JsonEvent getNextEvent();  }