Interface TokenReader
- All Superinterfaces:
AutoCloseable,Closeable,RecordReader
- All Known Implementing Classes:
CborTokenReader,Json5lTokenReader,Json5TokenReader,JsonlTokenReader,JsonTokenReader,MsgPackTokenReader
A StAX-like enum cursor that emits a stream of TokenType events without materializing
the whole object graph. This is a format-neutral surface; the same cursor methods work across
every Juneau format that implements TokenReadable (the structural-token role).
Usage:
Contract:
- The cursor is purely structural. It bypasses object swaps and
@Schemaannotations — that is the role of the POJO databind path onParserand its bridges. - For numeric values both the original lexeme and a parsed
Numberare available (seegetNumberLexeme()andgetNumber()); precision-sensitive consumers should prefer the lexeme. - Concrete implementations are not required to be thread-safe.
See Also:
TokenWriter— the push-side counterpart.TokenType— the format-neutral token vocabulary.- Serializers and Parsers
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringCanonical message thrown as anIllegalStateExceptionwhenread(Class)is called while the cursor is not positioned at a value boundary. -
Method Summary
Modifier and TypeMethodDescriptiondefault booleancanRead()Returnstrue ifread(Class)is currently callable on this cursor — i.e. the cursor is positioned at a value boundary.voidclose()Closes the underlying stream.byte[]Returns the current binary value.booleangetBool()Returns the current boolean value.Returns the current token type without advancing.intgetDepth()Returns the current depth of the cursor.default intReturns the typed-binary type byte for the current token.Returns the field name when the current token isTokenType.FIELD_NAME.default BinaryNativeKindReturns the binary-native classification of the current token.Returns the current numeric value as a parsedNumber.Returns the original numeric lexeme for the current numeric token.default intReturns the opaque simple value for the current token.Returns the current scalar value as a string.default longgetTag(int index) Returns the semantic tag at the specified position in the tag stack.default intReturns the number of semantic tags wrapping the current token.default booleanStructural token cursors are always O(1)-memory streamers.next()Advances the cursor to the next token.default <T> TConsumes the upcoming value (object, array, or scalar) and binds it to a POJO via the format's existing parser.default <T> TSame asread(Class)but accepts a parameterizedTypeand optional type arguments (the same shape asParser.read(Object, Type, Type...)).default <T> TSame asread(Class)but accepts a pre-resolvedClassMeta.voidSkips over the children of the current structural token.Methods inherited from interface org.apache.juneau.marshall.stream.RecordReader
iterator, stream
-
Field Details
-
MSG_READ_NON_VALUE_STATE
Canonical message thrown as anIllegalStateExceptionwhenread(Class)is called while the cursor is not positioned at a value boundary.Shared across all formats so the positioning/error contract is uniform (see the
read contract); implementations throw rather than inventing their own wording.new IllegalStateException(MSG_READ_NON_VALUE_STATE)- See Also:
-
-
Method Details
-
next
Advances the cursor to the next token.- Returns:
- The token type of the new current token, or
TokenType.END_OF_STREAMwhen the underlying input is exhausted. Never returnsTokenType.NOT_AVAILABLE— that value is reserved for the initial pre-next()state. - Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed for the format.
-
getCurrentToken
Returns the current token type without advancing.- Returns:
- The current token type. Returns
TokenType.NOT_AVAILABLEbefore the first call tonext(), andTokenType.END_OF_STREAMonce the stream is exhausted.
-
getDepth
int getDepth()Returns the current depth of the cursor.Depth starts at
0 at the document root and increases by 1 for eachTokenType.START_OBJECT/TokenType.START_ARRAYencountered (and decreases on the matching end tokens).- Returns:
- The current depth.
-
getFieldName
Returns the field name when the current token isTokenType.FIELD_NAME.- Returns:
- The field name.
- Throws:
IllegalStateException- If the current token is notTokenType.FIELD_NAME.
-
getString
Returns the current scalar value as a string.Defined for
TokenType.VALUE_STRING(the natural value),TokenType.FIELD_NAME(returns the field name),TokenType.VALUE_NUMBER(returns the lexeme), andTokenType.VALUE_BOOLEAN(returns"true" /"false" ). ForTokenType.VALUE_NULLreturnsnull .- Returns:
- The string view of the current value.
- Throws:
IllegalStateException- If the current token has no string view.
-
getNumber
Returns the current numeric value as a parsedNumber.Implementations may parse lazily; the returned
Numbersubtype is implementation- and format-defined (e.g.Long/Double/BigDecimal). UsegetNumberLexeme()to recover the original textual form.- Returns:
- The parsed numeric value.
- Throws:
IllegalStateException- If the current token is notTokenType.VALUE_NUMBER.ParseException- If the lexeme cannot be parsed.
-
getNumberLexeme
Returns the original numeric lexeme for the current numeric token.For text formats this is the exact substring that produced the number. For binary formats the implementation returns a canonical text rendering of the native numeric token.
- Returns:
- The numeric lexeme.
- Throws:
IllegalStateException- If the current token is notTokenType.VALUE_NUMBER.
-
getBool
boolean getBool()Returns the current boolean value.- Returns:
- The boolean value.
- Throws:
IllegalStateException- If the current token is notTokenType.VALUE_BOOLEAN.
-
getBinary
byte[] getBinary()Returns the current binary value.Binary scalars come from binary formats only (cbor, msgpack, bson, ...). Text formats deliver embedded binary content as base64-encoded
TokenType.VALUE_STRING.- Returns:
- The binary value.
- Throws:
IllegalStateException- If the current token is notTokenType.VALUE_BINARY.
-
canRead
Returnstrue ifread(Class)is currently callable on this cursor — i.e. the cursor is positioned at a value boundary.This is the natural loop predicate when streaming records:
try (TokenReaderr = Json.DEFAULT .readTokens(input )) {r .next();// consume the opening [ while (r .canRead()) { Itemitem =r .read(Item.class ); process(item ); }r .next();// consume the closing ] }- Specified by:
canReadin interfaceRecordReader- Returns:
true if the next operation can beread;false if the cursor is positioned at a structural separator, field name, end-container, or end-of-stream.- Throws:
IOException- If a problem occurred peeking at the underlying stream.ParseException- If the input is malformed for the format.
-
read
Consumes the upcoming value (object, array, or scalar) and binds it to a POJO via the format's existing parser.Cursor positioning: the cursor must be in a state where the next call to
next()would emit a value-shaped token (i.e.TokenType.START_OBJECT,TokenType.START_ARRAY, or one of theVALUE_*tokens). Callingreadconsumes that upcoming value wholesale and advances the cursor's depth back to where it was before the value would have started — structurally equivalent to callingnext()on a scalar, ornext()+skipChildren()on a container.Do NOT call
next()beforeread. Once the structural opener (e.g.{ or[ ) has been consumed bynext(), the underlying parser cannot re-bind the value. The contract is "instead-of-next, not after-next."Default behavior: the default implementation throws
UnsupportedOperationException. Format-specific token readers override this by delegating to their existing*ParserSessionat the cursor's current input position and resyncing the cursor's state machine afterward.- Specified by:
readin interfaceRecordReader- Type Parameters:
T- The expected return type.- Parameters:
type- The target Java type.- Returns:
- The bound POJO.
- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed for the format or cannot be bound to the type.IllegalStateException- If the cursor is not positioned at a value boundary.UnsupportedOperationException- If this format's reader has not implemented the bridge (e.g. writer-only formats with no parser, or formats whose Phase-2 increment did not include the bridge).
-
read
Same asread(Class)but accepts a pre-resolvedClassMeta.- Specified by:
readin interfaceRecordReader- Type Parameters:
T- The expected return type.- Parameters:
type- The target type.- Returns:
- The bound POJO.
- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed or cannot be bound to the type.
-
read
Same asread(Class)but accepts a parameterizedTypeand optional type arguments (the same shape asParser.read(Object, Type, Type...)).- Specified by:
readin interfaceRecordReader- Type Parameters:
T- The expected return type.- Parameters:
type- The target type.args- Optional type arguments for parameterized types.- Returns:
- The bound POJO.
- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed or cannot be bound to the type.
-
skipChildren
Skips over the children of the current structural token.If the current token is
TokenType.START_OBJECTorTokenType.START_ARRAY, the cursor is advanced to the matchingTokenType.END_OBJECTorTokenType.END_ARRAY(i.e. the entire sub-tree is consumed). For any other token this is a no-op.- Throws:
IOException- If a problem occurred reading from the underlying stream.ParseException- If the input is malformed for the format.
-
isStreaming
Structural token cursors are always O(1)-memory streamers.- Specified by:
isStreamingin interfaceRecordReader- Returns:
- Always
true .
-
close
Closes the underlying stream.Implementations should be idempotent — closing a closed reader is a no-op.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceRecordReader- Throws:
IOException
-
getNativeKind
Returns the binary-native classification of the current token.- Returns:
- The native kind, or
NONEwhen the current token carries no binary-native metadata. Nevernull.
-
getTagCount
Returns the number of semantic tags wrapping the current token.- Returns:
- The tag count (0 when
getNativeKind()is notCBOR_TAG).
-
getTag
Returns the semantic tag at the specified position in the tag stack.- Parameters:
index- The tag position; outermost tag is0.- Returns:
- The tag number.
- Throws:
IndexOutOfBoundsException- Ifindexis outside[0, getTagCount()).
-
getExtType
Returns the typed-binary type byte for the current token.- Returns:
- The signed type byte (range
-128..127). - Throws:
IllegalStateException- IfgetNativeKind()is notMSGPACK_EXT.
-
getSimpleValue
Returns the opaque simple value for the current token.- Returns:
- The simple int.
- Throws:
IllegalStateException- IfgetNativeKind()is notCBOR_SIMPLE.
-