JSON (RFC 8259) for Fe: a document tree, a
parser and a writer. It needs a compiler built from Fe's master, and is tested
on the native backend.
[dependencies]
json = { source = "https://github.com/fe-lang/json", rev = "<commit>" }use json::{Value, parse}
let doc = match parse(text.span()) {
Result::Ok(doc) => doc
Result::Err(e) => { eprintln!(e); return 1 }
}
let complete = doc["complete"].as_bool()
let seconds = doc["checks"][0]["seconds"].as_u64()
var report = Value::object()
report.set("name", Value::string("soak"))
report.set("runs", Value::number(3 as u64))
var out = MemBuffer::empty()
report.write_pretty(mut out, indent: 2) // the layout of json.dumps(indent=2)
- Objects keep their members in source order. A repeated key stays, and lookups find the last one.
- Numbers keep their source text, so a document round-trips exactly.
as_u64,as_i64,as_u256andas_i256read integers;1.0and1e2are not. - Strings hold UTF-8. Invalid UTF-8 and lone surrogates are rejected.
- Nesting deeper than 128 is rejected, which bounds the stack.
fe test --backend native .runs the unit tests.conformance/run.py <binary> <test_parsing dir> [file...]runs JSONTestSuite and compares output with CPython'sjson, layout included. Build the binary withfe build --backend native -O 2 --emit executable --out-dir out/conformance conformance.bench/compare.pycompares speed with a line-for-line Rust port of this parser and withserde_json, usingbench/build.shandbench/fetch_corpus.sh. The port uses the same containers andmallocand keeps its bounds checks, so a difference from it is the compiler's, not the library's design.
Licensed under the Apache License, Version 2.0.