Skip to main content

const_json/
lib.rs

1// Copyright Proa Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4#![no_std]
5
6//! Const-only JSON serialization and parsing runtime helpers.
7//!
8//! This crate pairs with the `ConstJson` derive macro to generate
9//! fully `const fn` JSON serialization with zero heap allocation.
10//!
11//! It also provides compile-time JSON parsing via `const fn`.
12
13pub use const_json_macros::ConstJson;
14
15mod buffer;
16pub mod parse;
17pub mod ser;
18
19pub use buffer::JsonBuffer;
20pub use parse::{JsonObject, JsonValue, ParseError};
21
22#[cfg(test)]
23extern crate std;
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use crate as const_json;
29
30    #[derive(ConstJson)]
31    #[const_json(capacity = 64)]
32    struct Example<'a> {
33        title: &'a str,
34        count: u32,
35        enabled: bool,
36    }
37
38    const fn build_example() -> JsonBuffer<64> {
39        Example {
40            title: "example",
41            count: 42,
42            enabled: true,
43        }
44        .to_json()
45    }
46
47    #[test]
48    fn generates_expected_json() {
49        const BUF: JsonBuffer<64> = build_example();
50        assert_eq!(
51            BUF.as_str(),
52            r#"{"title":"example","count":42,"enabled":true}"#
53        );
54    }
55}