Expand description
oxidized-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.
The original library has been retrieved from json.org and improved to accept every valid JSON element has a valid JSOn document.
Therefore this library accepts a single string or single integer as a valid JSON document,
this way we follow the serde_json
rules.
§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.
let text = r#"["I", "am", "a", "valid", "JSON", "array"]"#;
let bytes = text.as_bytes();
oxidized_json_checker::validate(bytes)?;
§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()?;
oxidized_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 oxidized_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§
- Json
Checker - The
JsonChecker
is aio::Read
adapter, it can be used like a pipe, reading bytes, checkings those and output the same bytes.
Enums§
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
.