jsonapi-serializer icon indicating copy to clipboard operation
jsonapi-serializer copied to clipboard

How to keep top level meta object after deserialization?

Open rubenkuipers opened this issue 7 years ago • 8 comments

In my API response I have a top level 'meta' object (including pagination info) and a top level 'data' object. How can I keep the 'meta' object after deserialization?

rubenkuipers avatar Jun 16 '17 10:06 rubenkuipers

Same here, no one can help?

schermannj avatar Jun 23 '17 11:06 schermannj

@kuiperr005 @schermannj How did you guys end up solving the issue?

Princu7 avatar Jul 02 '17 01:07 Princu7

@Princu7 Not in the best way in my case. Something like this: `
...

 request.then((resp) => { // resp is a jso-api serialized data object
      const data = new JSONAPIDeserializer().deserialize(resp);
      Object.assign(data, { meta: resp.meta });
      console.log(data);
 })

` So if it's possible to fix it somehow, it would be great.

schermannj avatar Jul 02 '17 05:07 schermannj

I did exactly like @Princu7, not the best way in my opinion too.

rubenkuipers avatar Jul 02 '17 11:07 rubenkuipers

@schermannj @kuiperr005 Thanks for the response Guys. I did a little investigation on this issue and the reason why it is like that is that the meta object is not meant to be deserialized. It is not a part of the original data which was serialized. It just contains some additional information about the data we received.

If we want the top-level meta object to deserialize, we can follow the same approach that has been done to deserialize the top-level links object. It's very easy. See this PR https://github.com/SeyZ/jsonapi-serializer/commit/12bd9bc5ce64e75f0a9f6b6e7d8fa56c2d64eaf3 Just at the point where the extra lines have been added, add these lines below it:-

if (jsonapi.meta) {
  record.meta = jsonapi.meta;
}

It should work fine :)

Princu7 avatar Jul 02 '17 11:07 Princu7

@Princu7 It sounds fine, when are you going to push these changes?

schermannj avatar Jul 02 '17 11:07 schermannj

@SeyZ I think this issue is related to https://github.com/SeyZ/jsonapi-serializer/issues/84. The links that are currently supported could be moved to record.meta.links maybe?

schechter avatar Sep 21 '17 14:09 schechter

Has this been implemented yet? Or if not, any alternative workarounds?

ajmathews avatar Feb 23 '18 21:02 ajmathews