2

I'm trying to run JMSSerializer. My simple code

use JMS\Serializer\Annotation\Type;

class Comment
{
    private $msg;

    public function __construct($msg)
    {
        $this->msg = $msg;
    }
}

class Person
{
    /**
     * @Type("array<Comment>")
     */
    private $commentList;

    public function addComment(Comment $comment)
    {
        $this->commentList[] = $comment;
    }
}

$type = new Type;
$serializer = JMS\Serializer\SerializerBuilder::create()->build();

$data = new Person();
$data->addComment(new Comment('hey'));

var_dump($serializer->serialize($data, 'json'));

fails with

PHP Fatal error:  Uncaught exception 'Doctrine\Common\Annotations\AnnotationException' with message '[Semantical Error] The annotation "@JMS\Serializer\Annotation\Type" in property Person::$commentList does not exist, or could not be auto-loaded.' in xxx.php:52

OK, but if I add line

$type = new Type;

to trigger autoloader manually, it works:

string(32) "{"comment_list":[{"msg":"hey"}]}"

As I see AnnotationRegistry doesn't use autoloader, it tries to use some own autoloader. It looks ugly, what do I have to do to fix it?

helvete
  • 2,455
  • 13
  • 33
  • 37
Misanthrope
  • 454
  • 4
  • 12
  • possible duplicate of [JMSSerializer stand alone - Annotation does not exist, or cannot be auto-loaded](http://stackoverflow.com/questions/14629137/jmsserializer-stand-alone-annotation-does-not-exist-or-cannot-be-auto-loaded) – Markus Schulte Jun 09 '15 at 15:52

2 Answers2

12

OK, I answer my question myself. I have to register annotations somewhere in autoloader file:

\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
    'JMS\Serializer\Annotation', __DIR__.'/vendor/jms/serializer/src'
);

Other ways: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html#registering-annotations

Misanthrope
  • 454
  • 4
  • 12
0

A full configuration sample for the standalone JMS serializer library could be:

<?php
namespace iMSCP\Service;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
use Doctrine\Common\Annotations\AnnotationRegistry;
use iMSCP_Registry as Registry;

/**
 * Class SerializerServiceFactory
 * @package iMSCP\Service
 */
class SerializerServiceFactory
{
    /**
     * @var Serializer
     */
    static $serialiszer;

    public static function create()
    {
        if (static::$serialiszer === null) {
            $config = Registry::get('config');
            AnnotationRegistry::registerAutoloadNamespace(
                'JMS\Serializer\Annotation', $config['CACHE_DATA_DIR'] . '/packages/vendor/jms/serializer/src'
            );
            static::$serialiszer = SerializerBuilder::create()
                ->setCacheDir(CACHE_PATH . '/serializer')
                ->setDebug($config['DEVMODE'])
                ->build();
        }

        return static::$serialiszer;
    }
}

Here, I register the JMS\Serializer\Annotation namespace using the Annotation registry as provided by Doctrine. Once done, all is working as expected.