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

Serializing a stdClass #158

Closed
simshaun opened this issue Aug 8, 2012 · 9 comments
Closed

Serializing a stdClass #158

simshaun opened this issue Aug 8, 2012 · 9 comments

Comments

@simshaun
Copy link

simshaun commented Aug 8, 2012

Somebody in IRC mentioned this, so figured I'd try it out too.

It seems the bundle doesn't serialize a stdClass. Is there a reason why? (I'm clueless as to the inner workings of JMSSerializer)

$object = new \stdClass();
$object->foo = 'foo';
$object->bar = 'bar';
echo $serializer->serialize($object, 'json');
@lsmith77
Copy link
Contributor

lsmith77 commented Aug 9, 2012

I guess stdClass should just be cast to an array.

@wjaspers
Copy link

Related: #271

A stdClass by itself doesn't seem to be a problem as you really have few other ways, if any, to serialize them, but it seems other objects are treated similarly.

@AlexSkrypnyk
Copy link

@AlexSkrypnyk
Copy link

@lsmith77
Casting stdClass to array converts empty objects to empty arrays. so, no good

@thomask
Copy link

thomask commented Dec 16, 2015

I agree.

Imo:

$aObj = new \StdClass();
$aObj->foo = "bar";
$aObj->fus = ["roo" => "da"];

$bObj = new \StdClass();

$arr = [
    [
       "bar" => "a",
       "obj" => $aObj,
    ],
    [
       "bar" => "a",
       "obj" => $bObj,
    ],
]

Should result in:

[
  {
    "bar": "a",
    "obj": {
       "foo": "bar",
       "fus": {
         "roo": "da"
       } 
    }
  },
  {
   "bar": "b",
   "obj": {}
  }
]

This is not possible at the moment as far as I can see

@skonsoft
Copy link

In 2016, i continue having this issue

@fracz
Copy link

fracz commented Jan 10, 2017

2017 and still banging our heads against a brick wall.

@goetas
Copy link
Collaborator

goetas commented Jan 11, 2017

@fracz @skonsoft

commented about it already here

As already said, stdclass is an php particularity and different developers handle data inside it in different ways and because of it, there is no "true" way to solve the problem.

Even phpitself handles it in a particular way and having edge cases that was necessary to document
http://php.net/manual/en/language.types.object.php#language.types.object.casting

An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated.

I understand that there are more than 5-10 issues about stdClss, but IMO the solution is simple:

  • get rid of stdClass in your code
  • implement this class handler in a user package

A custom handler as suggested in schmittjoh/serializer#429 can be a solution for you, since you can decide the solution to adopt that fits in a better way your application.

In case I'm missing some important details, or you do not agree with my explanation can you explain your usecase?

@studio3w
Copy link

@goetas Thanks, I got rid of stdClass in my code and I deal with a simple array instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants