-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Serialize using jsonSerialize() if object implements JsonSerializable #846
Comments
See here the a simple solution for the issue. Regarding the feature request, the jms/serializer "json" serialization is not based on "json_encode" conventions so to me it seems logical to not use The JMS serializer tries to guess the type of the object and visit its properties. When the A Is also possible to specify (obviously all this works when talking about "serialization", "de-serialization" is a different problem) |
Hey, Thanks for the response. Here's some of my code:
This still gives me the following response:
Also, |
did you clear your cache? |
Hey, I didn't.. but after your request I did remove Symfony cache which is under |
Weird, I'm using |
Are you sure that metadata are loaded correctly? if you put |
I got the same thing.. which means I might be missing something.. I will get back to this in the evening and try to look a bit more as I don't want to annoy you, because I probably lack some knowledge. Thanks. I would be glad though if you can reference me to some links. Thanks |
it looks that your metadata are not loaded. which version of symfony are you using? |
3.3.13 |
you do not need to build "manually" the serializer instance, just use public function generateGoodResponse($jsonResponse)
{
$jsonContent = $this->serializer->serialize($jsonResponse, 'json');
$response = new Response($jsonContent);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
if you use using autowiring just add a typehint Closing as it is not a bug but a config issue |
Serialize using jsonSerialize() if object implements JsonSerializable
I received an issue on ramsey/uuid-doctrine, where a user describes a situation where the
Ramsey\Uuid\Uuid
object is converted to a JSON object of its properties, rather than the expected string representation. See ramsey/uuid-doctrine#50.In using ramsey/uuid-doctrine, they expected the serialized JSON for the database field to include the string UUID and nothing else. Since Uuid implements the JsonSerializable interface, I would also have this same expectation. However, jms/serializer doesn't appear to respect JsonSerializable, so it doesn't serialize the object according to its own rules. Instead, it seems to iterate over the properties of the object and creates a JSON object based on those.
I'm requesting that jms/serializable implement a feature that detects whether an object implements JsonSerializable, and if so, call
json_encode()
on the object instead.Example
Here's an example showing how jms/serializer renders a Uuid object.
composer.json
:serialize-uuid.php
:Running this script, we see the following output:
The text was updated successfully, but these errors were encountered: