Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/JsonEncoder/Encode/PhpAstBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,17 @@ private function buildClosureStatements(DataModelNodeInterface $dataModelNode, a
];
}

$escapedKey = $dataModelNode->getType()->getCollectionKeyType()->isIdentifiedBy(TypeIdentifier::INT)
? new Ternary($this->builder->funcCall('is_int', [$this->builder->var('key')]), $this->builder->var('key'), $this->escapeString($this->builder->var('key')))
: $this->escapeString($this->builder->var('key'));

return [
new Expression(new Yield_($this->builder->val('{'))),
new Expression(new Assign($this->builder->var('prefix'), $this->builder->val(''))),
new Foreach_($accessor, $dataModelNode->getItemNode()->getAccessor()->toPhpExpr(), [
'keyVar' => $this->builder->var('key'),
'stmts' => [
new Expression(new Assign($this->builder->var('key'), $this->escapeString($this->builder->var('key')))),
new Expression(new Assign($this->builder->var('key'), $escapedKey)),
new Expression(new Yield_(new Encapsed([
$this->builder->var('prefix'),
new EncapsedStringPart('"'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ public static function generatedDecoderDataProvider(): iterable
yield ['list', Type::list()];
yield ['object_list', Type::list(Type::object(ClassicDummy::class))];
yield ['nullable_object_list', Type::nullable(Type::list(Type::object(ClassicDummy::class)))];
yield ['iterable_list', Type::iterable(key: Type::int(), asList: true)];

yield ['dict', Type::dict()];
yield ['object_dict', Type::dict(Type::object(ClassicDummy::class))];
yield ['nullable_object_dict', Type::nullable(Type::dict(Type::object(ClassicDummy::class)))];
yield ['iterable_dict', Type::iterable(key: Type::string())];

yield ['iterable', Type::iterable()];
yield ['object_iterable', Type::iterable(Type::object(ClassicDummy::class))];

yield ['object', Type::object(ClassicDummy::class)];
yield ['nullable_object', Type::nullable(Type::object(ClassicDummy::class))];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\JsonEncoder\Mapping\PropertyMetadataLoaderInterface;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Enum\DummyBackedEnum;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Enum\DummyEnum;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Model\DummyWithNormalizerAttributes;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Model\DummyWithOtherDummies;
Expand Down Expand Up @@ -92,12 +93,12 @@ public static function generatedEncoderDataProvider(): iterable
yield ['object_list', Type::list(Type::object(DummyWithNameAttributes::class))];
yield ['nullable_object_list', Type::nullable(Type::list(Type::object(DummyWithNameAttributes::class)))];

yield ['iterable_list', Type::iterable(key: Type::int(), asList: true)];

yield ['dict', Type::dict()];
yield ['object_dict', Type::dict(Type::object(DummyWithNameAttributes::class))];
yield ['nullable_object_dict', Type::nullable(Type::dict(Type::object(DummyWithNameAttributes::class)))];
yield ['iterable_dict', Type::iterable(key: Type::string())];

yield ['iterable', Type::iterable()];
yield ['object_iterable', Type::iterable(Type::object(ClassicDummy::class))];

yield ['object', Type::object(DummyWithNameAttributes::class)];
yield ['nullable_object', Type::nullable(Type::object(DummyWithNameAttributes::class))];
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions src/Symfony/Component/JsonEncoder/Tests/JsonDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,29 @@ public function testDecodeCollection()
{
$decoder = JsonDecoder::create(decodersDir: $this->decodersDir, lazyGhostsDir: $this->lazyGhostsDir);

$this->assertDecoded($decoder, [['foo' => 1, 'bar' => 2], ['foo' => 3]], '[{"foo": 1, "bar": 2}, {"foo": 3}]', Type::list(Type::dict(Type::int())));
$this->assertDecoded(
$decoder,
[true, false],
'{"0": true, "1": false}',
Type::array(Type::bool()),
);

$this->assertDecoded(
$decoder,
[true, false],
'[true, false]',
Type::list(Type::bool()),
);

$this->assertDecoded($decoder, function (mixed $decoded) {
$this->assertIsIterable($decoded);
$array = [];
foreach ($decoded as $item) {
$array[] = iterator_to_array($item);
}
$this->assertSame([true, false], iterator_to_array($decoded));
}, '{"0": true, "1": false}', Type::iterable(Type::bool()));

$this->assertSame([['foo' => 1, 'bar' => 2], ['foo' => 3]], $array);
}, '[{"foo": 1, "bar": 2}, {"foo": 3}]', Type::iterable(Type::iterable(Type::int()), Type::int(), asList: true));
$this->assertDecoded($decoder, function (mixed $decoded) {
$this->assertIsIterable($decoded);
$this->assertSame([true, false], iterator_to_array($decoded));
}, '{"0": true, "1": false}', Type::iterable(Type::bool(), Type::int()));
}

public function testDecodeObject()
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/JsonEncoder/Tests/JsonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ public function testEncodeUnion()
$this->assertEncoded('{"value":null}', $dummy, Type::object(DummyWithUnionProperties::class));
}

public function testEncodeCollection()
{
$this->assertEncoded(
'{"0":{"id":1,"name":"dummy"},"1":{"id":1,"name":"dummy"}}',
[new ClassicDummy(), new ClassicDummy()],
Type::array(Type::object(ClassicDummy::class)),
);

$this->assertEncoded(
'[{"id":1,"name":"dummy"},{"id":1,"name":"dummy"}]',
[new ClassicDummy(), new ClassicDummy()],
Type::list(Type::object(ClassicDummy::class)),
);

$this->assertEncoded(
'{"0":{"id":1,"name":"dummy"},"1":{"id":1,"name":"dummy"}}',
new \ArrayObject([new ClassicDummy(), new ClassicDummy()]),
Type::iterable(Type::object(ClassicDummy::class)),
);

$this->assertEncoded(
'{"0":{"id":1,"name":"dummy"},"1":{"id":1,"name":"dummy"}}',
new \ArrayObject([new ClassicDummy(), new ClassicDummy()]),
Type::iterable(Type::object(ClassicDummy::class), Type::int()),
);
}

public function testEncodeObject()
{
$dummy = new ClassicDummy();
Expand Down