How to deserialize JSON-LD from ApiPlatform with PHP

Viewed 1616

I've successfully built an API for my system with ApiPlatform. Thanks to the developers it's an excellent system. I can send it requests in JSON-LD format and it returns it's responses in JSON-LD as well.

I understand the benefits of using JSON-LD, but I feel like I am missing a big piece of the puzzle. How to deserialize to PHP objects, using the embedded JSON-LD context, in my Symfony based PHP client that is to consume the API.

Say I have the following response from my API

{
  "@context": "/contexts/CallbackRequest",
  "@id": "/callback-requests/72",
  "@type": "CallbackRequest",
  "id": 72,
  "created": "2017-09-22T08:07:25+02:00",
  "customer": {
    "@id": "/customers/13",
    "@type": "Customer",
    "id": 13,
    "firstName": "Joe",
    "lastName": "Bloggs",
    "email": "joe@bloggs.com",
    "mobilePhone": "123456789"
  }
}

Now there is enough information available in here to deserialize to the 2 PHP objects CallbackRequest with nested object Customer thanks to the JSON-LD @context and @type data.

I have written my own denormalizer, but I have to tell the serializer what kind of object I want to deserialize to first, but I can't extract this from the JSON-LD without normalizing it first.

I have searched around a lot looking for anyone who has made a JSON-LD normalizer/denormalizer for the Symfony serializer but have found nothing (with the exception of this functionality inside the API Platform package itself).

Can anybody provide me any pointers, either to a JSON-LD denormalizer package that I can implement, or any suggestions on how to implement the Symfony serializer to denormalize JSON-LD without knowing the what object it is supposed to contain in advance.

Thank you

0 Answers
Related