-
Notifications
You must be signed in to change notification settings - Fork 68
Open
Description
I know this is would be a partial rewrite of this crate, but utilizing embedded_io::Write
as the target in Serializer
instead of a fixed buffer would allow us to stream json data without any allocation.
It would still be backwards compatible with raw buffers, as &[u8]
implements embedded_io::Write
.
Any reasons against this?
As an alternative, we could use a &mut fmt::Formatter<'_>
as target inside of Serializer
, which would be possible without any external crates, and then use embedded_io::Write
only inside of the to_slice()
method.
Something like this:
/// Thin wrapper around a given data structure that produces JSON data when passed into
/// a [`core::write`]-like macro or through [`core::fmt::Display`].
#[derive(Debug)]
pub struct JsonWriter<'a, T: ?Sized>(&'a T);
/// Wraps the given data structure into a writer that can be used to generate JSON strings
/// through [`core::write`] or [`core::fmt::Display`].
pub fn to_writer<T>(value: &T) -> JsonWriter<'_, T>
where
T: ser::Serialize + ?Sized,
{
JsonWriter(value)
}
impl<T: ser::Serialize + ?Sized> core::fmt::Display for JsonWriter<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ser = Serializer { f };
self.0.serialize(&mut ser)
}
}
Our implementation of to_slice()
could then use embedded_io::Write::write_fmt
to write the JSON data through Display
to the slice.
I'm interested in working on this, if nobody objects.
Metadata
Metadata
Assignees
Labels
No labels