Package io.micronaut.http.netty.body
Class JsonCounter
java.lang.Object
io.micronaut.http.netty.body.JsonCounter
This class takes in JSON data and does simple parsing to detect boundaries between json nodes.
For example, this class can recognize the separation between the two JSON objects in
Public for fuzzing.
{"foo":"bar"} {"bar":"baz"}
.Public for fuzzing.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
A region that contains a JSON node. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionlong
If we arebuffering
, the startposition()
of the region that is being buffered.void
feed
(io.netty.buffer.ByteBuf buf) Parse some input data.boolean
Whether we are currently in the buffering state, i.e.void
Do not perform any tokenization, assume that there is only one root-level value.Check for any new flushed data from the lastfeed(ByteBuf)
operation.long
position()
The current position counter of the parser.void
Enable top-level array unwrapping: If the input starts with an array, that array's elements are returned as individual JSON nodes, not the array all at once.
-
Constructor Details
-
JsonCounter
public JsonCounter()
-
-
Method Details
-
feed
Parse some input data. Ifbuf
is readable, this method always advances (always consumes at least one byte).- Parameters:
buf
- The input buffer- Throws:
JsonSyntaxException
- If there is a syntax error in the JSON. Note that not all syntax errors are detected by this class.
-
unwrapTopLevelArray
public void unwrapTopLevelArray()Enable top-level array unwrapping: If the input starts with an array, that array's elements are returned as individual JSON nodes, not the array all at once.
Must be called before any data is processed, but can be called afternoTokenization()
. -
noTokenization
public void noTokenization()Do not perform any tokenization, assume that there is only one root-level value. There is still some basic validation (ensuring the input isn't utf-16 or utf-32). -
pollFlushedRegion
Check for any new flushed data from the lastfeed(ByteBuf)
operation.- Returns:
- The region that contains a JSON node, relative to
position()
, ornull
if the JSON node has not completed yet.
-
position
public long position()The current position counter of the parser. Increases by exactly one for each byte consumed byfeed(io.netty.buffer.ByteBuf)
.- Returns:
- The current position
-
isBuffering
public boolean isBuffering()Whether we are currently in the buffering state, i.e. there is a JSON node, but it's not done yet or we can't know for sure that it's done (e.g. for numbers). This is used to flush any remaining buffering data when EOF is reached.- Returns:
true
if we are currently buffering
-
bufferStart
public long bufferStart()If we arebuffering
, the startposition()
of the region that is being buffered.- Returns:
- The buffer region start
- Throws:
IllegalStateException
- if we aren't buffering
-