Skip to main content

const_json

Macro const_json 

Source
const_json!() { /* proc-macro */ }
Expand description

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"]}"#);
ⓘ
let name = "runtime";
let json = const_json::const_json!({"name": name});

Compile JSON literals to a validated, compact &'static str.