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

Unexpected results when serializing arrays containing null value elements #593

Closed
atufkas opened this issue May 24, 2016 · 3 comments
Closed
Milestone

Comments

@atufkas
Copy link

atufkas commented May 24, 2016

Hi,

I just experienced problems with an unexpected behavior when serializing arrays that contain null value elements, meaning: When one or more elements of the array are set to null. Note: I'm not talking about serializing null values at all – I know (need and enabled) this option, but the unexpected behavior affects both scenarios: When serializing null values is enabled as well when it is not.

Example to reproduce the behavior (from a SF controller context):

$data = array(
    'foo' => null,
    'bar' => array(
        'hal',
        42
    ),
    'baz' => array(
        null
    ),
    'biz' => array(
        null,
        'ford'
    )
);

$serializer = $this->container->get('jms_serializer');
$context = SerializationContext::create()->setSerializeNull(true);
$result = $serializer->serialize($data, 'json', $context);

Now, when dumping the $result we get this:

string(57) "{"foo":null,"bar":["hal",42],"baz":[],"biz":{"1":"ford"}}" 

While the JSON output for foo and barlooks normal, be sure you noticed the vanished null element for baz and strangely indexed second array element of biz.

This is obviously not a behavior or bug of json_encode itself, simply proven when dumping the result of json_encode($data), which looks much more like what I would expect:

string(62) "{"foo":null,"bar":["hal",42],"baz":[null],"biz":[null,"ford"]}"

Conclusion: Null value elements in arrays somehow get lost but the array itself – if null values were present – gets converted into an "object" with original numeric indices (with holes were the null values originally resided). Pheeew, a bit complicated to describe, you see it for yourself...

I would classify this bug (if not my mistake of any kind!) as severe since it doesn't allow to deal with null values within arrays at all! That's actually part of my current use case – and I'm pretty sure it is not that exotic.

Totally independently of this specific problem I'd like to say: Thanks to the author and all contributors for a very helpful piece of software you're sharing with us!

@schmittjoh
Copy link
Owner

Is this data on an object or do you just have this array structure?

@atufkas
Copy link
Author

atufkas commented May 24, 2016

Ah, right, didn't mention this. In this case it's just an array structure I'm building myself...

goetas added a commit that referenced this issue Oct 23, 2016
Fixes #593

Introduced with 8cb1e3c containing a non exact test case
@goetas
Copy link
Collaborator

goetas commented Oct 23, 2016

possible solution #660 (and source of the problem)

goetas added a commit that referenced this issue Oct 23, 2016
Fixes #593

Introduced with 8cb1e3c containing a non exact test case
@goetas goetas added this to the v1.4 milestone Oct 23, 2016
goetas added a commit that referenced this issue Oct 28, 2016
Fixes #593

Introduced with 8cb1e3c containing a non exact test case
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

3 participants