|
| 1 | +[Back to Guides](../README.md) |
| 2 | + |
| 3 | +# Deserialization |
| 4 | + |
| 5 | +This is currently an *experimental* feature. The interface may change. |
| 6 | + |
| 7 | +## JSON API |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +- Parameters |
| 12 | + - document: `Hash` or `ActionController::Parameters` instance |
| 13 | + - options: |
| 14 | + - only: `Array` of whitelisted fields |
| 15 | + - except: `Array` of blacklisted fields |
| 16 | + - keys: `Hash` of fields the name of which needs to be modified (e.g. `{ :author => :user, :date => :created_at }`) |
| 17 | + |
| 18 | +Examples: |
| 19 | + |
| 20 | +```ruby |
| 21 | +class PostsController < ActionController::Base |
| 22 | + def create |
| 23 | + Post.create(create_params) |
| 24 | + end |
| 25 | + |
| 26 | + def create_params |
| 27 | + ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:title, :content, :author]) |
| 28 | + end |
| 29 | +end |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +Given a JSON API document, |
| 35 | + |
| 36 | +``` |
| 37 | +document = { |
| 38 | + data: { |
| 39 | + id: 1, |
| 40 | + type: 'post', |
| 41 | + attributes: { |
| 42 | + title: 'Title 1', |
| 43 | + date: '2015-12-20' |
| 44 | + }, |
| 45 | + associations: { |
| 46 | + author: { |
| 47 | + data: { |
| 48 | + type: 'user', |
| 49 | + id: 2 |
| 50 | + } |
| 51 | + }, |
| 52 | + second_author: { |
| 53 | + data: nil |
| 54 | + }, |
| 55 | + comments: { |
| 56 | + data: [{ |
| 57 | + type: 'comment', |
| 58 | + id: 3 |
| 59 | + },{ |
| 60 | + type: 'comment', |
| 61 | + id: 4 |
| 62 | + }] |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The entire document can be parsed without specifying any options: |
| 70 | +```ruby |
| 71 | +ActiveModelSerializers::Deserialization.jsonapi_parse(document) |
| 72 | +#=> |
| 73 | +# { |
| 74 | +# title: 'Title 1', |
| 75 | +# date: '2015-12-20', |
| 76 | +# author_id: 2, |
| 77 | +# second_author_id: nil |
| 78 | +# comment_ids: [3, 4] |
| 79 | +# } |
| 80 | +``` |
| 81 | + |
| 82 | +and fields, relationships, and polymorphic relationships can be specified via the options: |
| 83 | + |
| 84 | +```ruby |
| 85 | +ActiveModelSerializers::Deserialization |
| 86 | + .jsonapi_parse(document, only: [:title, :date, :author], |
| 87 | + keys: { date: :published_at }, |
| 88 | + polymorphic: [:author]) |
| 89 | +#=> |
| 90 | +# { |
| 91 | +# title: 'Title 1', |
| 92 | +# published_at: '2015-12-20', |
| 93 | +# author_id: '2', |
| 94 | +# author_type: 'user' |
| 95 | +# } |
| 96 | +``` |
| 97 | + |
| 98 | +## Attributes/Json |
| 99 | + |
| 100 | +There is currently no deserialization for those adapters. |
0 commit comments