-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add docs for deserialization. #1431
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
[Back to Guides](../README.md) | ||
|
||
# Deserialization | ||
|
||
This is currently an *experimental* feature. The interface may change. | ||
|
||
## JSON API | ||
|
||
The `ActiveModelSerializers::Deserialization` defines two methods (namely `jsonapi_parse` and `jsonapi_parse!`), which take a `Hash` or an instance of `ActionController::Parameters` representing a JSON API payload, and return a hash that can directly be used to create/update models. The bang version throws an `InvalidDocument` exception when parsing fails, whereas the "safe" version simply returns an empty hash. | ||
|
||
- Parameters | ||
- document: `Hash` or `ActionController::Parameters` instance | ||
- options: | ||
- only: `Array` of whitelisted fields | ||
- except: `Array` of blacklisted fields | ||
- keys: `Hash` of fields the name of which needs to be modified (e.g. `{ :author => :user, :date => :created_at }`) | ||
|
||
Examples: | ||
|
||
```ruby | ||
class PostsController < ActionController::Base | ||
def create | ||
Post.create(create_params) | ||
end | ||
|
||
def create_params | ||
ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:title, :content, :author]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you include an example request that this would apply to? e.g. POST /posts HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
// whatever
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, that would be great! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @beauby , seeing the data before hand would help understand how much Deserialization is helping out :-) once it's explained that both fields AND relationships can be passed in here, I think this'll be ready for merge :-) |
||
end | ||
end | ||
``` | ||
|
||
|
||
|
||
Given a JSON API document, | ||
|
||
``` | ||
document = { | ||
data: { | ||
id: 1, | ||
type: 'post', | ||
attributes: { | ||
title: 'Title 1', | ||
date: '2015-12-20' | ||
}, | ||
associations: { | ||
author: { | ||
data: { | ||
type: 'user', | ||
id: 2 | ||
} | ||
}, | ||
second_author: { | ||
data: nil | ||
}, | ||
comments: { | ||
data: [{ | ||
type: 'comment', | ||
id: 3 | ||
},{ | ||
type: 'comment', | ||
id: 4 | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The entire document can be parsed without specifying any options: | ||
```ruby | ||
ActiveModelSerializers::Deserialization.jsonapi_parse(document) | ||
#=> | ||
# { | ||
# title: 'Title 1', | ||
# date: '2015-12-20', | ||
# author_id: 2, | ||
# second_author_id: nil | ||
# comment_ids: [3, 4] | ||
# } | ||
``` | ||
|
||
and fields, relationships, and polymorphic relationships can be specified via the options: | ||
|
||
```ruby | ||
ActiveModelSerializers::Deserialization | ||
.jsonapi_parse(document, only: [:title, :date, :author], | ||
keys: { date: :published_at }, | ||
polymorphic: [:author]) | ||
#=> | ||
# { | ||
# title: 'Title 1', | ||
# published_at: '2015-12-20', | ||
# author_id: '2', | ||
# author_type: 'user' | ||
# } | ||
``` | ||
|
||
## Attributes/Json | ||
|
||
There is currently no deserialization for those adapters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ Loving this