Skip to content

Conversation

chriso
Copy link
Contributor

@chriso chriso commented Oct 7, 2022

There's no way to determine how far into the input buffer a json.Tokenizer is.

This is useful information to have. For example, you might want to measure progress as you walk through a large JSON object. You might want to mark positions in the buffer as you traverse nested objects/arrays, so that you can slice a json.RawMessage from the buffer and defer parsing until later.

I originally wanted a Tell() int or Pos() int function that returned the current position into the input buffer, but then realized that the tokenizer progressively consumes its buffer and this information is lost. Rather than track this information and increase the size of the tokenizer, I opted to create a Remaining() int function which returns the number of bytes still to consume. The position into the buffer can be derived by the caller, which presumably still has access to the whole input buffer, by taking len(buf) - tokenizer.Remaining().

@chriso chriso changed the title Bytes remaining in Tokenizer json: bytes remaining in Tokenizer Oct 7, 2022
@chriso chriso merged commit 4601a45 into master Oct 7, 2022
@chriso chriso deleted the json-tokenizer-bytes-remaining branch October 7, 2022 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants