README.md in jsonapi-serializers-0.15.0 vs README.md in jsonapi-serializers-0.16.0
- old
+ new
@@ -16,16 +16,17 @@
* [Null handling](#null-handling)
* [Multiple attributes](#multiple-attributes)
* [Custom attributes](#custom-attributes)
* [More customizations](#more-customizations)
* [Base URL](#base-url)
- * [Root jsonapi object](#root-jsonapi-object)
* [Root metadata](#root-metadata)
* [Root links](#root-links)
* [Root errors](#root-errors)
+ * [Root jsonapi object](#root-jsonapi-object)
* [Explicit serializer discovery](#explicit-serializer-discovery)
* [Namespace serializers](#namespace-serializers)
+ * [Sparse fieldsets](#sparse-fieldsets)
* [Relationships](#relationships)
* [Compound documents and includes](#compound-documents-and-includes)
* [Relationship path handling](#relationship-path-handling)
* [Control links and data in relationships](#control-links-and-data-in-relationships)
* [Rails example](#rails-example)
@@ -316,18 +317,10 @@
JSONAPI::Serializer.serialize(post, base_url: 'http://example.com')
```
Note: if you override `self_link` in your serializer and leave out `base_url`, it will not be included.
-### Root 'jsonapi' object
-
-You can pass a `jsonapi` argument to specify a [top-level "jsonapi" key](http://jsonapi.org/format/#document-jsonapi-object) containing the version of JSON:API in use:
-
-```ruby
-JSONAPI::Serializer.serialize(post, jsonapi: {version: '1.0'})
-```
-
### Root metadata
You can pass a `meta` argument to specify top-level metadata:
```ruby
@@ -370,10 +363,18 @@
end
end
end
```
+### Root 'jsonapi' object
+
+You can pass a `jsonapi` argument to specify a [top-level "jsonapi" key](http://jsonapi.org/format/#document-jsonapi-object) containing the version of JSON:API in use:
+
+```ruby
+JSONAPI::Serializer.serialize(post, jsonapi: {version: '1.0'})
+```
+
### Explicit serializer discovery
By default, jsonapi-serializers assumes that the serializer class for `Namespace::User` is `Namespace::UserSerializer`. You can override this behavior on a per-object basis by implementing the `jsonapi_serializer_class_name` method.
```ruby
@@ -414,10 +415,39 @@
JSONAPI::Serializer.serialize(post, namespace: Api::V2)
```
This option overrides the `jsonapi_serializer_class_name` method.
+### Sparse fieldsets
+
+The JSON:API spec allows to return only [specific fields](http://jsonapi.org/format/#fetching-sparse-fieldsets) from attributes and relationships.
+
+For example, if you wanted to return only the `title` field and `author` relationship link for `posts`:
+
+```ruby
+fields =
+JSONAPI::Serializer.serialize(post, fields: {posts: [:title]})
+```
+
+Sparse fieldsets also affect relationship links. In this case, only the `author` relationship link would be included:
+
+``` ruby
+JSONAPI::Serializer.serialize(post, fields: {posts: [:title, :author]})
+```
+
+Sparse fieldsets operate on a per-type basis, so they affect all resources in the response including in compound documents. For example, this will affect both the `posts` type in the primary data and the `users` type in the compound data:
+
+``` ruby
+JSONAPI::Serializer.serialize(
+ post,
+ fields: {posts: ['title', 'author'], users: ['name']},
+ include: 'author',
+)
+```
+
+Sparse fieldsets support comma-separated strings (`fields: {posts: 'title,author'}`, arrays of strings (`fields: {posts: ['title', 'author']}`), single symbols (`fields: {posts: :title}`), and arrays of symbols (`fields: {posts: [:title, :author]}`).
+
## Relationships
You can easily specify relationships with the `has_one` and `has_many` directives.
```ruby
@@ -781,10 +811,9 @@
See [Releases](https://github.com/fotinakis/jsonapi-serializers/releases).
## Unfinished business
-* Support for the `fields` spec is planned, would love a PR contribution for this.
* Support for pagination/sorting is unlikely to be supported because it would likely involve coupling to ActiveRecord, but please open an issue if you have ideas of how to support this generically.
## Contributing
1. Fork it ( https://github.com/fotinakis/jsonapi-serializers/fork )