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, _remainder) = 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//! (\*\*) Serialization of strings doesn't escape stuff. This simply has not been implemented yet.
58//!
59//! # Planned features
60//!
61//! - (De)serialization from / into IO objects once `core::io::{Read,Write}` becomes a thing.
62//!
63//! # Non-features
64//!
65//! This is explicitly out of scope
66//!
67//! - Anything that involves dynamic memory allocation
68//! - Like the dynamic [`Value`](https://docs.rs/serde_json/1.0.11/serde_json/enum.Value.html)
69//! type
70//!
71//! # Minimum Supported Rust Version (MSRV)
72//!
73//! This crate is guaranteed to compile on stable Rust 1.65.0 and up. It *might* compile with older
74//! versions but that may change in any new patch release.
75
76#![deny(missing_docs)]
77#![deny(rust_2018_compatibility)]
78#![deny(rust_2018_idioms)]
79#![deny(warnings)]
80#![cfg_attr(not(feature = "std"), no_std)]
81
82pub mod de;
83pub mod ser;
84pub mod str;
85
86#[doc(inline)]
87pub use self::de::{from_slice, from_slice_escaped, from_str, from_str_escaped};
88#[doc(inline)]
89pub use self::ser::to_slice;
90#[cfg(feature = "heapless")]
91pub use self::ser::{to_string, to_vec};
92
93#[cfg(feature = "heapless")]
94pub use heapless;