New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make top-level member included optional #6
Comments
You're totally right. I agree, we have to remove |
There can be cases where I have compound documents and I have no need for the included top-level member. I want to have serialized data like in the following example, but I have no need for the {
"links": {
"self": "http://example.com/posts",
"next": "http://example.com/posts?page[offset]=2",
"last": "http://example.com/posts?page[offset]=10"
},
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/posts/1/relationships/author",
"related": "http://example.com/posts/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/posts/1/relationships/comments",
"related": "http://example.com/posts/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/posts/1"
}
}],
// Do not need
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"links": {
"self": "http://example.com/comments/12"
}
}]
} |
Since included should always be an array of resources, checking for Adding "excluded" options could be nice, though it could start to bloat the code. Couldn't you simply not include the comments or people relationship within the data you pass into serialize? Just a thought, trying to keep it simple. |
I still would like to see all the relationships. Another example: JSONAPISerializerConfig = {
typr: 'posts',
attributes: ['title', 'author'],
author: {
ref: 'id',
attributes: ['first-name']
}
} should produce {
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan"
}
}]
} I do not need the |
I've written a quick sample of what the code could look like: I agree it would be a bit of code bloat, but could be refactored in the future when more optional resource members are introduced. |
Fixed with #7 |
According to the JSON API spec http://jsonapi.org/format/#document-top-level:
In my specific use case, I do not want to have an
included
top-level member.A suggestion for passing in what I want to have excluded
Then when building the serialized object, see if the property exists in the array of excluded items.
Thoughts?
The text was updated successfully, but these errors were encountered: