You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an issue if you use the result in javascript, since there is for example no forEach() anymore on an object type.
The issue seems to be, that in visitArray of JsonSerializationVisitor the result array is constructed including the keys. While this is important for associate arrays, in our case it leads to a "missing" key in the array, which leads to json_encode returning an object.
A workaround is to explicitly provide the $type to serialize(), but it would be nice to have that behaviour automatically.
adding array<MyItem> is what needs to be done. any other way will lead to issues. what f i want to have it an object and not an array?
The current serializer behavior is consistent:
$this->serializer->serialize($items,'json',null,'array<MyItem>'); is always serialized as [...]
$this->serializer->serialize($items,'json',null,'array<string,MyItem>'); is always serialized as {...}
$this->serializer->serialize($items,'json'); is serialized in an inconsistent way and can be [...] or {...} depending on the internal structure of $items
This feature has been introduced with #728 (that fixed many other situations in which was not clear how arrays should be serialized)
Assume the following code:
The result is a javascript object with the three items:
Now add an ExcludeIf to the MyItem class:
The result is now a javascript object:
This is an issue if you use the result in javascript, since there is for example no forEach() anymore on an object type.
The issue seems to be, that in visitArray of JsonSerializationVisitor the result array is constructed including the keys. While this is important for associate arrays, in our case it leads to a "missing" key in the array, which leads to json_encode returning an object.
A workaround is to explicitly provide the $type to serialize(), but it would be nice to have that behaviour automatically.
Unfortunately it seems like there is no easy way to know if the provided array is a "regular" array or an associate array, since all arrays in PHP are associative arrays. .
What do you think?
The text was updated successfully, but these errors were encountered: