Skip to content

Commit

Permalink
fix: apply JSON options on keys
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Apr 2, 2024
1 parent 64fe0ed commit fbc89ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Normalizer/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public function format(mixed $value): void
$isFirst = false;

if (! $isList) {
$this->write('"' . $key . '":');
$key = json_encode($key, $this->jsonEncodingOptions);

$this->write($key . ':');
}

$this->format($val);
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/Normalizer/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,15 @@ public function __construct(
'transformerAttributes' => [],
'jsonEncodingOptions' => JSON_PRESERVE_ZERO_FRACTION,
];

yield 'array with key and value containing ampersand' => [
'input' => ['foo&bar' => 'bar&baz'],
'expected array' => ['foo&bar' => 'bar&baz'],
'expected_json' => '{"foo\u0026bar":"bar\u0026baz"}',
'transformers' => [],
'transformerAttributes' => [],
'jsonEncodingOptions' => JSON_HEX_AMP,
];
}

public function test_generator_of_scalar_yields_expected_array(): void
Expand Down

0 comments on commit fbc89ad

Please sign in to comment.