Skip to main content

Crate json_cursor

Crate json_cursor 

Source
Expand description

Read JSON one value at a time: fast, strict, and with every number exact.

A Cursor walks through a JSON document from start to end. You ask for the next key, string or number, and it hands it to you straight from your bytes, so you can build your own types with no tree in between.

use json_cursor::Cursor;

let mut cursor = Cursor::new(r#"{"name": "Ada", "born": 1815, "languages": ["English"]}"#);
let mut buf = String::new(); // used only when a key or string has escapes

let mut next = cursor.first_key(&mut buf)?;
while let Some(key) = next {
    match key {
        "name" => assert_eq!(cursor.string(&mut buf)?, "Ada"),
        "born" => assert_eq!(cursor.number()?.as_i64(), Some(1815)),
        _ => cursor.skip()?, // anything else is checked, then passed over
    }
    next = cursor.next_key(&mut buf)?;
}
cursor.finish()?; // only whitespace may follow
  • Cursor reads a document one value at a time.
  • Number keeps a number’s text exactly, and gives its value as an i64 or an f64.
  • validate checks a whole document in one call.
  • Error says what went wrong, and at which byte.

The README has a guide, and the benchmarks against other parsers.

Structs§

Cursor
A strict JSON pull cursor over text.
Error
Why the bytes are not JSON, and the byte where the cursor found out.
Number
A number as the cursor read it.

Enums§

ErrorKind
Why the bytes are not JSON.
Kind
What kind of value is next, from its first byte.
Value
The next value, as Cursor::value reads it.

Constants§

DEFAULT_DEPTH_LIMIT
How deep Cursor::skip goes into arrays and objects, unless set with Cursor::with_depth_limit: serde_json’s limit.

Functions§

validate
Checks that bytes hold one JSON value, with nothing but whitespace around it.