Skip to content

Commit 20821d6

Browse files
committed
[JsonStreamer] Add include_null_properties option
1 parent 79cd71d commit 20821d6

21 files changed

+255
-113
lines changed

src/Symfony/Component/JsonStreamer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Remove `nikic/php-parser` dependency
8+
* Add `include_null_properties` option to encode the properties with `null` value
89

910
7.3
1011
---

src/Symfony/Component/JsonStreamer/JsonStreamWriter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
/**
3030
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
3131
*
32-
* @implements StreamWriterInterface<array<string, mixed>>
32+
* @implements StreamWriterInterface<array{
33+
* include_null_properties?: bool,
34+
* ...<string, mixed>,
35+
* }>
3336
*
3437
* @experimental
3538
*/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;
4+
5+
use Symfony\Component\JsonStreamer\Attribute\StreamedName;
6+
7+
class DummyWithDollarNamedProperties
8+
{
9+
#[StreamedName('$foo')]
10+
public bool $foo = true;
11+
12+
#[StreamedName('{$foo->bar}')]
13+
public bool $bar = true;
14+
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/null.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
77
try {
8-
yield 'null';
8+
yield "null";
99
} catch (\JsonException $e) {
1010
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
1111
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/nullable_backed_enum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
if ($data instanceof \Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum) {
99
yield \json_encode($data->value, \JSON_THROW_ON_ERROR, 512);
1010
} elseif (null === $data) {
11-
yield 'null';
11+
yield "null";
1212
} else {
1313
throw new \Symfony\Component\JsonStreamer\Exception\UnexpectedValueException(\sprintf('Unexpected "%s" value.', \get_debug_type($data)));
1414
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/nullable_object.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
77
try {
88
if ($data instanceof \Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes) {
9-
yield '{"@id":';
9+
$prefix1 = '';
10+
yield "{{$prefix1}\"@id\":";
1011
yield \json_encode($data->id, \JSON_THROW_ON_ERROR, 511);
11-
yield ',"name":';
12+
$prefix1 = ',';
13+
yield "{$prefix1}\"name\":";
1214
yield \json_encode($data->name, \JSON_THROW_ON_ERROR, 511);
13-
yield '}';
15+
yield "}";
1416
} elseif (null === $data) {
15-
yield 'null';
17+
yield "null";
1618
} else {
1719
throw new \Symfony\Component\JsonStreamer\Exception\UnexpectedValueException(\sprintf('Unexpected "%s" value.', \get_debug_type($data)));
1820
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/nullable_object_dict.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
77
try {
88
if (\is_array($data)) {
9-
yield '{';
10-
$prefix = '';
9+
yield "{";
10+
$prefix1 = '';
1111
foreach ($data as $key => $value) {
1212
$key = \substr(\json_encode($key), 1, -1);
13-
yield "{$prefix}\"{$key}\":";
14-
yield '{"@id":';
13+
$prefix2 = '';
14+
yield "{$prefix1}\"{$key}\":{{$prefix2}\"@id\":";
1515
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
16-
yield ',"name":';
16+
$prefix2 = ',';
17+
yield "{$prefix2}\"name\":";
1718
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
18-
yield '}';
19-
$prefix = ',';
19+
yield "}";
20+
$prefix1 = ',';
2021
}
21-
yield '}';
22+
yield "}";
2223
} elseif (null === $data) {
23-
yield 'null';
24+
yield "null";
2425
} else {
2526
throw new \Symfony\Component\JsonStreamer\Exception\UnexpectedValueException(\sprintf('Unexpected "%s" value.', \get_debug_type($data)));
2627
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/nullable_object_list.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
77
try {
88
if (\is_array($data)) {
9-
yield '[';
10-
$prefix = '';
9+
yield "[";
10+
$prefix1 = '';
1111
foreach ($data as $value) {
12-
yield $prefix;
13-
yield '{"@id":';
12+
$prefix2 = '';
13+
yield "{$prefix1}{{$prefix2}\"@id\":";
1414
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
15-
yield ',"name":';
15+
$prefix2 = ',';
16+
yield "{$prefix2}\"name\":";
1617
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
17-
yield '}';
18-
$prefix = ',';
18+
yield "}";
19+
$prefix1 = ',';
1920
}
20-
yield ']';
21+
yield "]";
2122
} elseif (null === $data) {
22-
yield 'null';
23+
yield "null";
2324
} else {
2425
throw new \Symfony\Component\JsonStreamer\Exception\UnexpectedValueException(\sprintf('Unexpected "%s" value.', \get_debug_type($data)));
2526
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/object.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
*/
66
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
77
try {
8-
yield '{"@id":';
8+
$prefix1 = '';
9+
yield "{{$prefix1}\"@id\":";
910
yield \json_encode($data->id, \JSON_THROW_ON_ERROR, 511);
10-
yield ',"name":';
11+
$prefix1 = ',';
12+
yield "{$prefix1}\"name\":";
1113
yield \json_encode($data->name, \JSON_THROW_ON_ERROR, 511);
12-
yield '}';
14+
yield "}";
1315
} catch (\JsonException $e) {
1416
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
1517
}

src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/object_dict.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
*/
66
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
77
try {
8-
yield '{';
9-
$prefix = '';
8+
yield "{";
9+
$prefix1 = '';
1010
foreach ($data as $key => $value) {
1111
$key = \substr(\json_encode($key), 1, -1);
12-
yield "{$prefix}\"{$key}\":";
13-
yield '{"@id":';
12+
$prefix2 = '';
13+
yield "{$prefix1}\"{$key}\":{{$prefix2}\"@id\":";
1414
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
15-
yield ',"name":';
15+
$prefix2 = ',';
16+
yield "{$prefix2}\"name\":";
1617
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
17-
yield '}';
18-
$prefix = ',';
18+
yield "}";
19+
$prefix1 = ',';
1920
}
20-
yield '}';
21+
yield "}";
2122
} catch (\JsonException $e) {
2223
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
2324
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy