Skip to content

Allow deserializing either Box<RawValue> or &'de RawValue #1267

Open
@conradludgate

Description

@conradludgate

I've been working on a system to imitate some basic jq functionality using serde and serde_json. One of my design goals is to do as much with streaming as possible.

Some of the filters require multiple passes over the json. For instance, select(.foo != "bar") | .baz first needs to apply an extractor to get .foo and compare it to "bar". It then applies another extractor to the same json object to extract .baz. To achieve this I'm using RawValue and deserializing twice with two different seeds, one which returns a boolean (.foo != "bar"), then the other which passes the value down the chain (.baz).

I initially used &'de RawValue to make my life easier, working with input json read fully into memory. When switching to json from stdin, I was surprised with an obscure error, which I figured out is because I couldn't get the &'de lifetime.

Switching to Box<RawValue> worked, but is suboptimal if the input was shareable. It would be nice if I didn't have to think about it and I could just get a Cow<'de, RawValue> through some means - maybe via a CowRawValue seed.


Adjacently, I often don't need to own the boxed value, just borrow it, so I've had to develop a seed that allows running an internal DeserializeSeed with either &'de RawValue or an Unborrow<'a, &'a RawValue> Deserializer<'de> impl. I'm not happy with the API and I don't see a good way to expose it in serde_json, but I thought I would share it anyway for inspiration:

https://github.com/conradludgate/serde-path/blob/28013e3d13676887bb850cbceac3c84511623b5a/src/raw.rs#L8-L84

https://github.com/conradludgate/serde-path/blob/main/src/borrow.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions