infinum / Japx

Lightweight parser for the complex JSON:API (http://jsonapi.org/) structure.
MIT License
153 stars 35 forks source link

How to encode object that contains meta #27

Closed jcavar closed 5 years ago

jcavar commented 5 years ago

It might be due to my limited knowledge of this library, but I can't seem to find a way to encode object with meta field. If I add meta property to object, it is encoded as attribute (i would say expectedly). I can workaround this by manually inserting meta dictionary to encoded version of the object. But this is not very elegant.

I am using Codable extension.

fgulan commented 5 years ago

Hi @jcavar, thanks for the report! This seems similar to https://github.com/infinum/Japx/issues/10

What we can do here is to check if object has data as nested object and in that case treat meta and included objects as JSON API objects as well.

We didn't want to automatically encode meta and included as JSON API objects since those can represent normal JSON objects in most of the cases.

Handling meta and included only when data is used as nested object seems like a least intrusive way of handling this case. Or we can use some config which could specify wanted behaviour? What do you think @Truba ?

jcavar commented 5 years ago

We didn't want to automatically encode meta and included as JSON API objects since those can represent normal JSON objects in most of the cases.

Could you give an example of this? I thought, when present, meta has to be JSON API meta object.

fgulan commented 5 years ago

Not necessarily, I mean encoding JSON objects to JSON:API objects is not defined by any specification nor standard. So handling meta, included is left to implementation details of library.

For example:

{
    "type": "articles",
    "id": "1",
    "title": "JSON API paints my bikeshed!",
    "body": "The shortest article. Ever.",
    "created": "2015-05-22T14:56:29.000Z",
    "updated": "2015-05-22T14:56:28.000Z",
    "author": {
        "type": "people",
        "id": "42",
        "name": "John",
        "age": 80,
        "gender": "male"
    },
    "meta": {
        "type": "meta",
        "id": "2",
        "name": "Some name"
    }
}

it is unclear should we treat meta as included relationship or JSON:API meta information.

That's why we are considering using flags to let user choose the behavior

jcavar commented 5 years ago

Hmm, I see. Yes, that makes sense.

Truba commented 5 years ago

Hi @jcavar , In encoder there was already a property additionalParams to add additional params to the root near the main data part. You can end up with something like: { "data": ..., additionlParam1: ..., additionalParam2: ... }

We've also added JapxEncodingOptions with includeMetaToCommonNamespce property that would allow you to extract meta at the same level as the attributes and relationships, instead of it ending up in the attributes. It also allows you to extract relationship meta into resource identifiers.

jcavar commented 5 years ago

Very nice, thank you!

fgulan commented 5 years ago

This is released with 2.2.0, closing, thanks for the input @jcavar !