Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays are serialized as objects like {"0":... } when data contains empty objects #488

Closed
mrix opened this issue Sep 7, 2015 · 1 comment

Comments

@mrix
Copy link

mrix commented Sep 7, 2015

For example this code:

<?php
require __DIR__.'/vendor/autoload.php';

class EmptyClass
{
}
class NotEmptyClass
{
    public $a = 1;
}
class WrapperClass
{
    public $object;

    public function __construct($object)
    {
        $this->object = $object;
    }
}

$objects = [
    'stdClass'      => new stdClass(),
    'ArrayObject'   => new ArrayObject(),
    'EmptyClass'    => new EmptyClass(),
    'NotEmptyClass' => new NotEmptyClass(),
    'DateTime'      => new DateTime(),
];
foreach ($objects as $title => $object) {
    // data to serialize
    $item = [new WrapperClass($object)];
    // serialize
    $json = JMS\Serializer\SerializerBuilder::create()
        ->addDefaultHandlers()
        ->addDefaultSerializationVisitors()
        ->addDefaultDeserializationVisitors()
        ->setCacheDir(__DIR__ . '/runtime')
        ->setDebug(true)
        ->build()
        ->serialize($item, 'json');

    // output
    echo $title.':'.PHP_EOL;
    echo $json.PHP_EOL;
    echo str_repeat('-', 40).PHP_EOL;
}

outputs:

stdClass:
{"0":{"object":{}}}
----------------------------------------
ArrayObject:
{"0":{"object":{}}}
----------------------------------------
EmptyClass:
{"0":{"object":{}}}
----------------------------------------
NotEmptyClass:
[{"object":{"a":1}}]
----------------------------------------
DateTime:
[{"object":"2015-09-07T14:50:34+0600"}]
----------------------------------------

I think this is related to #373

@andreaslarssen
Copy link

...which has been closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants