heapless/
heapless.rs

1use justjson::doc::{HeaplessDocument, Node};
2use justjson::{ErrorKind, JsonString};
3
4fn main() {
5    // Using a heapless vec, we can parse directly to the stack.
6    let doc: HeaplessDocument<'_, 3> =
7        HeaplessDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json");
8    let mut nodes = doc.into_iter();
9    assert_eq!(nodes.next(), Some(Node::Object { length: 1 }));
10    assert_eq!(nodes.next(), Some(Node::String(JsonString::from("hello"))));
11    assert_eq!(nodes.next(), Some(Node::String(JsonString::from("world"))));
12
13    // When parsing a document too large for the heapless Vec, an error will be
14    // returned instead of panicing.
15    let error = HeaplessDocument::<3>::from_json("[1, 2, 3, 4]").expect_err("shouldn't have space");
16    assert_eq!(error.kind(), &ErrorKind::PaylodTooLarge);
17}
18
19#[test]
20fn runs() {
21    main();
22}