Skip to content

serialize::json is encouraging allocating intermediate buffers #15317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huonw opened this issue Jul 1, 2014 · 1 comment
Closed

serialize::json is encouraging allocating intermediate buffers #15317

huonw opened this issue Jul 1, 2014 · 1 comment

Comments

@huonw
Copy link
Member

huonw commented Jul 1, 2014

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

@huonw huonw added the A-libs label Jul 1, 2014
@huonw huonw changed the title serialize::json is strongly "encouraging" allocating intermediate buffers serialize::json is encouraging allocating intermediate buffers Jul 3, 2014
@steveklabnik
Copy link
Member

This issue has been moved to the rustc-serialize repo: rust-lang-deprecated/rustc-serialize#57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants