schmittjoh / JMSSerializerBundle

Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
http://jmsyst.com/bundles/JMSSerializerBundle
MIT License
1.8k stars 313 forks source link

Serialization : Invalid argument supplied for foreach() #431

Open grimabe opened 9 years ago

grimabe commented 9 years ago

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

    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.

danielsippel commented 9 years ago

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.

casual-web commented 9 years ago

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

grimabe commented 9 years ago

@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. :-)

Arkemlar commented 8 years ago

@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.

Arkemlar commented 8 years ago

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.

ognanshissi commented 5 years ago

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)
EnterVPL commented 1 year ago

on version 5.2.1 error the error still occurs but on "jms/serializer-bundle": "^2.4" not

how to capture all entities for repair and how to fix them?

EnterVPL commented 1 year ago

My bad. I used the string type in the Groups annotation, e.g. @Groups("default_group"), it should be @Groups({"default_group"})