From ad353eb07d1ab439b767aac765755946f339fcf5 Mon Sep 17 00:00:00 2001 From: wait-what <25844213+wait-what@users.noreply.github.com> Date: Mon, 26 Feb 2024 19:37:06 +0200 Subject: [PATCH] Add comment support in JSON (jsonc) --- src/serde_json.rs | 29 ++++++++++++++++ tests/json.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/src/serde_json.rs b/src/serde_json.rs index d59e7ad..6f3139e 100644 --- a/src/serde_json.rs +++ b/src/serde_json.rs @@ -432,6 +432,35 @@ impl DeJsonState { self.tok = DeJsonTok::Eof; return Ok(()); } + if self.cur == '/' { + self.next(i); + match self.cur { + '/' => { + while self.cur != '\n' && self.cur != '\0' { + self.next(i); + } + return self.next_tok(i); + } + '*' => { + let mut last = self.cur; + loop { + self.next(i); + if self.cur == '\0' { + return Err(self.err_token("MultiLineCommentClose")); + } + if last == '*' && self.cur == '/' { + self.next(i); + break; + } + last = self.cur; + } + return self.next_tok(i); + } + _ => { + return Err(self.err_token("CommentOpen")); + } + } + } match self.cur { ':' => { self.next(i); diff --git a/tests/json.rs b/tests/json.rs index 5622f60..41ce1b1 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -34,6 +34,90 @@ fn de() { assert_eq!(test.c, None); } +#[test] +fn de_inline_comment() { + #[derive(DeJson)] + pub struct Test { + pub a: Option, + } + + let json = r#"{ //comment + // comment + "a": "// asd"// comment + } // comment"#; + + let test: Test = DeJson::deserialize_json(json).unwrap(); + assert_eq!(test.a.unwrap(), "// asd"); +} + +#[test] +fn de_multiline_comment() { + #[derive(DeJson)] + pub struct Test { + pub a: f32, + } + + let json = r#"{ /* multiline + comment */ + "a": 1 /* multiline * + comment */ + } /** multiline **/"#; + + let test: Test = DeJson::deserialize_json(json).unwrap(); + assert_eq!(test.a, 1.); +} + +#[test] +fn de_illegal_inline_comment() { + #[derive(DeJson)] + pub struct Test { + pub a: f32, + } + + let jsons = vec![ + r#"{ + "a": // comment, + }"#, + r#"{ + / comment + "a": 1, + }"#, + ]; + + for json in jsons { + let test: Result = DeJson::deserialize_json(json); + assert!(test.is_err()); + } +} + +#[test] +fn de_illegal_multiline_comment() { + #[derive(DeJson)] + pub struct Test { + pub a: f32, + } + + let jsons = vec![ + r#"{ + /* /* comment */ */ + "a": 1 + }"#, + r#"{ + /* comment + "a": 1 + }"#, + r#"{ + */ + "a": 1 + }"#, + ]; + + for json in jsons { + let test: Result = DeJson::deserialize_json(json); + assert!(test.is_err()); + } +} + #[test] fn de_reorder() { #[derive(DeJson)]