简体   繁体   中英

JMS Serializer deserialize nested objects

I'm using: https://jmsyst.com/libs/serializer

For deserializing json objects. Currently I have this:

/**
 * @param int $id
 * @return Customer
 * @throws \Http\Client\Exception
 */
public function get(int $id): Customer
{
    $response = $this->client->get('/customers/' . $id);

    $data = json_encode(json_decode(
        $response->getBody()->getContents()
    )->data->attributes);

    return $this
        ->serializer
        ->deserialize($data, Customer::class, 'json');
}

The json that I receive here looks like this:

   {
    "data": {
            "type": "customer",
            "id": "4356456",
            "links": {
                    "self":"https:\/\/api.ecurring.com\/customers\/345656"
            },
            "attributes": {
                    "gender": "m",
            "first_name": "Laurens"
        }
    }

Is it possible to tell JMS it should automatically start from data->attributes instead of doing something dirty like this:

 $data = json_encode(json_decode(
            $response->getBody()->getContents()
        )->data->attributes);

You can check the exclusion policy.

Read the documentation, it's really helpfull.

Exclusion policy and Annotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM