spanned_json_parser/
lib.rs

1#![forbid(unsafe_code)]
2#![warn(clippy::all)]
3#![allow(clippy::needless_doctest_main)]
4//! This crate is a json parser that will return span information for values, which mean lines and column number. It is also compatible with [serde](https://serde.rs/) so you can serialize it to any other struct that implements [Deserialize](https://docs.rs/serde/latest/serde/de/trait.Deserialize.html)
5//!
6//! ## Why use it ?
7//!
8//! One of the main use case is to do validation after parsing. By having the line and col number, you can tell really precisely to user where a value is invalid
9//!
10//! ## How to use it ?
11//!
12//! The crate expose a `Value` that is similar to [serde](https://docs.rs/serde_json/latest/serde_json/value/enum.Value.html), and wraps everything into this struct:
13//!
14//! ```ignore
15//! pub struct Position {
16//!     pub col: usize,
17//!     pub line: usize,
18//! }
19//!
20//! pub struct SpannedValue {
21//!     pub value: Value,
22//!     pub start: Position,
23//!     pub end: Position,
24//! }
25//! ```
26//!
27//! ### Parsing
28//!
29//! ```ignore
30//! use spanned_json_parse::parse;
31//! use std::fs;
32//!
33//! fn main() {
34//!     let json = fs::read_to_string("path").unwrap();
35//!
36//!     let parsed = parse(&json);
37//!
38//!     println!("Parsed: {:#?}", parsed);
39//! }
40//! ```
41//!
42//! ### Serializing in a struct
43//!
44//! ```ignore
45//! use serde::Deserialize;
46//! use spanned_json_parser::parse;
47//!
48//! #[derive(Deserialize)]
49//! struct Test {
50//!     pub hello: String,
51//! }
52//!
53//! fn main() {
54//!     let json = r#"{"hello": "world"}"#;
55//!
56//!     let parsed = parse(json).unwrap();
57//!
58//!     let test: Test = serde_json::from_value(serde_json::to_value(parsed).unwrap()).unwrap();
59//!
60//!     println!("Test hello: {}", test.hello);
61//! }
62//! ```
63//!
64//! ## Performance
65//!
66//! Here are the outputs of the benchmark. Everything was tested on a Macbook Pro M1, so keep in mind that this numbers are here to give you an idea of the performance, but might not be representative of the reality:
67//!
68//! ```ignore
69//! Parser ./benches/data/twitter.json
70//! time:   [10.220 ms 10.279 ms 10.334 ms]
71//! thrpt:  [58.280 MiB/s 58.589 MiB/s 58.932 MiB/s]
72//!
73//! Parser ./benches/data/citm_catalog.json
74//! time:   [18.204 ms 18.281 ms 18.353 ms]
75//! thrpt:  [89.752 MiB/s 90.102 MiB/s 90.486 MiB/s]
76//!
77//! Parser ./benches/data/canada.json
78//! time:   [42.026 ms 42.188 ms 42.341 ms]
79//! thrpt:  [50.702 MiB/s 50.886 MiB/s 51.082 MiB/s]
80//! ```
81
82extern crate bytecount;
83extern crate memchr;
84extern crate nom;
85extern crate serde;
86
87mod input;
88mod parser;
89mod ser;
90
91pub mod error;
92pub mod value;
93
94pub use parser::parse;
95pub use value::*;