-
Notifications
You must be signed in to change notification settings - Fork 6
Description
I'm trying to use actson to extract a value from a stream of bytes that looks like:
{"irrelevant-key": "irrelevant-value", "important-key": {"something": "important"}}My goal is to be able to find important-key in the top-level object, and determine which bytes in my stream correspond to the value. Concretely, I want to be able to write just those bytes to an output stream as I receive new bytes in the input stream, without needing to wait for the full value to be read in. In the above example, this would mean writing {"something": "important"} to the output stream (without the whitespace or colon after important-key.
This is almost possible with the current API - I can detect important-key with JsonEvent::FieldName and current_str, and use parsed_bytes to determine where important-key ends. However, there doesn't seem to be a way to detect the start of an arbitrary value (while excluding the colon and whitespace) - if my object contains "important_key": "string_value" or "important_key": 123, I can't determine the start byte of the value, as I'll only get a JsonEvent::Value* event once the entire value has been fed into the parser.
I had a few ideas for an API that would allow extracting this information:
- Add a
JsonEvent::StartValueevent, which would indicate when the first byte of an object value has been read (e.g. a{,[,", or leading digit of a number). However, this would mean emitting multiple events for the same byte (as it could be immediately followed by aJsonEvent::StartObjectorJsonEvent::StartArray) - Add
JsonEvent::Start*events for the first byte of other values (e.g.JsonEvent::StartTruewhen atbyte is read,JsonEvent::StartStringwhen a"byte is read). Unlike the other events, these wouldn't necessarily correspond to a valid value (e.g. the stream{"my-key": tabcd}would produceStartObject,FieldName,StartTrue,<error>since thetbyte was not part of the sequencetrue).
Thanks for making such a useful crate!