Skip to content

[Serializer] Manage php JsonSerializable #13496

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

Closed
shouze opened this issue Jan 22, 2015 · 7 comments
Closed

[Serializer] Manage php JsonSerializable #13496

shouze opened this issue Jan 22, 2015 · 7 comments

Comments

@shouze
Copy link

shouze commented Jan 22, 2015

As it's a great enhancement from php 5.4: http://php.net/manual/en/class.jsonserializable.php should be cool the Serializer component would manage objects that implement this interface. Thoughts?

@dunglas
Copy link
Member

dunglas commented Jan 22, 2015

The ObjectNormalizer (#13257) is a good candidate to implement this feature. However, I cannot see a way yo make it compliant with serialization groups and how to handle deserialization support.

@hatembr
Copy link

hatembr commented Feb 18, 2015

This is an excellent suggestion. In an API for example, every "returnable" object could provides its own Json representation.
I was forced to move away from the Serializer and JMS because they do not recognize classes that implement the JsonSerializable interface.

@eddiejaoude
Copy link
Contributor

I was forced to move away from the Serializer and JMS because they do not recognize classes that implement the JsonSerializable interface

@hatembr what did you use/do instead?

@hatembr
Copy link

hatembr commented Feb 18, 2015

For now, in one of the projects I am working on, I had ti use a simple an basic solution. I had to build my own classes that managed the API responses in JSON format.
Basically, every entity/document that could be returned by the API has to implement an interface which itself extends JsonSerializable and provides the correct resource name and the fields that represent the object, in addition to some extra internal stuff.
Everything is cooked inside JsonSerialize which is pretty straightforward, all it does is it returns an array with exact and needed fields. No extra processing is required, no annotations to read etc.
The rest of the work is done by the classes that build up the response and which assemble the different objects to be returned. At the end, every object is then converted to the Json representation it provided, natively.
I can't say where this stands performance-wise, compared to the old annotation-based way, though.
The project I am working on is still at its very beginning, the code and classes are still subject to big changes, but I think I will stick with this approach, it is a nice experiment.

@boekkooi
Copy link
Contributor

boekkooi commented Jan 7, 2016

Why not implement a normalizer for this? Something like https://gist.github.com/boekkooi/5a7d59981739fa0ea5f0

@dunglas
Copy link
Member

dunglas commented Jan 7, 2016

Yes it can be done that way. Groups can even be supported (iterate over the resulting array and remove keys not in applicable groups).

Do you want to work on a PR?

@boekkooi
Copy link
Contributor

boekkooi commented Jan 7, 2016

I wish I had the time to work on this but I'm sorry to say that I really don't have the time.
I just had this code so I wanted to point out a way to do this.

fabpot added a commit that referenced this issue Feb 26, 2016
…zable objects (mcfedr)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Serializer] Add a normalizer that support JsonSerializable objects

This normalizer makes it easier to start to combine using `JsonSerializable` objects with the Symfony serializer. I have implemented it in a number of projects and #13496 shows that others are doing so as well. So it seemed like it would be useful to include it in the Serializer component.

It handles circular references in the same way as the other normalizers.

Because groups and max depth are based on property annotations it doesn't make sense to apply them here.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13496
| License       | MIT
| Doc PR        |

Commits
-------

a678881 Add a normalizer that support JsonSerializable objects
@fabpot fabpot closed this as completed Feb 26, 2016
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

7 participants