Skip to content

Added deserialize type #787

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

Closed

Conversation

bobvandevijver
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Doc updated yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets schmittjoh/JMSSerializerBundle#582
License Apache-2.0

This PR adds the possibility to have a different deserialization type than the original type. This can be useful for DateTime serialization, when you might expect an all zero time as result (see linked issue).

@goetas
Copy link
Collaborator

goetas commented Jun 17, 2017 via email

@bobvandevijver
Copy link
Contributor Author

Right 😅

Well, luckily I also created the other solution! However, I do think there can be some usecases.. For example, when you want to deserialize a child member a a special class which indicates the deserialization step. Might be far fetched though.

@goetas
Copy link
Collaborator

goetas commented Jun 17, 2017

closing in favor of #788

@goetas goetas closed this Jun 17, 2017
@goetas
Copy link
Collaborator

goetas commented Jun 17, 2017

Will motivate properly on Monday

so, the main issue that what this PR was trying to solve was a "format" issue, not a type. objects have a type, and that type is "serialized" in a certain format.

Dates to be serialized or parsed back to objects have 10.000 different formats (standards... https://imgs.xkcd.com/comics/iso_8601.png).
Different "formats" will converge to a class DateTime (hopefully always...)

@martintama
Copy link

martintama commented Jul 27, 2017

Hi, I might have a use case that might be suitable for this PR. I consume an API that returns a full object in one property (Country), so I use the @Type("AppBundle\Entity\Country") annotation, but it expects a string when patching that property (which is the field "name" belonging to that object).

I tried changing the getter of this property to return not the Country object itself but the property value, but I always get the following error:

[Symfony\Component\Debug\Exception\ContextErrorException] Warning: ReflectionProperty::getValue() expects parameter 1 to be object, string given

So what I'm doing as a workaround for this is kind of a hack. I assign a non-existent @Group to that field, to prevent it from being serialized back, and then add a new getter with the @VirtualProperty and @SerializedName annotations that just returns the property name.

The final result looks like this:

/**
 * Client
 *
 */
class Client
{
    /**
     * @var int
     * @Serializer\Type("integer")
     *
     */
    private $id;

    /**
     * @var Country
     * @Serializer\Type("AppBundle\Entity\Country")
     * @Serializer\Groups({"excluded"})
     */
    private $country;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set country
     *
     * @param string $country
     * @return Client
     */
    public function setCountry($country)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return Country
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * @Serializer\VirtualProperty
     * @Serializer\SerializedName("country")
     */
    public function getCountryName(){
        if ($this->getCountry()){
            return $this->getCountry()->getName();
        }
    }

}

Is there any other cleaner way to achieve this? Having a @DeserializeType("string") might help in this situation.

@goetas
Copy link
Collaborator

goetas commented Jul 27, 2017

I tend to use symfony/form when is about handling requests that involve any kind of data validation or normalization.

I use almost always jms/serializer when serializing, while the deserialization component is used only when the incoming data do not require validation or normalization.

If the data are different for different operations, shouldn't you use DTOs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants