Skip to content

serialize::json is encouraging allocating intermediate buffers #15317

Closed
@huonw

Description

@huonw

The Encodable/Decodable interface is designed to avoid having to allocate everything to intermediate buffers, but json is encouraging going via String. serialize::json has the top level encode function returning a String, and all the examples use encode directly, there's no demonstration of the good direct-to-Writer API.

That is, something like

println!("{}", json::encode(&x))

can be written a little like

encodable.encode(&mut json::Encoder::new(&mut io::stdout()))

If that is deemed too ugly, I think we should work on fixing the ergonomics of the good version, rather than just leaping straight to the -> String ones (e.g. json::encode(&mut io::stdout(), &x) would be nice, and work with json::encode(&mut some_file, &x), json::encode(&mut some_tcp_stream, &x), etc.).

cc @aturon

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