json-serializer 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +18 -0
- data/LICENSE +1 -1
- data/README.md +20 -211
- data/lib/json_serializer.rb +19 -25
- metadata +27 -48
- data/test/association_test.rb +0 -178
- data/test/attribute_test.rb +0 -65
- data/test/helper.rb +0 -13
- data/test/root_test.rb +0 -25
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 263746b04fd8d125e5d61626f3fd938756c8de2fb2a92e154c583dc0abe3c6e5
|
|
4
|
+
data.tar.gz: 6865cb2c98aa5c1b24038f71d5cdddda334352599b841b727171f543cb70886b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cbeb264a05fac707e4e83d73dbdca045de84436b9d6a92234469d3392b178372d93ab7d813543982458a3581cb3b3ed20cddc8452737e6f13f0e4855b850923e
|
|
7
|
+
data.tar.gz: 47510b5662a1ed199cb120e09cb8dc8da197d6b6ad7f24915638a94d91d3f505fdd7372621229641ffd4704a78a5d6c8ee6c9baf1e6a27c84850ab946af2dbee
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.0.0 (2026-11-30)
|
|
4
|
+
|
|
5
|
+
### Breaking changes
|
|
6
|
+
|
|
7
|
+
- Requires Ruby 3.3 or later, the oldest Ruby that is not end of life.
|
|
8
|
+
- Attributes read from the object with `public_send`. A private method listed as an attribute raises `NoMethodError` instead of ending up in the JSON.
|
|
9
|
+
- Only objects that respond to `to_ary`, like arrays and ActiveRecord relations, serialize as lists. Structs and hashes serialize as objects.
|
|
10
|
+
- `JsonSerializer::Utils` is removed.
|
|
11
|
+
|
|
12
|
+
### Fixes
|
|
13
|
+
|
|
14
|
+
- Structs serialize as objects instead of raising `NoMethodError`. This is the bug from [#2](https://github.com/frodsan/json-serializer/issues/2), which 1.0.0 did not fix.
|
|
15
|
+
- Serializers nest inside other JSON, like `{ data: UserSerializer.new(user) }.to_json`.
|
|
16
|
+
- A subclass can change the serializer of an inherited attribute.
|
|
17
|
+
- An attribute named like an `Object` method, such as `hash`, reads from the object.
|
|
18
|
+
- `nil` inside an array serializes as `null`.
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,238 +1,47 @@
|
|
|
1
|
-
json-serializer [](https://github.com/frodsan/json-serializer/actions/workflows/ci.yml)
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
A Ruby JSON serializer in under [50 lines of code](lib/json_serializer.rb).
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
------------
|
|
8
|
-
|
|
9
|
-
Add this line to your application's Gemfile:
|
|
10
|
-
|
|
11
|
-
```ruby
|
|
12
|
-
gem "json-serializer"
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
And then execute:
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
$ bundle
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Or install it yourself as:
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
$ gem install json-serializer
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
Usage
|
|
28
|
-
-----
|
|
29
|
-
|
|
30
|
-
Here's a simple example:
|
|
5
|
+
## Usage
|
|
31
6
|
|
|
32
7
|
```ruby
|
|
33
8
|
require "json_serializer"
|
|
34
9
|
|
|
35
10
|
class UserSerializer < JsonSerializer
|
|
36
11
|
attribute :id
|
|
37
|
-
attribute :
|
|
38
|
-
attribute :last_name
|
|
12
|
+
attribute :name
|
|
39
13
|
end
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
In this case, we defined a new serializer class and specified the attributes
|
|
43
|
-
we would like to include in the serialized form.
|
|
44
|
-
|
|
45
|
-
```ruby
|
|
46
|
-
user = User.create(first_name: "Sonny", last_name: "Moore", admin: true)
|
|
47
|
-
|
|
48
|
-
UserSerializer.new(user).to_json
|
|
49
|
-
# => "{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}"
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
You can add a root to the outputted json through the `:root` option:
|
|
53
|
-
|
|
54
|
-
```ruby
|
|
55
|
-
user = User.create(first_name: "Sonny", last_name: "Moore", admin: true)
|
|
56
|
-
|
|
57
|
-
UserSerializer.new(user).to_json(root: :user)
|
|
58
|
-
# => "{\"user\":{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}}"
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
Arrays
|
|
62
|
-
------
|
|
63
|
-
|
|
64
|
-
A serializer can be used for objects contained in an array:
|
|
65
|
-
|
|
66
|
-
```ruby
|
|
67
|
-
require "json_serializer"
|
|
68
14
|
|
|
69
15
|
class PostSerializer < JsonSerializer
|
|
70
16
|
attribute :id
|
|
71
17
|
attribute :title
|
|
72
|
-
attribute :
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
posts = []
|
|
76
|
-
posts << Post.create(title: "Post 1", body: "Hello!")
|
|
77
|
-
posts << Post.create(title: "Post 2", body: "Goodbye!")
|
|
78
|
-
|
|
79
|
-
PostSerializer.new(posts).to_json
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
Given the example above, it will return a json output like:
|
|
83
|
-
|
|
84
|
-
```json
|
|
85
|
-
[
|
|
86
|
-
{ "id": 1, "title": "Post 1", "body": "Hello!" },
|
|
87
|
-
{ "id": 2, "title": "Post 2", "body": "Goodbye!" }
|
|
88
|
-
]
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
Attributes
|
|
92
|
-
----------
|
|
93
|
-
|
|
94
|
-
By default, before looking up the attribute on the object, it checks the presence
|
|
95
|
-
of a method with the name of the attribute. This allow serializes to include
|
|
96
|
-
properties in addition to the object attributes or customize the result of a
|
|
97
|
-
specified attribute. You can access the object being serialized with the +object+
|
|
98
|
-
method.
|
|
99
|
-
|
|
100
|
-
```ruby
|
|
101
|
-
require "json_serializer"
|
|
102
|
-
|
|
103
|
-
class UserSerializer < JsonSerializer
|
|
104
|
-
attribute :id
|
|
105
|
-
attribute :first_name
|
|
106
|
-
attribute :last_name
|
|
107
|
-
attribute :full_name
|
|
108
|
-
|
|
109
|
-
def full_name
|
|
110
|
-
object.first_name + " " + object.last_name
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
user = User.create(first_name: "Sonny", last_name: "Moore")
|
|
115
|
-
|
|
116
|
-
UserSerializer.new(user).to_json
|
|
117
|
-
# => "{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\",\"full_name\":\"Sonny Moore\"}"
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
If you would like direct, low-level control of attribute serialization, you can
|
|
121
|
-
completely override `to_hash` method to return the hash you need:
|
|
122
|
-
|
|
123
|
-
```ruby
|
|
124
|
-
require "json_serializer"
|
|
125
|
-
|
|
126
|
-
class UserSerializer < JsonSerializer
|
|
127
|
-
attribute :id
|
|
128
|
-
attribute :first_name
|
|
129
|
-
attribute :last_name
|
|
130
|
-
|
|
131
|
-
attr_reader :current_user
|
|
132
|
-
|
|
133
|
-
def initialize(object, current_user)
|
|
134
|
-
super(object)
|
|
135
|
-
@current_user = current_user
|
|
136
|
-
end
|
|
18
|
+
attribute :author, :UserSerializer
|
|
19
|
+
attribute :comments, :CommentSerializer
|
|
137
20
|
|
|
138
|
-
def
|
|
139
|
-
|
|
140
|
-
hash.merge!(admin: object.admin) if current_user.admin?
|
|
141
|
-
hash
|
|
21
|
+
def title
|
|
22
|
+
object.title.upcase
|
|
142
23
|
end
|
|
143
24
|
end
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
Attributes with Custom Serializer
|
|
147
|
-
---------------------------------
|
|
148
|
-
|
|
149
|
-
You can specify a serializer class for a defined attribute. This is very useful
|
|
150
|
-
for serializing each element of an association.
|
|
151
|
-
|
|
152
|
-
```ruby
|
|
153
|
-
require "json_serializer"
|
|
154
|
-
|
|
155
|
-
class UserSerializer < JsonSerializer
|
|
156
|
-
attribute :id
|
|
157
|
-
attribute :username
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
class PostSerializer < JsonSerializer
|
|
161
|
-
attribute :id
|
|
162
|
-
attribute :title
|
|
163
|
-
attribute :user, :UserSerializer
|
|
164
|
-
attribute :comments, :CommentSerializer
|
|
165
|
-
end
|
|
166
25
|
|
|
167
26
|
class CommentSerializer < JsonSerializer
|
|
168
|
-
attribute :
|
|
169
|
-
attribute :content
|
|
170
|
-
attribute :user, :UserSerializer
|
|
27
|
+
attribute :body
|
|
171
28
|
end
|
|
172
29
|
|
|
173
|
-
admin = User.create(username: "admin", admin: true)
|
|
174
|
-
user = User.create(username: "user")
|
|
175
|
-
|
|
176
|
-
post = Post.create(title: "Hello!", user: admin)
|
|
177
|
-
post.comments << Comment.create(content: "First comment", user: user)
|
|
178
|
-
|
|
179
30
|
PostSerializer.new(post).to_json
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
The example above returns the following json output:
|
|
183
|
-
|
|
184
|
-
```json
|
|
185
|
-
{
|
|
186
|
-
"id": 1,
|
|
187
|
-
"title": "Hello!",
|
|
188
|
-
"user":
|
|
189
|
-
{
|
|
190
|
-
"id": 1,
|
|
191
|
-
"username": "admin"
|
|
192
|
-
},
|
|
193
|
-
"comments":
|
|
194
|
-
[
|
|
195
|
-
{
|
|
196
|
-
"id": 1,
|
|
197
|
-
"content": "First comment",
|
|
198
|
-
"user":
|
|
199
|
-
{
|
|
200
|
-
"id": 2,
|
|
201
|
-
"username": "user"
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
]
|
|
205
|
-
}
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
Contributing
|
|
209
|
-
------------
|
|
210
|
-
|
|
211
|
-
Fork the project with:
|
|
212
|
-
|
|
213
|
-
```
|
|
214
|
-
$ git clone git@github.com:frodsan/json-serializer.git
|
|
215
|
-
```
|
|
31
|
+
# => {"id":1,"title":"HELLO","author":{"id":1,"name":"Jane"},"comments":[{"body":"Nice post"}]}
|
|
216
32
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
```
|
|
220
|
-
$ bundle install
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
To run the test suite, do:
|
|
224
|
-
|
|
225
|
-
```
|
|
226
|
-
$ rake test
|
|
33
|
+
PostSerializer.new([post]).to_json(root: :posts)
|
|
34
|
+
# => {"posts":[{"id":1,"title":"HELLO","author":{"id":1,"name":"Jane"},"comments":[{"body":"Nice post"}]}]}
|
|
227
35
|
```
|
|
228
36
|
|
|
229
|
-
|
|
37
|
+
## What it does
|
|
230
38
|
|
|
231
|
-
|
|
232
|
-
|
|
39
|
+
- **Nested serializers**: `attribute :author, :UserSerializer` serializes the value with another serializer.
|
|
40
|
+
- **Arrays and enumerables**: anything that responds to `to_a` is serialized as a list.
|
|
41
|
+
- **Custom attributes**: a method on the serializer with the attribute name wins over the one on the object. Inside it, `object` is the object being serialized.
|
|
42
|
+
- **Inheritance**: a subclass gets its parent's attributes and can add its own without changing the parent.
|
|
43
|
+
- **Root key**: `to_json(root: :posts)` wraps the output in a `posts` key.
|
|
233
44
|
|
|
234
|
-
|
|
45
|
+
## Why
|
|
235
46
|
|
|
236
|
-
|
|
237
|
-
[mit]: http://www.opensource.org/licenses/MIT
|
|
238
|
-
[issues]: https://github.com/frodsan/json-serializer/issues
|
|
47
|
+
I wrote json-serializer to see how little Ruby code it takes to write a JSON serializer.
|
data/lib/json_serializer.rb
CHANGED
|
@@ -3,24 +3,12 @@
|
|
|
3
3
|
require "json"
|
|
4
4
|
|
|
5
5
|
class JsonSerializer
|
|
6
|
-
module Utils
|
|
7
|
-
def self.const(context, name)
|
|
8
|
-
case name
|
|
9
|
-
when Symbol, String
|
|
10
|
-
context.const_get(name)
|
|
11
|
-
else name
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
6
|
def self.inherited(subclass)
|
|
17
|
-
attributes.
|
|
18
|
-
subclass.attribute(name, serializer)
|
|
19
|
-
end
|
|
7
|
+
subclass.attributes.merge!(attributes)
|
|
20
8
|
end
|
|
21
9
|
|
|
22
10
|
def self.attribute(name, serializer = nil)
|
|
23
|
-
attributes[name]
|
|
11
|
+
attributes[name] = serializer
|
|
24
12
|
end
|
|
25
13
|
|
|
26
14
|
def self.attributes
|
|
@@ -33,35 +21,41 @@ class JsonSerializer
|
|
|
33
21
|
@object = object
|
|
34
22
|
end
|
|
35
23
|
|
|
36
|
-
def to_json(root: nil)
|
|
24
|
+
def to_json(*args, root: nil)
|
|
37
25
|
result = decorate
|
|
38
26
|
result = { root => result } if root
|
|
39
27
|
|
|
40
|
-
result.to_json
|
|
28
|
+
result.to_json(*args)
|
|
41
29
|
end
|
|
42
30
|
|
|
43
31
|
def decorate
|
|
44
32
|
return nil unless object
|
|
45
33
|
|
|
46
|
-
|
|
47
|
-
to_arry
|
|
48
|
-
else
|
|
49
|
-
to_hash
|
|
50
|
-
end
|
|
34
|
+
object.respond_to?(:to_ary) ? to_array : to_hash
|
|
51
35
|
end
|
|
52
36
|
|
|
53
37
|
protected
|
|
54
38
|
|
|
55
|
-
def
|
|
56
|
-
object.
|
|
39
|
+
def to_array
|
|
40
|
+
object.to_ary.map { |o| self.class.new(o).decorate }
|
|
57
41
|
end
|
|
58
42
|
|
|
59
43
|
def to_hash
|
|
60
44
|
self.class.attributes.each_with_object({}) do |(name, serializer), hash|
|
|
61
|
-
res =
|
|
62
|
-
res =
|
|
45
|
+
res = serializer_method?(name) ? send(name) : object.public_send(name)
|
|
46
|
+
res = serializer_class(serializer).new(res).decorate if serializer
|
|
63
47
|
|
|
64
48
|
hash[name] = res
|
|
65
49
|
end
|
|
66
50
|
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def serializer_method?(name)
|
|
55
|
+
self.class.method_defined?(name) && !JsonSerializer.method_defined?(name)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def serializer_class(serializer)
|
|
59
|
+
serializer.is_a?(Class) ? serializer : self.class.const_get(serializer)
|
|
60
|
+
end
|
|
67
61
|
end
|
metadata
CHANGED
|
@@ -1,89 +1,74 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json-serializer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Francesco Rodríguez
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: minitest
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '0'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
27
|
+
name: ostruct
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '0'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
39
|
+
version: '0'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: rake
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
|
-
- - "
|
|
44
|
+
- - ">="
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '
|
|
46
|
+
version: '0'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
|
-
- - "
|
|
51
|
+
- - ">="
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
|
58
|
-
requirements:
|
|
59
|
-
- - "~>"
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: '0.39'
|
|
62
|
-
type: :development
|
|
63
|
-
prerelease: false
|
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
-
requirements:
|
|
66
|
-
- - "~>"
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: '0.39'
|
|
69
|
-
description: Customize JSON ouput through serializer objects
|
|
70
|
-
email: hello@frodsan.com
|
|
53
|
+
version: '0'
|
|
54
|
+
description: A Ruby JSON serializer in under 50 lines of code
|
|
55
|
+
email: frodsan@me.com
|
|
71
56
|
executables: []
|
|
72
57
|
extensions: []
|
|
73
58
|
extra_rdoc_files: []
|
|
74
59
|
files:
|
|
60
|
+
- CHANGELOG.md
|
|
75
61
|
- LICENSE
|
|
76
62
|
- README.md
|
|
77
63
|
- lib/json_serializer.rb
|
|
78
|
-
- test/association_test.rb
|
|
79
|
-
- test/attribute_test.rb
|
|
80
|
-
- test/helper.rb
|
|
81
|
-
- test/root_test.rb
|
|
82
64
|
homepage: https://github.com/frodsan/json-serializer
|
|
83
65
|
licenses:
|
|
84
66
|
- MIT
|
|
85
|
-
metadata:
|
|
86
|
-
|
|
67
|
+
metadata:
|
|
68
|
+
source_code_uri: https://github.com/frodsan/json-serializer
|
|
69
|
+
changelog_uri: https://github.com/frodsan/json-serializer/blob/master/CHANGELOG.md
|
|
70
|
+
bug_tracker_uri: https://github.com/frodsan/json-serializer/issues
|
|
71
|
+
rubygems_mfa_required: 'true'
|
|
87
72
|
rdoc_options: []
|
|
88
73
|
require_paths:
|
|
89
74
|
- lib
|
|
@@ -91,20 +76,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
91
76
|
requirements:
|
|
92
77
|
- - ">="
|
|
93
78
|
- !ruby/object:Gem::Version
|
|
94
|
-
version: '
|
|
79
|
+
version: '3.3'
|
|
95
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
81
|
requirements:
|
|
97
82
|
- - ">="
|
|
98
83
|
- !ruby/object:Gem::Version
|
|
99
84
|
version: '0'
|
|
100
85
|
requirements: []
|
|
101
|
-
|
|
102
|
-
rubygems_version: 2.5.1
|
|
103
|
-
signing_key:
|
|
86
|
+
rubygems_version: 4.0.3
|
|
104
87
|
specification_version: 4
|
|
105
|
-
summary:
|
|
106
|
-
test_files:
|
|
107
|
-
- test/association_test.rb
|
|
108
|
-
- test/attribute_test.rb
|
|
109
|
-
- test/helper.rb
|
|
110
|
-
- test/root_test.rb
|
|
88
|
+
summary: A Ruby JSON serializer in under 50 lines of code
|
|
89
|
+
test_files: []
|
data/test/association_test.rb
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "helper"
|
|
4
|
-
|
|
5
|
-
class OrganizationSerializer < JsonSerializer
|
|
6
|
-
attribute :id
|
|
7
|
-
attribute :name
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
class UserWithOrganizationSerializer < JsonSerializer
|
|
11
|
-
attribute :id
|
|
12
|
-
attribute :name
|
|
13
|
-
attribute :organization, :OrganizationSerializer
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
class UserWithCustomOrganizationSerializer < JsonSerializer
|
|
17
|
-
attribute :organizations, :OrganizationSerializer
|
|
18
|
-
|
|
19
|
-
def organizations
|
|
20
|
-
[Organization.new(id: 1, name: "enterprise")]
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# rubocop:disable ClassLength
|
|
25
|
-
class AssociationTest < Minitest::Test
|
|
26
|
-
test "serializes object with association" do
|
|
27
|
-
user = User.new(id: 1, name: "sonny")
|
|
28
|
-
user.organization = Organization.new(id: 1, name: "enterprise")
|
|
29
|
-
|
|
30
|
-
result = {
|
|
31
|
-
id: 1,
|
|
32
|
-
name: "sonny",
|
|
33
|
-
organization: {
|
|
34
|
-
id: 1,
|
|
35
|
-
name: "enterprise"
|
|
36
|
-
}
|
|
37
|
-
}.to_json
|
|
38
|
-
|
|
39
|
-
assert_equal result, UserWithOrganizationSerializer.new(user).to_json
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
test "serializes object with a nil association" do
|
|
43
|
-
user = User.new(id: 1, name: "sonny")
|
|
44
|
-
user.organization = nil
|
|
45
|
-
|
|
46
|
-
result = {
|
|
47
|
-
id: 1,
|
|
48
|
-
name: "sonny",
|
|
49
|
-
organization: nil
|
|
50
|
-
}.to_json
|
|
51
|
-
|
|
52
|
-
assert_equal result, UserWithOrganizationSerializer.new(user).to_json
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
test "serializes array with association" do
|
|
56
|
-
users = [
|
|
57
|
-
User.new(
|
|
58
|
-
id: 1,
|
|
59
|
-
name: "sonny",
|
|
60
|
-
organization: Organization.new(id: 1, name: "enterprise")
|
|
61
|
-
),
|
|
62
|
-
User.new(
|
|
63
|
-
id: 2,
|
|
64
|
-
name: "anton",
|
|
65
|
-
organization: Organization.new(id: 2, name: "evil")
|
|
66
|
-
)
|
|
67
|
-
]
|
|
68
|
-
|
|
69
|
-
result = [
|
|
70
|
-
{
|
|
71
|
-
id: 1,
|
|
72
|
-
name: "sonny",
|
|
73
|
-
organization: {
|
|
74
|
-
id: 1,
|
|
75
|
-
name: "enterprise"
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
id: 2,
|
|
80
|
-
name: "anton",
|
|
81
|
-
organization: {
|
|
82
|
-
id: 2,
|
|
83
|
-
name: "evil"
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
].to_json
|
|
87
|
-
|
|
88
|
-
assert_equal result, UserWithOrganizationSerializer.new(users).to_json
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
class UserWithOrganizationsSerializer < JsonSerializer
|
|
92
|
-
attribute :id
|
|
93
|
-
attribute :name
|
|
94
|
-
attribute :organizations, :OrganizationSerializer
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
test "serializes object with collection" do
|
|
98
|
-
user = User.new(id: 1, name: "sonny")
|
|
99
|
-
user.organizations = [
|
|
100
|
-
Organization.new(id: 1, name: "enterprise"),
|
|
101
|
-
Organization.new(id: 2, name: "evil")
|
|
102
|
-
]
|
|
103
|
-
|
|
104
|
-
result = {
|
|
105
|
-
id: 1,
|
|
106
|
-
name: "sonny",
|
|
107
|
-
organizations: [
|
|
108
|
-
{
|
|
109
|
-
id: 1,
|
|
110
|
-
name: "enterprise"
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
id: 2,
|
|
114
|
-
name: "evil"
|
|
115
|
-
}
|
|
116
|
-
]
|
|
117
|
-
}.to_json
|
|
118
|
-
|
|
119
|
-
assert_equal result, UserWithOrganizationsSerializer.new(user).to_json
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
test "serializes array with nested collections" do
|
|
123
|
-
users = [
|
|
124
|
-
User.new(
|
|
125
|
-
id: 1,
|
|
126
|
-
name: "sonny",
|
|
127
|
-
organizations: [
|
|
128
|
-
Organization.new(id: 1, name: "enterprise"),
|
|
129
|
-
Organization.new(id: 2, name: "evil")
|
|
130
|
-
]
|
|
131
|
-
),
|
|
132
|
-
User.new(
|
|
133
|
-
id: 2,
|
|
134
|
-
name: "anton",
|
|
135
|
-
organizations: [
|
|
136
|
-
Organization.new(id: 3, name: "showtek")
|
|
137
|
-
]
|
|
138
|
-
)
|
|
139
|
-
]
|
|
140
|
-
|
|
141
|
-
result = [
|
|
142
|
-
{
|
|
143
|
-
id: 1,
|
|
144
|
-
name: "sonny",
|
|
145
|
-
organizations: [
|
|
146
|
-
{
|
|
147
|
-
id: 1,
|
|
148
|
-
name: "enterprise"
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
id: 2,
|
|
152
|
-
name: "evil"
|
|
153
|
-
}
|
|
154
|
-
]
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
id: 2,
|
|
158
|
-
name: "anton",
|
|
159
|
-
organizations: [
|
|
160
|
-
{
|
|
161
|
-
id: 3,
|
|
162
|
-
name: "showtek"
|
|
163
|
-
}
|
|
164
|
-
]
|
|
165
|
-
}
|
|
166
|
-
].to_json
|
|
167
|
-
|
|
168
|
-
assert_equal result, UserWithOrganizationsSerializer.new(users).to_json
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
test "implements association method and returns different result" do
|
|
172
|
-
user = User.new
|
|
173
|
-
|
|
174
|
-
result = { organizations: [{ id: 1, name: "enterprise" }] }.to_json
|
|
175
|
-
|
|
176
|
-
assert_equal result, UserWithCustomOrganizationSerializer.new(user).to_json
|
|
177
|
-
end
|
|
178
|
-
end
|
data/test/attribute_test.rb
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "helper"
|
|
4
|
-
|
|
5
|
-
class PostSerializer < JsonSerializer
|
|
6
|
-
attribute :id
|
|
7
|
-
attribute :title
|
|
8
|
-
attribute :slug
|
|
9
|
-
|
|
10
|
-
def slug
|
|
11
|
-
"#{ object.id }-#{ object.title }"
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
class ParentSerializer < JsonSerializer
|
|
16
|
-
attribute :parent
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
class ChildSerializer < ParentSerializer
|
|
20
|
-
attribute :child
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
class UserSerializer < JsonSerializer
|
|
24
|
-
attribute :id
|
|
25
|
-
attribute :fullname
|
|
26
|
-
|
|
27
|
-
def fullname
|
|
28
|
-
object.name + " " + object.lastname
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
class AttributeTest < Minitest::Test
|
|
33
|
-
test "converts defined attributes into json" do
|
|
34
|
-
post = Post.new(id: 1, title: "tsunami")
|
|
35
|
-
|
|
36
|
-
result = {
|
|
37
|
-
id: 1,
|
|
38
|
-
title: "tsunami",
|
|
39
|
-
slug: "1-tsunami"
|
|
40
|
-
}.to_json
|
|
41
|
-
|
|
42
|
-
assert_equal result, PostSerializer.new(post).to_json
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
test "serializes array" do
|
|
46
|
-
users = [
|
|
47
|
-
User.new(id: 1, name: "sonny", lastname: "moore"),
|
|
48
|
-
User.new(id: 2, name: "anton", lastname: "zaslavski")
|
|
49
|
-
]
|
|
50
|
-
|
|
51
|
-
result = [
|
|
52
|
-
{ id: 1, fullname: "sonny moore" },
|
|
53
|
-
{ id: 2, fullname: "anton zaslavski" }
|
|
54
|
-
].to_json
|
|
55
|
-
|
|
56
|
-
assert_equal result, UserSerializer.new(users).to_json
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
test "inheritance" do
|
|
60
|
-
parent = ParentSerializer.attributes
|
|
61
|
-
child = ChildSerializer.attributes
|
|
62
|
-
|
|
63
|
-
assert_equal(parent.merge(child: nil), child)
|
|
64
|
-
end
|
|
65
|
-
end
|
data/test/helper.rb
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "bundler/setup"
|
|
4
|
-
require "minitest/autorun"
|
|
5
|
-
require "minitest/pride"
|
|
6
|
-
require "minitest/sugar"
|
|
7
|
-
require "ostruct"
|
|
8
|
-
require_relative "../lib/json_serializer"
|
|
9
|
-
|
|
10
|
-
User = OpenStruct
|
|
11
|
-
Organization = OpenStruct
|
|
12
|
-
Person = OpenStruct
|
|
13
|
-
Post = OpenStruct
|
data/test/root_test.rb
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "helper"
|
|
4
|
-
|
|
5
|
-
class PersonSerializer < JsonSerializer
|
|
6
|
-
attribute :name
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
class RootTest < Minitest::Test
|
|
10
|
-
setup do
|
|
11
|
-
@person = Person.new(name: "sonny")
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
test "serialized object includes root" do
|
|
15
|
-
result = { person: @person.to_h }.to_json
|
|
16
|
-
|
|
17
|
-
assert_equal result, PersonSerializer.new(@person).to_json(root: :person)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
test "serialized array includes root" do
|
|
21
|
-
result = { people: [@person.to_h] }.to_json
|
|
22
|
-
|
|
23
|
-
assert_equal result, PersonSerializer.new([@person]).to_json(root: :people)
|
|
24
|
-
end
|
|
25
|
-
end
|