Jmsserializerbundle: Serialization : Invalid argument supplied for foreach()

Created on 17 Dec 2014  路  6Comments  路  Source: schmittjoh/JMSSerializerBundle

Hi,

I have a project with a bundle called UserBundle :
This bundle is an extension of SonataUserBundle which inherites of FosUserBundle.

I'm using FosRestBundle to make API calls and I'm using JMS Serializer to write the response inside the API calls.

The serialization to Json worked well until I tried to serialize the User model.

When I tried to do it I got the following error:

Warning: Invalid argument supplied for foreach() in XXXX/vendor/jms/serializer/src/JMS/Serializer/GenericSerializationVisitor.php line 101

So I tried different things to solve this

  • I tried to add an exclusion_policy: ALL to my entity user class, sonata user class and fos user class without success
  • I tried to fix the issue in JMS class which provide the error
    public function visitArray($data, array $type, Context $context)
    {
        if (null === $this->root) {
            $this->root = array();
            $rs = &$this->root;
        } else {
            $rs = array();
        }

        foreach ($data as $k => $v) {
            $v = $this->navigator->accept($v, $this->getElementType($type), $context);

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

            $rs[$k] = $v;
        }

        return $rs;
    }

so to avoid the error I added the following code juste before the foreach statement:

        if(!is_array($data))
            return;

And it works now

So my question is : what is the good solution ? for now it's just a hack I guess so if anyone has an Idea ?

I can provide the whole log message if you want.

serializer issue

Most helpful comment

i experienced the same problem.. my problem: i used the following ORM data type "json_array" in the entity.

@ORM\Column(type="json_array", name="other_nationalities", nullable=true)

in this case the serializer wants to handle it.. (json decode... attach to serialized object..). you have to put a valid json string in the fields of this type.

hope this could help you.

All 6 comments

i experienced the same problem.. my problem: i used the following ORM data type "json_array" in the entity.

@ORM\Column(type="json_array", name="other_nationalities", nullable=true)

in this case the serializer wants to handle it.. (json decode... attach to serialized object..). you have to put a valid json string in the fields of this type.

hope this could help you.

Same problem, but not fixed with "valid json in the field".
@grimabe how have you solved your pb ?

@casual-web I did not solved it. I just don't use the serializer to serialize the user anymore. Indeed I created a function that instantiate an array with the user attributes I wanted (firstname, email , lastname, userId etc.). That's ugly but it works. :-)

@casual-web , @danielsippel
Same problem with ORM data type "json_array" in the entity. And I am sure that json in field is valid.
I made it work with following config:

     * @JMS\AccessType("public_method")
     * @JMS\Type("array")
     * @JMS\Accessor(getter="serializer_getScanResultCache")

And the accessor just returns an array or null if empty.
I think thats all about reflection, using "public_method" access type forces serializer to call getter. It also works in some other cases when reflection access is not working.

Btw doctrines serailisation methods is just too dumb to correctly work with nested objects, so then I need to use string type and paste json string (serialized by JMS) and doing this I getting another trap: JMS doesnt have "json" type for cases like this for adding already serialized string to resulting json.

i experienced the same problem.. my problem: i used the following ORM data type "json_array" in the entity.

@ORM\Column(type="json_array", name="other_nationalities", nullable=true)

in this case the serializer wants to handle it.. (json decode... attach to serialized object..). you have to put a valid json string in the fields of this type.

hope this could help you.
Mine was

@ORM\Column(type="array", name="label", nullable=true)

so i had to change type to text

@ORM\Column(type="text", name="label", nullable=true)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinpapst picture kevinpapst  路  6Comments

Jejec picture Jejec  路  8Comments

CsabaNa picture CsabaNa  路  3Comments

simshaun picture simshaun  路  9Comments

egonolieux picture egonolieux  路  4Comments