Skip to content
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

Support no_std #362

Closed
dtolnay opened this issue Oct 1, 2017 · 7 comments
Closed

Support no_std #362

dtolnay opened this issue Oct 1, 2017 · 7 comments

Comments

@dtolnay
Copy link
Member

dtolnay commented Oct 1, 2017

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

But in dtolnay/dtoa#8:

<pierre-rouanet> I'm currently trying to use serde_json on a microcontroller

cuviper added a commit to cuviper/json that referenced this issue Feb 10, 2018
It now has a `std` features, and uses `#![no_std]` otherwise.

cc serde-rs#362
cuviper added a commit to cuviper/json that referenced this issue Feb 10, 2018
It now has a `std` feature, and uses `#![no_std]` otherwise.

cc serde-rs#362
@japaric
Copy link
Contributor

japaric commented Mar 17, 2018

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 dtolnay added docs and removed enhancement labels Mar 17, 2018
@dtolnay
Copy link
Member Author

dtolnay commented Mar 17, 2018

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
Copy link
Member Author

dtolnay commented Mar 24, 2018

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

@dtolnay
Copy link
Member Author

dtolnay commented Oct 18, 2018

Reopening because there continues not to be a no_std JSON crate that can support types from alloc (String, Vec). rust-embedded-community/serde-json-core#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
Copy link

geofft commented Aug 18, 2019

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:

  • Rewrite this support to not rely on Write.
  • Use an alternative std::io-like crate which can map to both std::io and no-std environments, like https://crates.io/crates/genio (I'm not sure what the state of the art is here).
  • Lobby the Rust developers to spin out std::io into its own stable crate the way alloc is its own crate.
  • Give up on making no-std work for this use case and figure out how to build std in a minimal way for freestanding environments, by returning an error for all the things that actually need OS interfaces but letting traits and in-memory stuff work.

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
Copy link
Contributor

Xanewok commented Jan 22, 2020

This should be addressed by #606 now (I'm sorry, I forgot to add a relevant Closes directive in the PR)

@dtolnay dtolnay closed this as completed Jan 22, 2020
@geofft
Copy link

geofft commented Jan 23, 2020

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

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

No branches or pull requests

4 participants