-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding no-std to readme, added HeaplessDocument
- Loading branch information
Showing
6 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
use heapless::Vec; | ||
use justjson::doc::{GenericDocument, Node}; | ||
use justjson::doc::{HeaplessDocument, Node}; | ||
use justjson::{ErrorKind, JsonString}; | ||
|
||
fn main() { | ||
// Using a heapless vec, we can parse directly to the stack. | ||
let doc: GenericDocument<Vec<Node<'_>, 3>> = | ||
GenericDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json"); | ||
let doc: HeaplessDocument<'_, 3> = | ||
HeaplessDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json"); | ||
let mut nodes = doc.into_iter(); | ||
assert_eq!(nodes.next(), Some(Node::Object { length: 1 })); | ||
assert_eq!(nodes.next(), Some(Node::String(JsonString::from("hello")))); | ||
assert_eq!(nodes.next(), Some(Node::String(JsonString::from("world")))); | ||
|
||
// When parsing a document too large for the heapless Vec, an error will be | ||
// returned instead of panicing. | ||
let error = GenericDocument::<Vec<Node<'_>, 3>>::from_json("[1, 2, 3, 4]") | ||
.expect_err("shouldn't have space"); | ||
let error = HeaplessDocument::<3>::from_json("[1, 2, 3, 4]").expect_err("shouldn't have space"); | ||
assert_eq!(error.kind(), &ErrorKind::PaylodTooLarge); | ||
} | ||
|
||
#[test] | ||
fn runs() { | ||
main(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters