Skip to main content

serde_json_core/
lib.rs

1//! [`serde-json`] for `no_std` programs
2//!
3//! [`serde-json`]: https://crates.io/crates/serde_json
4//!
5//! This version of [`serde-json`] is aimed at applications that run on resource constrained
6//! devices.
7//!
8//! ## Example
9//! ```
10//! # use serde::{Serialize, Deserialize};
11//! #[derive(Serialize, Deserialize)]
12//! struct Data<'a> {
13//!     value: u32,
14//!     message: &'a str,
15//! }
16//!
17//! // Serialized JSON data can be easily deserialized into Rust types.
18//! let message = b"{\"value\":10,\"message\":\"Hello, World!\"}";
19//! let (data, _consumed) = serde_json_core::from_slice::<Data<'_>>(message).unwrap();
20//! assert_eq!(data.value, 10);
21//! assert_eq!(data.message, "Hello, World!");
22//!
23//! // Structures can also be serialized into slices or strings.
24//! let mut deserialized = [0u8; 256];
25//! let len = serde_json_core::to_slice(&data, &mut deserialized[..]).unwrap();
26//! assert_eq!(&deserialized[..len], message);
27//! ```
28//!
29//! # Current features
30//!
31//! - The error type is a simple C like enum (less overhead, smaller memory footprint)
32//! - (De)serialization doesn't require memory allocations
33//! - Deserialization of integers doesn't go through `u64`; instead the string is directly parsed
34//!   into the requested integer type. This avoids pulling in KBs of compiler intrinsics when
35//!   targeting a non 64-bit architecture.
36//! - Supports deserialization of:
37//!   - `bool`
38//!   - Integers
39//!   - Floats
40//!   - `str` (This is a zero copy operation when deserializing without de-escaping strings.)
41//!   - `Option`
42//!   - Arrays
43//!   - Tuples
44//!   - Structs
45//!   - C like enums
46//! - Supports serialization (compact format only) of:
47//!   - `bool`
48//!   - Integers
49//!   - Floats
50//!   - `str`
51//!   - `Option`
52//!   - Arrays
53//!   - Tuples
54//!   - Structs
55//!   - C like enums
56//!
57//! # Planned features
58//!
59//! - (De)serialization from / into IO objects once `core::io::{Read,Write}` becomes a thing.
60//!
61//! # Non-features
62//!
63//! This is explicitly out of scope
64//!
65//! - Anything that involves dynamic memory allocation
66//!   - Like the dynamic [`Value`](https://docs.rs/serde_json/1.0.11/serde_json/enum.Value.html)
67//!     type
68//!
69//! # Minimum Supported Rust Version (MSRV)
70//!
71//! This crate is guaranteed to compile on stable Rust 1.70.0 and up. It *might* compile with older
72//! versions but that may change in any new patch release.
73
74#![deny(missing_docs)]
75#![deny(rust_2018_compatibility)]
76#![deny(rust_2018_idioms)]
77#![deny(warnings)]
78#![cfg_attr(not(feature = "std"), no_std)]
79
80pub mod de;
81pub mod ser;
82pub mod str;
83
84#[doc(inline)]
85pub use self::de::{from_slice, from_slice_escaped, from_str, from_str_escaped};
86#[doc(inline)]
87pub use self::ser::to_slice;
88#[cfg(feature = "heapless")]
89pub use self::ser::{to_string, to_vec};
90
91#[cfg(feature = "heapless")]
92pub use heapless;