README.md in jsonapi-serializers-0.1.1 vs README.md in jsonapi-serializers-0.1.2
- old
+ new
@@ -75,11 +75,12 @@
"title": "Hello World",
"content": "Your first post"
},
"links": {
"self": "/posts/1"
- }
+ },
+ "relationships": {}
}
}
```
### Serialize a collection
@@ -100,22 +101,24 @@
"title": "Hello World",
"content": "Your first post"
},
"links": {
"self": "/posts/1"
- }
+ },
+ "relationships": {}
},
{
"id": "2",
"type": "posts",
"attributes": {
"title": "Hello World again",
"content": "Your second post"
},
"links": {
"self": "/posts/2"
- }
+ },
+ "relationships": {}
}
]
}
```
@@ -262,20 +265,22 @@
```
Returns:
```json
-
+{
"data": {
"id": "1",
"type": "posts",
"attributes": {
"title": "Hello World",
"content": "Your first post"
},
"links": {
- "self": "/posts/1",
+ "self": "/posts/1"
+ },
+ "relationships": {
"author": {
"self": "/posts/1/links/author",
"related": "/posts/1/author",
"linkage": {
"type": "users",
@@ -301,27 +306,34 @@
"attributes": {
"name": "Post Author"
},
"links": {
"self": "/users/1"
- }
+ },
+ "relationships": {}
},
{
"id": "1",
"type": "comments",
"attributes": {
"content": "Have no fear, sers, your king is safe."
},
"links": {
- "self": "/comments/1",
+ "self": "/comments/1"
+ },
+ "relationships": {
"user": {
"self": "/comments/1/links/user",
"related": "/comments/1/user",
"linkage": {
"type": "users",
"id": "2"
}
+ },
+ "post": {
+ "self": "/comments/1/links/post",
+ "related": "/comments/1/post"
}
}
},
{
"id": "2",
@@ -329,22 +341,23 @@
"attributes": {
"name": "Barristan Selmy"
},
"links": {
"self": "/users/2"
- }
+ },
+ "relationships": {}
}
]
}
```
Notice a few things:
-* The [primary data](http://jsonapi.org/format/#document-structure-top-level) now includes "linkage" information for each relationship that was included.
+* The [primary data](http://jsonapi.org/format/#document-structure-top-level) relationships now include "linkage" information for each relationship that was included.
* The related objects themselves are loaded in the top-level `included` member.
* The related objects _also_ include "linkage" information when a deeper relationship is also present in the compound document. This is a very powerful feature of the JSON:API spec, and allows you to deeply link complicated relationships all in the same document and in a single HTTP response. JSONAPI::Serializers automatically includes the correct linkage information for whatever `include` paths you specify. This conforms to this part of the spec:
- > Note: Resource linkage in a compound document allows a client to link together all of the included resource objects without having to GET any relationship URLs.
- > [JSON:API Resource Relationships](http://jsonapi.org/format/#document-structure-resource-relationships)
+ > Note: Full linkage ensures that included resources are related to either the primary data (which could be resource objects or resource identifier objects) or to each other.
+ > [JSON:API Compound Documents](http://jsonapi.org/format/#document-structure-compound-documents)
#### Relationship path handling
The `include` param also accepts a string of [relationship paths](http://jsonapi.org/format/#fetching-includes), ie. `include: 'author,comments,comments.user'` so you can pass an `?include` query param directly through to the serialize method. Be aware that letting users pass arbitrary relationship paths might introduce security issues depending on your authorization setup, where a user could `include` a relationship they might not be authorized to see directly. Be aware of what you allow API users to include.