Skip to content

Invalid null serialization in arrays #571

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

Closed
scaytrase opened this issue Apr 21, 2016 · 1 comment
Closed

Invalid null serialization in arrays #571

scaytrase opened this issue Apr 21, 2016 · 1 comment

Comments

@scaytrase
Copy link
Contributor

This code
https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/GenericSerializationVisitor.php#L104-L106

            if (null === $v && ( ! is_string($k) || ! $context->shouldSerializeNull())) {
                continue;
            }

Test

$context = \JMS\Serializer\SerializationContext::create();
$context->setSerializeNull(true);
var_dump($serializer->toArray([1, null, 2], $context));

$context = \JMS\Serializer\SerializationContext::create();
$context->setSerializeNull(true);
var_dump($serializer->serialize([1, null, 2], 'json', $context));

Output

array(2) {
  [0] =>
  int(1)
  [2] =>
  int(2)
}

string(13) "{"0":1,"2":2}"

Dropping null value makes the json_encode think of the data like the named properties. I think correct condition should be

if (null == $v && is_string($k) && !$context->shouldSerializeNull()) {
continue;
}

In this case we drop null only in case they are at hash array, not positional.

@scaytrase
Copy link
Contributor Author

scaytrase commented May 23, 2016

The given commit actually does not resolve the problem.

It fails a simple test

    public function testNullArraySerialization()
    {
        $context = SerializationContext::create();
        $context->setSerializeNull(true);
        $array = [0, null, 'a'];
        self::assertSame($array, $this->serializer->toArray($array, $context));
    }

How we force an array being normalized as array, not object?

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

1 participant