jsonc_parser/lib.rs
1//! # jsonc-parser
2//!
3//! A JSON parser and manipulator that supports comments and other JSON extensions.
4//!
5//! ## Parsing
6//!
7//! To a simple `JsonValue`:
8//!
9//! ```
10//! use jsonc_parser::parse_to_value;
11//!
12//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
13//! let json_value = parse_to_value(r#"{ "test": 5 } // test"#, &Default::default())?;
14//! // check the json_value here
15//! # Ok(())
16//! # }
17//! ```
18//!
19//! Or an AST:
20//!
21//! ```
22//! use jsonc_parser::parse_to_ast;
23//! use jsonc_parser::CollectOptions;
24//! use jsonc_parser::CommentCollectionStrategy;
25//!
26//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
27//! let parse_result = parse_to_ast(r#"{ "test": 5 } // test"#, &CollectOptions {
28//! comments: CommentCollectionStrategy::Separate, // include comments in result
29//! tokens: true, // include tokens in result
30//! }, &Default::default())?;
31//! // ...inspect parse_result for value, tokens, and comments here...
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! ## Manipulation (CST)
37//!
38//! When enabling the `cst` cargo feature, parsing to a CST provides a first class manipulation API:
39//!
40//! ```
41//! # #[cfg(feature = "cst")]
42//! # {
43//! use jsonc_parser::cst::CstRootNode;
44//! use jsonc_parser::ParseOptions;
45//! use jsonc_parser::json;
46//!
47//! let json_text = r#"{
48//! // comment
49//! "data": 123
50//! }"#;
51//!
52//! let root = CstRootNode::parse(json_text, &ParseOptions::default()).unwrap();
53//! let root_obj = root.object_value_or_set();
54//!
55//! root_obj.get("data").unwrap().set_value(json!({
56//! "nested": true
57//! }));
58//! root_obj.append("new_key", json!([456, 789, false]));
59//!
60//! assert_eq!(root.to_string(), r#"{
61//! // comment
62//! "data": {
63//! "nested": true
64//! },
65//! "new_key": [456, 789, false]
66//! }"#);
67//! # }
68//! ```
69//!
70//! ## Serde
71//!
72//! If you enable the `"serde"` feature as follows:
73//!
74//! ```toml
75//! # in Cargo.toml
76//! jsonc-parser = { version = "...", features = ["serde"] }
77//! ```
78//!
79//! Then you can use the `parse_to_serde_value` function to deserialize JSONC directly into
80//! any type implementing `serde::Deserialize`:
81//!
82//! ```
83//! # #[cfg(feature = "serde")]
84//! # {
85//! use jsonc_parser::parse_to_serde_value;
86//!
87//! #[derive(serde::Deserialize)]
88//! struct Config {
89//! test: u32,
90//! }
91//!
92//! # fn parse_example() -> Result<(), Box<dyn std::error::Error>> {
93//! let config: Config = parse_to_serde_value(r#"{ "test": 5 } // test"#, &Default::default())?;
94//! # Ok(())
95//! # }
96//! # }
97//! ```
98//!
99//! ## Parse Strictly as JSON
100//!
101//! By default this library is extremely loose in what it allows parsing. To be strict,
102//! provide `ParseOptions` and set all the options to false:
103//!
104//! ```
105//! use jsonc_parser::parse_to_value;
106//! use jsonc_parser::ParseOptions;
107//!
108//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
109//! # let text = "{}";
110//! let json_value = parse_to_value(text, &ParseOptions {
111//! allow_comments: false,
112//! allow_loose_object_property_names: false,
113//! allow_trailing_commas: false,
114//! allow_missing_commas: false,
115//! allow_single_quoted_strings: false,
116//! allow_hexadecimal_numbers: false,
117//! allow_unary_plus_numbers: false,
118//! })?;
119//! # Ok(())
120//! # }
121//! ```
122//!
123//! ## Error column number with unicode-width
124//!
125//! To get more accurate display column numbers in error messages, enable the `error_unicode_width` cargo feature,
126//! which will pull in and use the [unicode-width](https://crates.io/crates/unicode-width) dependency internally.
127//! Otherwise it will use the character count, which isn't as accurate of a number, but will probably be good enough
128//! in most cases.
129
130#![deny(clippy::print_stderr)]
131#![deny(clippy::print_stdout)]
132#![allow(clippy::uninlined_format_args)]
133
134pub mod ast;
135pub mod common;
136#[cfg(feature = "cst")]
137pub mod cst;
138pub mod errors;
139mod parse_to_ast;
140mod parse_to_value;
141mod parser;
142mod scanner;
143#[cfg(feature = "serde")]
144mod serde;
145mod string;
146pub mod tokens;
147mod value;
148
149pub use parse_to_ast::*;
150pub use parse_to_value::*;
151pub use scanner::*;
152pub use string::ParseStringErrorKind;
153pub use value::*;
154
155#[cfg(feature = "serde")]
156pub use serde::*;