Skip to content

Commit

Permalink
Merge pull request #68 from Crell/json-stream-quote
Browse files Browse the repository at this point in the history
Escape strings in JSON stream formatter.
  • Loading branch information
Crell committed Aug 2, 2024
2 parents d9f47ea + 1cc3151 commit 6c1a862
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Formatter/JsonStreamFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public function format(): string
return 'json-stream';
}

/**
* @param FormatterStream $runningValue
*/
public function serializeString(mixed $runningValue, Field $field, ?string $next): mixed
{
$runningValue->printf('"%s"', is_string($next) ? str_replace('"', '\"', $next) : null);
return $runningValue;
}

/**
* @param FormatterStream $runningValue
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/JsonStreamFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public static function streamExamples(): iterable
)
];

yield AllFieldTypes::class . ' with JSON-type characters' => [
'data' => new AllFieldTypes(
string: '{ "with quotes", and { characters ".',
)
];

yield NullArrays::class => [
'data' => new NullArrays(),
];
Expand Down

0 comments on commit 6c1a862

Please sign in to comment.