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

Bad Serialization : object instead of array #773

Closed
alexdu98 opened this issue Oct 19, 2019 · 1 comment
Closed

Bad Serialization : object instead of array #773

alexdu98 opened this issue Oct 19, 2019 · 1 comment

Comments

@alexdu98
Copy link

Hello,

I have a problem of serialization. An array with non-consecutive keys is serialized as an object.

Examples of arrays :

$a1 = array(
    NULL,
    array('prop1' => 2),
    array('prop1' => 3)
);
$a2 = array(
    array('prop1' => 1),
    NULL,
    array('prop1' => 3)
);
$a3 = array(
    array('prop1' => 1),
    array('prop1' => 2),
    NULL
);

The Serialization results are :
$a1 :

{
    "1": {
        "prop1": 2
    },
    "2": {
        "prop1": 3
    }
}

$a2 :

{
    "0": {
        "prop1": 1
    },
    "2": {
        "prop1": 3
    }
}

$a3 :

[
    {
        "prop1": 1
    },
    {
        "prop1": 2
    }
]

I found issue 321 that mentions this behavior.
I conclude with the same thing, the problem is due to the indexes of the array.

The problem is that in my case the result comes from a Doctrine query and sometimes contains null values.
2 possible but not viable solutions in my opinion :
The first one is to add setSerializeNull(true), but this forces you to check in post traitement if the foreach that the value is not null.
The second is to browse the array before serialization to delete the null values and reorder the array, but it's not very optimal in terms of performance.

Would it not be possible to plan a correction/evolution to have an array rather than an object in this case?
If not, do you have another solution to offer me?

@goetas
Copy link
Collaborator

goetas commented Oct 19, 2019

Please check https://github.com/schmittjoh/serializer/blob/master/doc/cookbook/arrays.rst to see how can you configure jms to serialize arrays properly. You can use the same syntax for the @Type annotation

@goetas goetas closed this as completed Oct 19, 2019
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