serde-rs / json

Strongly typed JSON library for Rust
Apache License 2.0
4.92k stars 562 forks source link

Support no_std #362

Closed dtolnay closed 4 years ago

dtolnay commented 7 years ago

We have been putting this off because nobody has needed it so far.

But in https://github.com/dtolnay/dtoa/issues/8:

\ I'm currently trying to use serde_json on a microcontroller

japaric commented 6 years ago

FWIW, this morning I wrote a serde-json-core crate that provides JSON (de)serialization on no_std. It's certainly not on par with the real serde-json as I don't plan to add any stuff that requires dynamic memory allocation to it, or add support for floats. I also made some adjustments to avoid having the binary size of my firmware increase by several KBs. Anyway, you can check the documentation for details about what works and what doesn't work.

Also, I don't plan to send a PR to serde-json as I doubt all my changes are going to be welcomed, but anyone else is free to try.

cc @pierre-rouanet @perlindgren @MrBuddyCasino

dtolnay commented 6 years ago

Nice! I am happy to have @japaric own a separate no_std JSON library. The API is sufficiently different that I think both std and no_std users will be better served if we keep these as separate libraries. I have retagged this issue as a docs issue to drop a link to serde-json-core in our readme and rustdoc.

dtolnay commented 6 years ago

I added a link to serde-json-core in our readme and rustdoc, as well as https://serde.rs/no-std.html. Thanks!

dtolnay commented 6 years ago

Reopening because there continues not to be a no_std JSON crate that can support types from alloc (String, Vec). https://github.com/japaric/serde-json-core/issues/4 I would be willing to consider a PR that adds a std feature on by default, and exposes the same or a subset of the current API of serde_json by using the alloc crate in no_std mode.

geofft commented 5 years ago

I gave this a shot for fishinabarrel/linux-kernel-module-rust because we support allocation just fine, and serde-json-core doesn't support either dynamic types (like serializing/deserializing a Vec) or nifty things like JSON Pointer. (Also the "heapless" stuff, while it sounds fantastic for environments that need it, is frustratingly unergonomic for environments where normal vectors and strings work.)

Unfortunately it looks like the bulk of src/ser.rs is implemented via the std::io::Write trait, and even serialization to a string relies on impl Write for Vec<u8> and casting that to a string, and std::io isn't available in a standalone crate.

A couple of options that occur to me:

I'm leaning towards the last option n fishinabarrel/linux-kernel-module-rust#121, but that's pretty involved.... but if other people are interested in that approach, then it would make more sense to go down that road. (Also, because of issues with build-dependencies and no-std described in that ticket, we probably couldn't use serde-json even if there were a working no-std version, until that Cargo bug gets fixed too.)

Xanewok commented 4 years ago

This should be addressed by https://github.com/serde-rs/json/pull/606 now (I'm sorry, I forgot to add a relevant Closes directive in the PR)

geofft commented 4 years ago

Wow, thank you @Xanewok! I'm excited to try it out!