Skip to content

Determining the byte range for the value of a particular object key #97

@Aaron1011

Description

@Aaron1011

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::StartValue event, 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 a JsonEvent::StartObject or JsonEvent::StartArray)
  • Add JsonEvent::Start* events for the first byte of other values (e.g. JsonEvent::StartTrue when a t byte is read, JsonEvent::StartString when 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 produce StartObject, FieldName, StartTrue, <error> since the t byte was not part of the sequence true).

Thanks for making such a useful crate!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions