Jmsserializerbundle: The document for yml configuration is confusing, why yml configuration is not working?

Created on 4 Aug 2015  ·  10Comments  ·  Source: schmittjoh/JMSSerializerBundle

As you can see yml configuration below, MyBundle\Resources\config\serializer\Fully.Qualified.ClassName.yml suggests if the directory path for my entity is /private/var/www/symfony/src/VMSP/CarBundle/Entity/CarBrand.php, then serializer configuration should be /private/var/www/symfony/src/VMSP/CarBundle/Resources/config/serializer/VMSP.CarBundle.Entity.CarBrand.yml
, but unfortunately this doesn't work.

the main configure for JSMSerializerBundle is

jms_serializer:
    metadata:
        directories:
            vmsp-user:
                namespace_prefix: "VMSP\\UserBundle"
                path: "@VMSPUserBundle/Resources/config/serializer"
            vmsp-car:
                namespace_prefix: "VMSP\\CarBundle"
                path: "@VMSPCarBundle/Resources/config/serializer"

I invoke the serializer with these code:

  $carBrands= $brandRepository->findCarBrandWithSeriesAndModel();

        $serializer =    $this->get('jms_serializer');

        $result=$serializer->serialize($carBrands, 'json', SerializationContext::create()->setGroups(array('product_select_car_model')));

yml configuration for my entity:

VMSP\CarBundle\Entity\CarBrand:
    exclusion_policy: ALL
    xml_root_name: file
    properties:
        id:
            expose: true
            type: string
            group: product_select_car_model
        brandName:
            expose: true
            type: string
            group: product_select_car_model
        carSeries:
            expose: true
            group: product_select_car_model


YAML Reference

::

# MyBundle\Resources\config\serializer\Fully.Qualified.ClassName.yml
Fully\Qualified\ClassName:
    exclusion_policy: ALL
    xml_root_name: foobar
    exclude: true
    read_only: false
    access_type: public_method # defaults to property
    accessor_order: custom
    custom_accessor_order: [propertyName1, propertyName2, ..., propertyNameN]
    discriminator:
        field_name: type
        map:
            some-value: ClassName
    virtual_properties:
        getSomeProperty:
            serialized_name: foo
            type: integer
    xml_namespaces:
        "": http://your.default.namespace
        atom: http://www.w3.org/2005/Atom
    properties:
        some-property:
            exclude: true
            expose: true
            access_type: public_method # defaults to property
            accessor: # access_type must be set to public_method
                getter: getSomeOtherProperty
                setter: setSomeOtherProperty
            type: string
            serialized_name: foo
            since_version: 1.0
            until_version: 1.1
            groups: [foo, bar]
            xml_attribute: true
            xml_value: true
            inline: true
            read_only: true
            xml_key_value_pairs: true
            xml_list:
                inline: true
                entry_name: foo
            xml_map:
                inline: true
                key_attribute_name: foo
                entry_name: bar
            xml_attribute_map: true
            xml_element:
                cdata: false
                namespace: http://www.w3.org/2005/Atom
            max_depth: 2

    handler_callbacks:
        serialization:
            xml: serializeToXml
            json: serializeToJson
        deserialization:
            xml: deserializeFromXml

    callback_methods:
        pre_serialize: [foo, bar]
        post_serialize: [foo, bar]
        post_deserialize: [foo, bar]

All 10 comments

Even though I change /private/var/www/symfony/src/VMSP/CarBundle/Resources/config/serializer/VMSP.CarBundle.Entity.CarBrand.yml to /private/var/www/symfony/src/VMSP/CarBundle/Resources/config/serializer/Entity.CarBrand.yml or /private/var/www/symfony/src/VMSP/CarBundle/Resources/config/serializer/CarBrand.yml, and clear the cache, it still doesn't work.

I am wondering anyone has the problem, no matter what I try, I just can't make the metadata work.

I think JMSSerializer should write a log for entity that has no metadata defined , and output the path it' look to.

I have exactly the same issue.

@videni looks loke I found the solution.

When I initialized the serializer like:
$serializer = \JMS\Serializer\SerializerBuilder::create()->build();
only the annotations were applied.

But as soon as I changed to:
$serializer = $this->get('jms_serializer');
the YML files are getting parsed. The YML's name: Entity.RoundImage.yml

@matyaspeto ,you are right, most of the time, the reason is the wrong yml file name or we change the metadata mapping like this

 jms_serializer:
    metadata:
        directories:
             vmsp-user:
                namespace_prefix: "VMSP\\UserBundle"
                path: "@VMSPUserBundle/Resources/config/serializer"

so what was the solution?

I am also struggling with this...

The same issue

Create your yaml file and set directory path in the configuration

Was this page helpful?
0 / 5 - 0 ratings