diff --git a/src/Printer.php b/src/Printer.php index a04b7f36..49ef509a 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -72,6 +72,13 @@ public function print(string $original, bool $unEscapeUnicode, bool $unEscapeSla continue; } + /** + * Ignore whitespace. + */ + if ('' === \trim($character)) { + continue; + } + /** * Process string literal if we are about to leave it. */ diff --git a/test/Unit/PrinterTest.php b/test/Unit/PrinterTest.php index e238495b..a4692258 100644 --- a/test/Unit/PrinterTest.php +++ b/test/Unit/PrinterTest.php @@ -286,6 +286,106 @@ public function testPrintPrintsObjectPrettyWithUnEscapeUnicodeAndUnEscapeSlashes $this->assertSame($expected, $printed); } + public function testPrintPrintsObjectPrettyIdempotently() + { + $original = <<<'JSON' +{ + "name": "Andreas M\u00f6ller", + "emoji": "🤓", + "urls": [ + "https:\/\/localheinz.com", + "https:\/\/github.com\/localheinz", + "https:\/\/twitter.com\/localheinz" + ] +} +JSON; + + $printer = new Printer(); + + $printed = $printer->print( + $original, + false, + false + ); + + $this->assertSame($original, $printed); + } + + public function testPrintPrintsObjectPrettyWithUnEscapeUnicodeIdempotently() + { + $original = <<<'JSON' +{ + "name": "Andreas Möller", + "emoji": "🤓", + "urls": [ + "https:\/\/localheinz.com", + "https:\/\/github.com\/localheinz", + "https:\/\/twitter.com\/localheinz" + ] +} +JSON; + + $printer = new Printer(); + + $printed = $printer->print( + $original, + true, + false + ); + + $this->assertSame($original, $printed); + } + + public function testPrintPrintsObjectPrettyWithUnEscapeSlashesIdempotently() + { + $original = <<<'JSON' +{ + "name": "Andreas M\u00f6ller", + "emoji": "🤓", + "urls": [ + "https://localheinz.com", + "https://github.com/localheinz", + "https://twitter.com/localheinz" + ] +} +JSON; + + $printer = new Printer(); + + $printed = $printer->print( + $original, + false, + true + ); + + $this->assertSame($original, $printed); + } + + public function testPrintPrintsObjectPrettyWithUnEscapeUnicodeAndUnEscapeSlashesIdempotently() + { + $original = <<<'JSON' +{ + "name": "Andreas Möller", + "emoji": "🤓", + "urls": [ + "https://localheinz.com", + "https://github.com/localheinz", + "https://twitter.com/localheinz" + ] +} +JSON; + + $printer = new Printer(); + + $printed = $printer->print( + $original, + true, + true + ); + + $this->assertSame($original, $printed); + } + /** * @see https://github.com/zendframework/zend-json/pull/37 */