1

JSON:

{
    "email": "[email protected]",
    "userProfile": {
        "firstName": "First Name",
        "lastName": "Last Name"
    }
}

In controller:

$user = $this->get('jms_serializer')->deserialize($request->getContent(), 'My\TestBundle\Entity\User', 'json');
print_r($user);

Result:

My\TestBundle\Entity\User Object
(
    [id:My\TestBundle\Entity\User:private] => 
    [email:My\TestBundle\Entity\User:private] => [email protected]
    [userProfile:My\TestBundle\Entity\User:private] => 
)

I don't understand why UserProfile entity is empty even though I provided first and last names. Am I missing something? Thanks.

1 Answer 1

0

According to JMS serializer documentation:

@SerializedName "This annotation can be defined on a property to define the serialized name for a property. If this is not defined, the property will be translated from camel-case to a lower-cased underscored name, e.g. camelCase -> camel_case." http://jmsyst.com/libs/serializer/master/reference/annotations#serializedname

So, I should use @SerializedName annotation to force it to use camelCase instead of underscore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.