Jmsserializerbundle: change serialized name of a property when it is in a specific group

Created on 31 May 2015  路  7Comments  路  Source: schmittjoh/JMSSerializerBundle

it happens lots of time we wanna serialize a sub set of properties , just like this, it also happens we need to change name of serialized name of property in different group. may it will be better if we can do this:

    /**
     * @Expose
     * @SerializedName("text",{"product_select_car_model"})
     * @Groups({"product_select_car_model"})
     */
     private $brandName;

this means when we serialize this entity in product_select_car_model group,change serialize property brandName to text.

Most helpful comment

Hello,

You can make a new getter for your property and use a virtual_property.

Here an exemple with lastname serialized in "lastname" and "name" in two groups.

    /**
     * @return string
     */
    public function getNameForGroup2()
    {
        return $this->getLastname();
    }

    /**
     * @return string
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * @var string
     */
    private $lastname;
    virtual_properties:
        getNameForGroup2:
            serialized_name: name
            expose: true
            type: string
            groups: ['group2']
    properties:
        lastname:
            serialized_name: lastname
            expose: true
            type: string
            groups: ['group1']

All 7 comments

Hi, I need it too. Have you found a solution ?

Thanks.

@redayoub , unfortunitely I have't , I will post it here if I do.

Hi guyz,

I need it too, Have you found a solution ?

see u.

Hello,

You can make a new getter for your property and use a virtual_property.

Here an exemple with lastname serialized in "lastname" and "name" in two groups.

    /**
     * @return string
     */
    public function getNameForGroup2()
    {
        return $this->getLastname();
    }

    /**
     * @return string
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * @var string
     */
    private $lastname;
    virtual_properties:
        getNameForGroup2:
            serialized_name: name
            expose: true
            type: string
            groups: ['group2']
    properties:
        lastname:
            serialized_name: lastname
            expose: true
            type: string
            groups: ['group1']

@psary looks good

Thanks :+1:

I am extending the entity(model) by another one and changing the accessType to public method. Unless you do have own naming strategy you can specify the new name and/or use groups. On second thought you may use Trait instead.

Was this page helpful?
0 / 5 - 0 ratings