-
-
Notifications
You must be signed in to change notification settings - Fork 587
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
I'm receiving "Class ArrayCollection does not exist" when serializing (temporarily solved with a workaround) #274
Comments
do you register any custom handlers? if yes, you need to call addDefaultHandlers on the builder object |
Oh. Okay, slowly the pieces are coming together. I'm trying to add a custom SerializationVisitor which will output a simple array (I'm calling it I see that all of the handlers defined in $formats = array('json', 'xml', 'yml'); This is why when I defined 'array' as a serializer, the handlers were not being used. Workaround 2: If I add my ArraySerializationVisitor and call it "json" then it tricks the handlers in to working. $serializer->setSerializationVisitor('json', new ArraySerializationVisitor(new PropertyNamingStrategy())) Now everything works as expected. I suppose one way of fixing this at a library level would be to expose those Thoughts? |
Same problem here. Looks like you are not the only one who wants to serialize objects into arrays. Actually, this is not really a serialization, it's a normalization; but IMHO it should be possible to add Visitors without so much effort. The I see another workaround : use |
+1 Formats as a hard-coded array: $formats = array('json', 'xml', 'yml'); break option to add a new Serializer |
Lets go for some extends...: parameters:
jms_serializer.datetime_handler.class: YourBundle\Serializer\DateHandler <?php
namespace YourBundle\Serializer;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\DateHandler as BaseDateHandler;
class DateHandler extends BaseDateHandler
{
/**
* @return array
*/
public static function getSubscribingMethods()
{
$methods = [];
$types = ['DateTime', 'DateInterval'];
foreach (['json', 'xml', 'yml', 'array'] as $format) {
$methods[] = [
'type' => 'DateTime',
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => $format,
];
foreach ($types as $type) {
$methods[] = [
'type' => $type,
'format' => $format,
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'method' => 'serialize'.$type,
];
}
}
return $methods;
}
} And a simple array serializer you.array_serialization_visitor:
class: YourBundle\Serializer\ArraySerializationVisitor
arguments:
- @jms_serializer.naming_strategy
tags:
- { name: jms_serializer.serialization_visitor, format: array} <?php
namespace YourBundle\Serializer;
use JMS\Serializer\GenericSerializationVisitor;
class ArraySerializationVisitor extends GenericSerializationVisitor
{
/**
* @return array
*/
public function getResult()
{
return $this->getRoot();
}
} |
can it be related to #517 (comment) ? |
I'm using JMS Serializer with Slim 3 framework for groups so I can easily return the response with desired fields. In this case there's no purpose to encode the result to json in the serializer using "json" method as if I want to attach it to the
then I need to decode it, as it will be encoded in In my opinion, JMS Serializer is really powerful, and I prefer to overwrite "json" method as mentioned above to get the desired result. Actually I was trying to play with PSR-7 StreamInterface thingy, but I still can't get it really (I end up with errors that I can't write to stream, or other weird issues) so actually I don't know how to set the part of the body in other way than by using |
Does your application include |
fixed with c8ad292 |
@goetas In my case |
I found out, that this error happened in my case because of FriendsOfSymfony/FOSRestBundle#1851 |
I saw another issue ticket covering this but the thread included so many topics that didn't help me solve or debug the issue.
I'm receiving this exception when serializing a class with nested collections:
... and I debugged and breakpointed myself to death and eventually found the code arriving at this point:
JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber
For anyone who needs to solve this in a hurry and move on with their day, adding this to your bootstrapping/config file will give PHP a chance to find the class:
Is there any more reasonable way to solving this?
The text was updated successfully, but these errors were encountered: