From d0d878ac9d6acff9e9a099cdc3b93b75228c33b2 Mon Sep 17 00:00:00 2001 From: Jitendra <2908547+adhocore@users.noreply.github.com> Date: Sat, 1 Oct 2022 12:47:45 +0700 Subject: [PATCH 1/3] fix: json string inside json with comment like value breaks --- src/Comment.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Comment.php b/src/Comment.php index d4e1764..1d32672 100644 --- a/src/Comment.php +++ b/src/Comment.php @@ -63,11 +63,12 @@ protected function doStrip(string $json): string $crlf = ["\n" => '\n', "\r" => '\r']; while (isset($json[++$this->index])) { + $oldprev = $prev ?? ''; list($prev, $char, $next) = $this->getSegments($json); $return = $this->checkTrail($char, $return); - if ($this->inStringOrCommentEnd($prev, $char, $char . $next)) { + if ($this->inStringOrCommentEnd($prev, $char, $char . $next, $oldprev)) { $return .= $this->inStr && isset($crlf[$char]) ? $crlf[$char] : $char; continue; @@ -115,19 +116,19 @@ protected function checkTrail(string $char, string $json): string return $json; } - protected function inStringOrCommentEnd(string $prev, string $char, string $next): bool + protected function inStringOrCommentEnd(string $prev, string $char, string $next, string $oldprev): bool { - return $this->inString($char, $prev, $next) || $this->inCommentEnd($next); + return $this->inString($char, $prev, $next, $oldprev) || $this->inCommentEnd($next); } - protected function inString(string $char, string $prev, string $next): bool + protected function inString(string $char, string $prev, string $next, string $oldprev): bool { if (0 === $this->comment && $char === '"' && $prev !== '\\') { return $this->inStr = !$this->inStr; } if ($this->inStr && \in_array($next, ['":', '",', '"]', '"}'], true)) { - $this->inStr = false; + $this->inStr = "$oldprev$prev" !== '\\\\'; } return $this->inStr; From 7ca526bede70d25b13e03410bbe61bac7d995849 Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Sat, 1 Oct 2022 14:05:21 +0700 Subject: [PATCH 2/3] test: add subjson testcase --- tests/CommentTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/CommentTest.php b/tests/CommentTest.php index af763f2..654769e 100644 --- a/tests/CommentTest.php +++ b/tests/CommentTest.php @@ -64,9 +64,33 @@ public function testParseFromFile() $parsed = Comment::parseFromFile(__DIR__ . '/composer.json', true); $this->assertTrue(is_array($parsed)); + $this->assertNotEmpty($parsed); $this->assertSame('adhocore/json-comment', $parsed['name']); } + public function testSubJson() + { + // https://github.com/adhocore/php-json-comment/issues/15 + $parsed = Comment::parse('{ + "jo": "{ + /* comment */ + \"url\": \"http://example.com\"//comment + }", + "x": { + /* comment 1 + comment 2 */ + "y": { + // comment + "XY\\\": "//no comment/*", + }, + } + }', true); + + $this->assertArrayHasKey('jo', $parsed); + $this->assertSame('//no comment/*', $parsed['x']['y']['XY\\']); + $this->assertSame('http://example.com', Comment::parse($parsed['jo'])->url); + } + public function theTests() { return [ From 41926bbbad9640d0dc8b3ed26bf52cfe44fb5190 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 1 Oct 2022 07:06:32 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- src/Comment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment.php b/src/Comment.php index 1d32672..418c66e 100644 --- a/src/Comment.php +++ b/src/Comment.php @@ -63,7 +63,7 @@ protected function doStrip(string $json): string $crlf = ["\n" => '\n', "\r" => '\r']; while (isset($json[++$this->index])) { - $oldprev = $prev ?? ''; + $oldprev = $prev ?? ''; list($prev, $char, $next) = $this->getSegments($json); $return = $this->checkTrail($char, $return);