1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
//! 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
//!
//! ```rust
//! use jsonc_to_json::{jsonc_to_json, jsonc_to_json_into};
//!
//! let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";
//!
//! let json = jsonc_to_json(jsonc);
//! println!("{}", json);
//! # assert_eq!(json, "{\"arr\": [1, 2, 3, 4]}");
//!
//! // Alternatively, use `jsonc_to_json_into()` to reuse an
//! // already allocated `String`
//! let mut json = String::new();
//! jsonc_to_json_into(jsonc, &mut json);
//! println!("{}", json);
//! # assert_eq!(json, "{\"arr\": [1, 2, 3, 4]}");
//! ```
//!
//! Both output the following:
//!
//! ```text
//! {"arr": [1, 2, 3, 4]}
//! ```
//!
//! # Serde Example
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use jsonc_to_json::{jsonc_to_json, jsonc_to_json_into};
//! use serde::Deserialize;
//!
//! #[derive(Deserialize, Debug)]
//! # #[derive(PartialEq)]
//! struct Data {
//! arr: Vec<i32>,
//! }
//! let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";
//!
//! let json = jsonc_to_json(jsonc);
//! let data: Data = serde_json::from_str(&json)?;
//!
//! println!("{}", json);
//! println!("{:?}", data);
//! # assert_eq!(json, "{\"arr\": [1, 2, 3, 4]}");
//! # assert_eq!(data, Data { arr: vec![1, 2, 3, 4] });
//! # Ok(())
//! # }
//! ```
//!
//! Which outputs the following:
//!
//! ```text
//! {"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].
//!
//! ```rust
//! use jsonc_to_json::jsonc_to_json_iter;
//!
//! let jsonc = r#"{foo}/**/[1,2,3,,]"bar""#;
//!
//! let mut iter = jsonc_to_json_iter(jsonc);
//! assert_eq!(iter.next(), Some("{foo}")); // Line comment was removed
//! assert_eq!(iter.next(), Some("[1,2,3")); // Trailing commas was removed
//! assert_eq!(iter.next(), Some("]\"bar\""));
//! assert_eq!(iter.next(), None);
//! ```
//!
//! [JSON with Comments]: https://code.visualstudio.com/docs/languages/json#_json-with-comments
//! [JSON]: https://www.json.org/json-en.html
#![forbid(unsafe_code)]
#![forbid(elided_lifetimes_in_paths)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![warn(clippy::all)]
use std::borrow::Cow;
use std::iter::FusedIterator;
use std::ops::Range;
use any_lexer::{JsonCLexer, JsonCToken, Lexer, TokenSpan};
/// Removes all [JSON with Comments] parts from `jsonc`, turning it into
/// valid [JSON], i.e. removing line comments, block comments, and trailing
/// commas.
///
/// - Line comments, e.g. `// Line Comment`
/// - Block comments, e.g. `/* Block Comment */`
/// - Trailing commas, e.g. `[1,2,3,,]` -> `[1,2,3]`
///
/// If `jsonc` is already valid [JSON], then <code>[Cow]::[Borrowed]\(jsonc)</code>
/// is returned, otherwise a new [`String`] is allocated and <code>[Cow]::[Owned]</code>
/// is returned.
///
/// **Warning:** The conversion is infallible and does not validate `jsonc`.
/// If it contains invalid [JSON] or invalid [JSON with Comments], then the
/// invalid parts are included in the result, i.e. `{foo,/*comment*/bar,}`
/// is turned into `{foo,bar}`.
///
/// See also [`jsonc_to_json_into()`] for an alternative variant, that reuses
/// an already allocated [`String`].
///
/// # Example
///
/// See also [`serde_json` example] in crate root docs.
///
/// ```rust
/// use jsonc_to_json::{jsonc_to_json, jsonc_to_json_into};
///
/// let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";
///
/// let json = jsonc_to_json(jsonc);
/// println!("{}", json);
/// # assert_eq!(json, "{\"arr\": [1, 2, 3, 4]}");
/// ```
///
/// Which outputs the following:
///
/// ```text
/// {"arr": [1, 2, 3, 4]}
/// ```
///
/// [JSON with Comments]: https://code.visualstudio.com/docs/languages/json#_json-with-comments
/// [JSON]: https://www.json.org/json-en.html
/// [Borrowed]: Cow::Borrowed
/// [Owned]: Cow::Owned
/// [`serde_json` example]: crate#serde-example
pub fn jsonc_to_json(jsonc: &str) -> Cow<'_, str> {
let mut iter = JsonCToJsonIter::new(jsonc);
let first = match iter.next() {
Some(first) => first,
None => return Cow::Borrowed(""),
};
let second = match iter.next() {
Some(second) => second,
None => return Cow::Borrowed(first),
};
let mut json = String::new();
json.push_str(first);
json.push_str(second);
for part in iter {
json.push_str(part);
}
Cow::Owned(json)
}
/// Same as [`jsonc_to_json()`], but instead of allocating a
/// new [`String`], then the output JSON is appended to `json`.
///
/// **Note:** The output [JSON] is appended to `json`, i.e. if `json`
/// is not empty, then call [`clear()`] beforehand.
///
/// See [`jsonc_to_json()`] for more information.
///
/// # Example
///
/// See also [`serde_json` example] in crate root docs.
///
/// ```rust
/// # use jsonc_to_json::jsonc_to_json_into;
/// let jsonc = "{\"arr\": [1, 2,/* Comment */ 3, 4,,]}// Line Comment";
///
/// let mut json = String::new();
/// jsonc_to_json_into(jsonc, &mut json);
/// println!("{}", json);
/// # assert_eq!(json, "{\"arr\": [1, 2, 3, 4]}");
/// ```
///
/// Which outputs the following:
///
/// ```text
/// {"arr": [1, 2, 3, 4]}
/// ```
///
/// [JSON]: https://www.json.org/json-en.html
/// [`clear()`]: String::clear
/// [`serde_json` example]: crate#serde-example
#[inline]
pub fn jsonc_to_json_into(jsonc: &str, json: &mut String) {
for part in JsonCToJsonIter::new(jsonc) {
json.push_str(part);
}
}
/// Non-allocating and zero-copy [`Iterator`] that yields string slices
/// of valid [JSON].
///
/// **Warning:** The conversion is infallible and does not validate `jsonc`.
/// If it contains invalid [JSON] or invalid [JSON with Comments], then the
/// invalid parts are included in the result, i.e. `{foo,/*comment*/bar,}`
/// is turned into `{foo,bar}`.
///
/// See [`jsonc_to_json()`] for more information.
///
/// # Example
///
/// ```rust
/// # use jsonc_to_json::jsonc_to_json_iter;
/// let jsonc = r#"{foo}/**/[1,2,3,,]"bar""#;
///
/// let mut iter = jsonc_to_json_iter(jsonc);
/// assert_eq!(iter.next(), Some("{foo}")); // Line comment was removed
/// assert_eq!(iter.next(), Some("[1,2,3")); // Trailing commas was removed
/// assert_eq!(iter.next(), Some("]\"bar\""));
/// assert_eq!(iter.next(), None);
/// ```
///
/// [JSON]: https://www.json.org/json-en.html
#[inline]
pub fn jsonc_to_json_iter(jsonc: &str) -> JsonCToJsonIter<'_> {
JsonCToJsonIter::new(jsonc)
}
/// See [`jsonc_to_json_iter()`] for more information.
#[derive(Clone, Debug)]
pub struct JsonCToJsonIter<'jsonc> {
lexer: JsonCLexer<'jsonc>,
next: Option<Range<usize>>,
}
impl<'jsonc> JsonCToJsonIter<'jsonc> {
/// See [`jsonc_to_json_iter()`] for more information.
pub fn new(jsonc: &'jsonc str) -> Self {
Self {
lexer: JsonCLexer::new(jsonc),
next: None,
}
}
}
impl<'jsonc> Iterator for JsonCToJsonIter<'jsonc> {
type Item = &'jsonc str;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let mut span = match self.next.take() {
Some(span) => span,
None => self.lexer.next_valid_json_token()?,
};
loop {
let next = self.lexer.next_valid_json_token();
if let Some(next) = next {
match span.continue_range(&next) {
Some(new_span) => {
span = new_span;
}
None => {
self.next = Some(next);
break;
}
}
} else {
break;
}
}
Some(&self.lexer.scanner().text()[span])
}
}
impl FusedIterator for JsonCToJsonIter<'_> {}
trait JsonCToJsonExt<'jsonc> {
fn next_token(&mut self) -> Option<(JsonCToken, &'jsonc str)>;
fn next_valid_json_token(&mut self) -> Option<Range<usize>>;
}
impl<'jsonc, I> JsonCToJsonExt<'jsonc> for I
where
I: Iterator<Item = (JsonCToken, TokenSpan<'jsonc>)>,
I: Clone,
{
#[inline]
fn next_token(&mut self) -> Option<(JsonCToken, &'jsonc str)> {
let (tok, span) = self.next()?;
Some((tok, span.as_str()))
}
fn next_valid_json_token(&mut self) -> Option<Range<usize>> {
loop {
let (tok, span) = self.next()?;
let s = span.as_str();
match tok {
JsonCToken::Space => {}
JsonCToken::LineComment | JsonCToken::BlockComment => continue,
JsonCToken::Punct if s == "," => {
let mut iter = self.clone().filter(|(tok, _span)| {
!matches!(
tok,
JsonCToken::Space | JsonCToken::LineComment | JsonCToken::BlockComment
)
});
let (tok, s) = match iter.next_token() {
Some((tok, s)) => (tok, s),
None => continue,
};
match tok {
JsonCToken::Punct if s == "," => continue,
JsonCToken::Delim => continue,
JsonCToken::String
| JsonCToken::Number
| JsonCToken::Null
| JsonCToken::True
| JsonCToken::False
| JsonCToken::Punct
| JsonCToken::Unknown => {}
JsonCToken::Space | JsonCToken::LineComment | JsonCToken::BlockComment => {
unreachable!()
}
}
}
JsonCToken::String
| JsonCToken::Number
| JsonCToken::Null
| JsonCToken::True
| JsonCToken::False
| JsonCToken::Punct
| JsonCToken::Delim
| JsonCToken::Unknown => {}
}
return Some(span.range());
}
}
}
trait ContinueRange: Sized {
fn continue_range(&self, next: &Self) -> Option<Self>;
}
impl ContinueRange for Range<usize> {
#[inline]
fn continue_range(&self, next: &Self) -> Option<Self> {
if self.end == next.start {
Some(self.start..next.end)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! assert_jsonc_to_json {
($jsonc:expr, $json:expr) => {{
let jsonc: &str = $jsonc;
let json: Cow<'_, str> = $json;
let actual = jsonc_to_json(jsonc);
assert_eq!(actual, json);
assert_eq!(
matches!(actual, Cow::Borrowed(_)),
matches!(json, Cow::Borrowed(_))
);
}};
}
#[test]
fn test_empty() {
assert_jsonc_to_json!("", Cow::Borrowed(""));
}
#[test]
fn test_borrowed() {
let jsonc = r#"{"arr": [1, 2, 3, 4]}"#;
assert_jsonc_to_json!(jsonc, Cow::Borrowed(jsonc));
}
#[test]
fn test_borrowed_ending_removed() {
let jsonc = r#"{"arr": [1, 2, 3, 4]} // Line Comment"#;
let json = r#"{"arr": [1, 2, 3, 4]} "#;
assert_jsonc_to_json!(jsonc, Cow::Borrowed(json));
}
#[test]
fn test_line_comment() {
let jsonc = r#"// Comment
{
//
"arr": [1, 2,
// Comment
3, 4] // Comment
//
}
// Comment"#;
let json = "\n{\n \n \"arr\": [1, 2,\n \n 3, 4] \n \n}\n";
assert_jsonc_to_json!(jsonc, Cow::Owned(json.to_owned()));
}
#[test]
fn test_iter() {
let jsonc = r#"{foo}/**/[1,2,3,,]"bar""#;
let mut iter = jsonc_to_json_iter(jsonc);
assert_eq!(iter.next(), Some("{foo}"));
assert_eq!(iter.next(), Some("[1,2,3"));
assert_eq!(iter.next(), Some("]\"bar\""));
assert_eq!(iter.next(), None);
}
}