Serde_json: Streaming Serialization of Struct

Hello,

I'm looking for a solution to serialize a struct without having to fully load it into memory.

// Current status
#[derive(Serialize)]
struct Container {
  // [various stuff]
  entries: Vec<SomeValue>
}

I'm looking for something that I can serialize to JSON where entries comes from an external source (a database, a mpsc::Receiver, Iterator, ...):

struct Container {
  // [various stuff]
  entries: mpsc::Receiver<SomeValue> 
  // or: entries: (Database, Vec<DbId>)
}

Implementing Serialize is not possible, as serialize takes &self, not allowing me to mutate anything. I also investigated driving serde_json::ser::Formatter directly, but that would mean re-implementing serde_json::Serializer which I'm trying to avoid.

Is serde_json / serde just not flexible enough for this?

This might be helpful

Please note that I have researched this issue. Your linked thread came up and doesn't solve my underlying problem: I need to consume (or at least mutate) the type I'm serialising to consume the mpsc::Receiver, fetch stuff from a db, or similar.

Would putting it in a Cell help?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.