Skip to content

Commit 586ff09

Browse files
committed
Added more detailed examples to deserialization.md from json_api/deserialization.rb
1 parent 3aeb34b commit 586ff09

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

docs/general/deserialization.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The `ActiveModelSerializers::Deserialization` defines two methods (namely `jsona
1515
- except: `Array` of blacklisted fields
1616
- keys: `Hash` of fields the name of which needs to be modified (e.g. `{ :author => :user, :date => :created_at }`)
1717

18-
Example:
18+
Examples:
1919

2020
```ruby
2121
class PostsController < ActionController::Base
@@ -29,6 +29,72 @@ class PostsController < ActionController::Base
2929
end
3030
```
3131

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+
3298
## Attributes/Json
3399

34100
There is currently no deserialization for those adapters.

0 commit comments

Comments
 (0)