Skip to content

Commit 572c86a

Browse files
authored
feat: ability to parse strictly as JSON (#25)
1 parent c67569a commit 572c86a

10 files changed

+236
-95
lines changed

.rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max_width = 120
2+
tab_spaces = 2

README.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ To a simple `JsonValue`:
1111
```rs
1212
use jsonc_parser::parse_to_value;
1313

14-
let json_value = parse_to_value(r#"{ "test": 5 } // test"#)?;
14+
let json_value = parse_to_value(r#"{ "test": 5 } // test"#, &Default::default())?;
1515
// check the json_value here
1616
```
1717

1818
Or an AST:
1919

2020
```rs
21-
use jsonc_parser::{parse_to_ast, ParseOptions};
21+
use jsonc_parser::parse_to_ast;
22+
use jsonc_parser::CollectOptions;
2223

23-
let parse_result = parse_to_ast(r#"{ "test": 5 } // test"#, &ParseOptions {
24+
let parse_result = parse_to_ast(r#"{ "test": 5 } // test"#, &CollectOptions {
2425
comments: true, // include comments in result
2526
tokens: true, // include tokens in result
26-
})?;
27+
}, &Default::default())?;
2728
// ...inspect parse_result for value, tokens, and comments here...
2829
```
2930

@@ -37,5 +38,20 @@ jsonc-parser = { version = "...", features = ["serde"] }
3738
```rs
3839
use jsonc_parser::parse_to_serde_value;
3940

40-
let json_value = parse_to_serde_value(r#"{ "test": 5 } // test"#)?;
41+
let json_value = parse_to_serde_value(r#"{ "test": 5 } // test"#, &Default::default())?;
42+
```
43+
44+
## Parse Strictly as JSON
45+
46+
Provide `ParseOptions` and set all the options to false:
47+
48+
```rs
49+
use jsonc_parser::parse_to_value;
50+
use jsonc_parser::ParseOptions;
51+
52+
let json_value = parse_to_value(text, &ParseOptions {
53+
allow_comments: false,
54+
allow_loose_object_property_names: false,
55+
allow_trailing_commas: false,
56+
})?;
4157
```

benches/bench.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
extern crate test;
44

5-
use jsonc_parser::{parse_to_ast, parse_to_value, ParseOptions};
5+
use jsonc_parser::parse_to_ast;
6+
use jsonc_parser::parse_to_value;
67
use std::fs::read_to_string;
78
use test::Bencher;
89

@@ -45,11 +46,11 @@ fn package_json_value(b: &mut Bencher) {
4546
// bench helpers
4647

4748
fn bench_ast(b: &mut Bencher, json_text: &str) {
48-
b.iter(|| parse_to_ast(json_text, &ParseOptions::default()).unwrap());
49+
b.iter(|| parse_to_ast(json_text, &Default::default(), &Default::default()).unwrap());
4950
}
5051

5152
fn bench_value(b: &mut Bencher, json_text: &str) {
52-
b.iter(|| parse_to_value(json_text).unwrap());
53+
b.iter(|| parse_to_value(json_text, &Default::default()).unwrap());
5354
}
5455

5556
#[cfg(feature = "serde")]

dprint.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"incremental": true,
33
"indentWidth": 2,
4-
"rustfmt": {
5-
"max_width": 120
4+
"exec": {
5+
"associations": "**/*.rs",
6+
"rustfmt": "rustfmt"
67
},
78
"includes": ["**/*.{md,rs}"],
89
"excludes": [
910
"**/target",
1011
"./benches/json"
1112
],
1213
"plugins": [
13-
"https://plugins.dprint.dev/markdown-0.12.1.wasm",
14-
"https://plugins.dprint.dev/rustfmt-0.4.0.exe-plugin@c6bb223ef6e5e87580177f6461a0ab0554ac9ea6b54f78ea7ae8bf63b14f5bc2"
14+
"https://plugins.dprint.dev/markdown-0.13.3.wasm",
15+
"https://plugins.dprint.dev/exec-0.3.1.json@9351b67ec7a6b58a69201c2834cba38cb3d191080aefc6422fb1320f03c8fc4d"
1516
]
1617
}

src/ast.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,8 @@ mod test {
443443
fn it_should_take() {
444444
let ast = parse_to_ast(
445445
"{'prop': 'asdf', 'other': 'text'}",
446-
&ParseOptions {
447-
tokens: false,
448-
comments: false,
449-
},
446+
&Default::default(),
447+
&ParseOptions::default(),
450448
)
451449
.unwrap();
452450
let mut obj = match ast.value {
@@ -469,14 +467,7 @@ mod test {
469467

470468
#[test]
471469
fn it_should_get() {
472-
let ast = parse_to_ast(
473-
"{'prop': 'asdf'}",
474-
&ParseOptions {
475-
tokens: false,
476-
comments: false,
477-
},
478-
)
479-
.unwrap();
470+
let ast = parse_to_ast("{'prop': 'asdf'}", &Default::default(), &ParseOptions::default()).unwrap();
480471
let obj = match ast.value {
481472
Some(Value::Object(obj)) => obj,
482473
_ => unreachable!(),

0 commit comments

Comments
 (0)