proa_const_json 0.1.3

Const-evaluable JSON serialization and parsing with derive support.
Documentation
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#![no_std]

//! Const-only JSON serialization and parsing runtime helpers.
//!
//! This crate pairs with the `ConstJson` derive macro to generate
//! fully `const fn` JSON serialization with zero heap allocation.
//!
//! It also provides compile-time JSON parsing via `const fn`.

/// Serialize literal JSON at compile time, returning a compact `&'static str`.
///
/// Supports objects, arrays, strings, JSON numbers, booleans, and null. Object
/// keys must be string literals. Runtime expressions and duplicate keys are
/// rejected; use `ConstJson` or a runtime serializer for typed values instead.
/// Rust raw strings, comments, and trailing commas are accepted in the input.
/// Object key order depends on the serializer configuration.
/// Integers must fit `i64`/`u64`; floating-point literals must fit finite `f64`.
///
/// ```
/// use const_json::const_json;
/// const SCHEMA: &str = const_json!({"required": ["name"]});
/// assert_eq!(SCHEMA, r#"{"required":["name"]}"#);
/// ```
///
/// ```compile_fail
/// let name = "runtime";
/// let json = const_json::const_json!({"name": name});
/// ```
pub use const_json_macros::const_json;
pub use const_json_macros::ConstJson;

mod buffer;
pub mod parse;
pub mod ser;

pub use buffer::JsonBuffer;
pub use parse::{JsonObject, JsonValue, ParseError};

#[cfg(test)]
extern crate std;

#[cfg(test)]
mod tests {
    use super::*;
    use crate as const_json;

    #[derive(ConstJson)]
    #[const_json(capacity = 64)]
    struct Example<'a> {
        title: &'a str,
        count: u32,
        enabled: bool,
    }

    const fn build_example() -> JsonBuffer<64> {
        Example {
            title: "example",
            count: 42,
            enabled: true,
        }
        .to_json()
    }

    #[test]
    fn generates_expected_json() {
        const BUF: JsonBuffer<64> = build_example();
        assert_eq!(
            BUF.as_str(),
            r#"{"title":"example","count":42,"enabled":true}"#
        );
    }
}