Crate turbo_json_checker

Source
Expand description

turbo-json-checker is a library that provides JSON validation without keeping the stream of bytes in memory, it streams the bytes and validate it on the fly using a pushdown automaton.

It returns the root type of the json (Array, Object, String, …), followed by the index of its first and last non whitespace character (ex: (Array, 1, 12)).

This library is a fork of oxidized-json-checker which is itself an improvement of the json.org checker.

§Example: validate some bytes

This example shows how you can give the library a simple slice of bytes and validate that it is a valid JSON document.

// index:     0                                        41
//            |                                        |
//            v                                        v
let text = r#"["I", "am", "a", "valid", "JSON", "array"]"#;
let bytes = text.as_bytes();

let (json_type, start, end) = turbo_json_checker::validate(bytes)?;

assert_eq!(json_type, turbo_json_checker::JsonType::Array);
assert_eq!(start, 0);
assert_eq!(end, 41);

§Example: validate a stream of bytes

This example shows that you can use any type that implements io::Read to the JsonChecker and validate that it is valid JSON.

let stream = streaming_from_the_web()?;

turbo_json_checker::validate(stream)?;

§Example: complex compositions

This example show how you can use the JsonChecker type to check a compressed stream of bytes.

You can decompress the stream, check it using the JsonChecker, and compress it again to pipe it elsewhere. All of that without much memory impact.

use std::io;
use turbo_json_checker::JsonChecker;

let stdin = io::stdin();
let stdout = io::stdout();

// Wrap the stdin reader in a Snappy reader
// then wrap it in a JsonChecker reader.
let rdr = snap::read::FrameDecoder::new(stdin.lock());
let mut rdr = JsonChecker::new(rdr);

// Wrap the stdout writer in a Snappy writer.
let mut wtr = snap::write::FrameEncoder::new(stdout.lock());

// The copy function will return any io error thrown by any of the reader,
// the JsonChecker throw errors when invalid JSON is encountered.
io::copy(&mut rdr, &mut wtr)?;

// We must check that the final bytes were valid.
rdr.finish()?;

Structs§

JsonChecker
The JsonChecker is a io::Read adapter, it can be used like a pipe, reading bytes, checkings those and output the same bytes.

Enums§

Error
The error type returned by the JsonChecker type.
JsonType
Represents any valid JSON type.

Functions§

validate
A convenient method to check and consume JSON from a stream of bytes.
validate_bytes
A convenient method to check and consume JSON from a bytes slice.
validate_str
A convenient method to check and consume JSON from an str.