Skip to main content

parse

Function parse 

Source
pub fn parse(src: &str) -> Result<Value, JsonError>
Expand description

Parse a JSON source string with the shared default parser.

The engine is built once, on first use, and reused after that. Both other runtimes do the same (sync.Once in go/json.go, a lazily assigned module variable in ts/src/json.ts), and reuse is safe here for the same reason it is there: Tabnas::parse takes &self and builds a fresh parse context per call, and Tabnas is Send + Sync, so concurrent callers share one installed grammar instead of each rebuilding it. tests/json_test.rs pins that with a threaded test.

Use make instead when the parser needs configuring: that returns a fresh instance and leaves this one alone.

let value = tabnas_json::parse(r#"{"a":[1,2]}"#)?;
assert_eq!(value.to_string(), r#"{"a":[1,2]}"#);
assert_eq!(tabnas_json::parse("{a:1}").unwrap_err().code, "unexpected");