jsonc-to-json
Simple library for converting JSON with Comments into JSON, in short it removes the following:
- Line comments, e.g.
// Line Comment
- Block comments, e.g.
/* Block Comment */
- Trailing commas, e.g.
[1,2,3,,]
->[1,2,3]
Note: The implementation does not use a full-blown JSON with Comments parser. Instead it uses a JSON with Comments tokenizer, which makes conversion a lot faster.
Currently #![no_std]
is not supported. It will however be added, when
some upstream changes have been applied.
See jsonc_to_json()
for more information.
Example
use ;
let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";
let json = jsonc_to_json;
println!;
// Alternatively, use `jsonc_to_json_into()` to reuse an
// already allocated `String`
let mut json = String new;
jsonc_to_json_into;
println!;
Both output the following:
{"arr": [1, 2, 3, 4]}
Serde Example
use ;
use Deserialize;
let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";
let json = jsonc_to_json;
let data: Data = from_str?;
println!;
println!;
Which outputs the following:
{"arr": [1, 2, 3, 4]}
Data { arr: [1, 2, 3, 4] }
Non-Allocating & Zero-Copy Iterator Example
Non-allocating Iterator
that yields string slices of
valid JSON.
use jsonc_to_json_iter;
let jsonc = r#"{foo}/**/[1,2,3,,]"bar""#;
let mut iter = jsonc_to_json_iter;
assert_eq!; // Line comment was removed
assert_eq!; // Trailing commas was removed
assert_eq!;
assert_eq!;