-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Added deserialize type #787
Conversation
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. |
closing in favor of #788 |
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). |
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 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:
So what I'm doing as a workaround for this is kind of a hack. I assign a non-existent 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 |
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? |
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).