json-serializer 0.0.9 → 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 +22 -170
- data/lib/json_serializer.rb +32 -32
- metadata +43 -24
- data/.gems +0 -1
- data/json-serializer.gemspec +0 -14
- data/makefile +0 -8
- data/test/association.rb +0 -168
- data/test/attribute.rb +0 -64
- data/test/helper.rb +0 -3
- data/test/root.rb +0 -23
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,195 +1,47 @@
|
|
|
1
|
-
json-serializer
|
|
2
|
-
===============
|
|
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
|
-
Installation
|
|
9
|
-
------------
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
gem install json-serializer
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
Usage
|
|
16
|
-
-----
|
|
17
|
-
|
|
18
|
-
### Basics
|
|
19
|
-
|
|
20
|
-
Here's a simple example:
|
|
5
|
+
## Usage
|
|
21
6
|
|
|
22
7
|
```ruby
|
|
23
8
|
require "json_serializer"
|
|
24
9
|
|
|
25
10
|
class UserSerializer < JsonSerializer
|
|
26
11
|
attribute :id
|
|
27
|
-
attribute :
|
|
28
|
-
attribute :last_name
|
|
12
|
+
attribute :name
|
|
29
13
|
end
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
In this case, we defined a new serializer class and specified the attributes
|
|
33
|
-
we would like to include in the serialized form.
|
|
34
|
-
|
|
35
|
-
```ruby
|
|
36
|
-
user = User.create(first_name: "Sonny", last_name: "Moore", admin: true)
|
|
37
|
-
|
|
38
|
-
UserSerializer.new(user).to_json
|
|
39
|
-
# => "{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}"
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
You can add a root to the outputted json through the `:root` option:
|
|
43
|
-
|
|
44
|
-
```ruby
|
|
45
|
-
user = User.create(first_name: "Sonny", last_name: "Moore", admin: true)
|
|
46
|
-
|
|
47
|
-
UserSerializer.new(user).to_json(root: :user)
|
|
48
|
-
# => "{\"user\":{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}}"
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Arrays
|
|
52
|
-
|
|
53
|
-
A serializer can be used for objects contained in an array:
|
|
54
|
-
|
|
55
|
-
```ruby
|
|
56
|
-
require "json_serializer"
|
|
57
14
|
|
|
58
15
|
class PostSerializer < JsonSerializer
|
|
59
16
|
attribute :id
|
|
60
17
|
attribute :title
|
|
61
|
-
attribute :
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
posts = []
|
|
65
|
-
posts << Post.create(title: "Post 1", body: "Hello!")
|
|
66
|
-
posts << Post.create(title: "Post 2", body: "Goodbye!")
|
|
67
|
-
|
|
68
|
-
PostSerializer.new(posts).to_json
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Given the example above, it will return a json output like:
|
|
72
|
-
|
|
73
|
-
```json
|
|
74
|
-
[
|
|
75
|
-
{ "id": 1, "title": "Post 1", "body": "Hello!" },
|
|
76
|
-
{ "id": 2, "title": "Post 2", "body": "Goodbye!" }
|
|
77
|
-
]
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### Attributes
|
|
81
|
-
|
|
82
|
-
By default, before looking up the attribute on the object, it checks the presence
|
|
83
|
-
of a method with the name of the attribute. This allow serializes to include
|
|
84
|
-
properties in addition to the object attributes or customize the result of a
|
|
85
|
-
specified attribute. You can access the object being serialized with the +object+
|
|
86
|
-
method.
|
|
87
|
-
|
|
88
|
-
```ruby
|
|
89
|
-
require "json_serializer"
|
|
90
|
-
|
|
91
|
-
class UserSerializer < JsonSerializer
|
|
92
|
-
attribute :id
|
|
93
|
-
attribute :first_name
|
|
94
|
-
attribute :last_name
|
|
95
|
-
attribute :full_name
|
|
96
|
-
|
|
97
|
-
def full_name
|
|
98
|
-
object.first_name + " " + object.last_name
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
user = User.create(first_name: "Sonny", last_name: "Moore")
|
|
103
|
-
|
|
104
|
-
UserSerializer.new(user).to_json
|
|
105
|
-
# => "{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\",\"full_name\":\"Sonny Moore\"}"
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
If you would like direct, low-level control of attribute serialization, you can
|
|
109
|
-
completely override `to_hash` method to return the hash you need:
|
|
110
|
-
|
|
111
|
-
```ruby
|
|
112
|
-
require "json_serializer"
|
|
113
|
-
|
|
114
|
-
class UserSerializer < JsonSerializer
|
|
115
|
-
attribute :id
|
|
116
|
-
attribute :first_name
|
|
117
|
-
attribute :last_name
|
|
118
|
-
|
|
119
|
-
attr :current_user
|
|
120
|
-
|
|
121
|
-
def initialize(object, current_user)
|
|
122
|
-
super(object)
|
|
123
|
-
@current_user = current_user
|
|
124
|
-
end
|
|
18
|
+
attribute :author, :UserSerializer
|
|
19
|
+
attribute :comments, :CommentSerializer
|
|
125
20
|
|
|
126
|
-
def
|
|
127
|
-
|
|
128
|
-
hash.merge!(admin: object.admin) if current_user.admin?
|
|
129
|
-
hash
|
|
21
|
+
def title
|
|
22
|
+
object.title.upcase
|
|
130
23
|
end
|
|
131
24
|
end
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Attributes with Custom Serializer
|
|
135
|
-
|
|
136
|
-
You can specify a serializer class for a defined attribute. This is very useful
|
|
137
|
-
for serializing each element of an association.
|
|
138
|
-
|
|
139
|
-
```ruby
|
|
140
|
-
require "json_serializer"
|
|
141
|
-
|
|
142
|
-
class UserSerializer < JsonSerializer
|
|
143
|
-
attribute :id
|
|
144
|
-
attribute :username
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
class PostSerializer < JsonSerializer
|
|
148
|
-
attribute :id
|
|
149
|
-
attribute :title
|
|
150
|
-
attribute :user, :UserSerializer
|
|
151
|
-
attribute :comments, :CommentSerializer
|
|
152
|
-
end
|
|
153
25
|
|
|
154
26
|
class CommentSerializer < JsonSerializer
|
|
155
|
-
attribute :
|
|
156
|
-
attribute :content
|
|
157
|
-
attribute :user, :UserSerializer
|
|
27
|
+
attribute :body
|
|
158
28
|
end
|
|
159
29
|
|
|
160
|
-
admin = User.create(username: "admin", admin: true)
|
|
161
|
-
user = User.create(username: "user")
|
|
162
|
-
|
|
163
|
-
post = Post.create(title: "Hello!", user: admin)
|
|
164
|
-
post.comments << Comment.create(content: "First comment", user: user)
|
|
165
|
-
|
|
166
30
|
PostSerializer.new(post).to_json
|
|
31
|
+
# => {"id":1,"title":"HELLO","author":{"id":1,"name":"Jane"},"comments":[{"body":"Nice post"}]}
|
|
32
|
+
|
|
33
|
+
PostSerializer.new([post]).to_json(root: :posts)
|
|
34
|
+
# => {"posts":[{"id":1,"title":"HELLO","author":{"id":1,"name":"Jane"},"comments":[{"body":"Nice post"}]}]}
|
|
167
35
|
```
|
|
168
36
|
|
|
169
|
-
|
|
37
|
+
## What it does
|
|
170
38
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
"username": "admin"
|
|
179
|
-
},
|
|
180
|
-
"comments":
|
|
181
|
-
[
|
|
182
|
-
{
|
|
183
|
-
"id": 1,
|
|
184
|
-
"content": "First comment",
|
|
185
|
-
"user":
|
|
186
|
-
{
|
|
187
|
-
"id": 2,
|
|
188
|
-
"username": "user"
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
]
|
|
192
|
-
}
|
|
193
|
-
```
|
|
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.
|
|
44
|
+
|
|
45
|
+
## Why
|
|
194
46
|
|
|
195
|
-
|
|
47
|
+
I wrote json-serializer to see how little Ruby code it takes to write a JSON serializer.
|
data/lib/json_serializer.rb
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require "json"
|
|
2
4
|
|
|
3
5
|
class JsonSerializer
|
|
4
|
-
module Utils
|
|
5
|
-
def self.const(context, name)
|
|
6
|
-
case name
|
|
7
|
-
when Symbol, String
|
|
8
|
-
context.const_get(name)
|
|
9
|
-
else name
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
6
|
def self.inherited(subclass)
|
|
15
|
-
attributes.
|
|
16
|
-
subclass.attribute(name, serializer)
|
|
17
|
-
end
|
|
7
|
+
subclass.attributes.merge!(attributes)
|
|
18
8
|
end
|
|
19
9
|
|
|
20
10
|
def self.attribute(name, serializer = nil)
|
|
21
|
-
attributes[name]
|
|
11
|
+
attributes[name] = serializer
|
|
22
12
|
end
|
|
23
13
|
|
|
24
14
|
def self.attributes
|
|
25
15
|
@attributes ||= {}
|
|
26
16
|
end
|
|
27
17
|
|
|
28
|
-
|
|
18
|
+
attr_reader :object
|
|
29
19
|
|
|
30
20
|
def initialize(object)
|
|
31
21
|
@object = object
|
|
32
22
|
end
|
|
33
23
|
|
|
34
|
-
def to_json(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
24
|
+
def to_json(*args, root: nil)
|
|
25
|
+
result = decorate
|
|
26
|
+
result = { root => result } if root
|
|
27
|
+
|
|
28
|
+
result.to_json(*args)
|
|
40
29
|
end
|
|
41
30
|
|
|
42
|
-
|
|
31
|
+
def decorate
|
|
32
|
+
return nil unless object
|
|
33
|
+
|
|
34
|
+
object.respond_to?(:to_ary) ? to_array : to_hash
|
|
35
|
+
end
|
|
43
36
|
|
|
44
|
-
|
|
45
|
-
return nil unless @object
|
|
37
|
+
protected
|
|
46
38
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
else
|
|
50
|
-
to_hash
|
|
51
|
-
end
|
|
39
|
+
def to_array
|
|
40
|
+
object.to_ary.map { |o| self.class.new(o).decorate }
|
|
52
41
|
end
|
|
53
42
|
|
|
54
43
|
def to_hash
|
|
55
44
|
self.class.attributes.each_with_object({}) do |(name, serializer), hash|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
45
|
+
res = serializer_method?(name) ? send(name) : object.public_send(name)
|
|
46
|
+
res = serializer_class(serializer).new(res).decorate if serializer
|
|
47
|
+
|
|
48
|
+
hash[name] = res
|
|
59
49
|
end
|
|
60
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
|
|
61
61
|
end
|
metadata
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json-serializer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Francesco Rodríguez
|
|
8
|
-
- Mayn Kjær
|
|
9
|
-
autorequire:
|
|
10
8
|
bindir: bin
|
|
11
9
|
cert_chain: []
|
|
12
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
11
|
dependencies:
|
|
14
12
|
- !ruby/object:Gem::Dependency
|
|
15
|
-
name:
|
|
13
|
+
name: minitest
|
|
16
14
|
requirement: !ruby/object:Gem::Requirement
|
|
17
15
|
requirements:
|
|
18
16
|
- - ">="
|
|
@@ -25,29 +23,52 @@ dependencies:
|
|
|
25
23
|
- - ">="
|
|
26
24
|
- !ruby/object:Gem::Version
|
|
27
25
|
version: '0'
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: ostruct
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rake
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
description: A Ruby JSON serializer in under 50 lines of code
|
|
55
|
+
email: frodsan@me.com
|
|
32
56
|
executables: []
|
|
33
57
|
extensions: []
|
|
34
58
|
extra_rdoc_files: []
|
|
35
59
|
files:
|
|
36
|
-
-
|
|
60
|
+
- CHANGELOG.md
|
|
37
61
|
- LICENSE
|
|
38
62
|
- README.md
|
|
39
|
-
- json-serializer.gemspec
|
|
40
63
|
- lib/json_serializer.rb
|
|
41
|
-
-
|
|
42
|
-
- test/association.rb
|
|
43
|
-
- test/attribute.rb
|
|
44
|
-
- test/helper.rb
|
|
45
|
-
- test/root.rb
|
|
46
|
-
homepage: https://github.com/harmoni/json-serializer
|
|
64
|
+
homepage: https://github.com/frodsan/json-serializer
|
|
47
65
|
licenses:
|
|
48
66
|
- MIT
|
|
49
|
-
metadata:
|
|
50
|
-
|
|
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'
|
|
51
72
|
rdoc_options: []
|
|
52
73
|
require_paths:
|
|
53
74
|
- lib
|
|
@@ -55,16 +76,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
55
76
|
requirements:
|
|
56
77
|
- - ">="
|
|
57
78
|
- !ruby/object:Gem::Version
|
|
58
|
-
version: '
|
|
79
|
+
version: '3.3'
|
|
59
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
81
|
requirements:
|
|
61
82
|
- - ">="
|
|
62
83
|
- !ruby/object:Gem::Version
|
|
63
84
|
version: '0'
|
|
64
85
|
requirements: []
|
|
65
|
-
|
|
66
|
-
rubygems_version: 2.4.5
|
|
67
|
-
signing_key:
|
|
86
|
+
rubygems_version: 4.0.3
|
|
68
87
|
specification_version: 4
|
|
69
|
-
summary:
|
|
88
|
+
summary: A Ruby JSON serializer in under 50 lines of code
|
|
70
89
|
test_files: []
|
data/.gems
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
cutest -v 1.2.2
|
data/json-serializer.gemspec
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
Gem::Specification.new do |s|
|
|
2
|
-
s.name = "json-serializer"
|
|
3
|
-
s.version = "0.0.9"
|
|
4
|
-
s.summary = "Customize JSON ouput through serializer objects."
|
|
5
|
-
s.description = s.summary
|
|
6
|
-
s.authors = ["Francesco Rodríguez", "Mayn Kjær"]
|
|
7
|
-
s.email = ["frodsan@me.com", "mayn.kjaer@gmail.com"]
|
|
8
|
-
s.homepage = "https://github.com/harmoni/json-serializer"
|
|
9
|
-
s.license = "MIT"
|
|
10
|
-
|
|
11
|
-
s.files = `git ls-files`.split("\n")
|
|
12
|
-
|
|
13
|
-
s.add_development_dependency "cutest"
|
|
14
|
-
end
|
data/makefile
DELETED
data/test/association.rb
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
require_relative "helper"
|
|
2
|
-
|
|
3
|
-
User = OpenStruct
|
|
4
|
-
Organization = OpenStruct
|
|
5
|
-
|
|
6
|
-
class OrganizationSerializer < JsonSerializer
|
|
7
|
-
attribute :id
|
|
8
|
-
attribute :name
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
class UserWithOrganizationSerializer < JsonSerializer
|
|
12
|
-
attribute :id
|
|
13
|
-
attribute :name
|
|
14
|
-
attribute :organization, :OrganizationSerializer
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
test "serializes object with association" do
|
|
18
|
-
user = User.new(id: 1, name: "sonny")
|
|
19
|
-
user.organization = Organization.new(id: 1, name: "enterprise")
|
|
20
|
-
|
|
21
|
-
result = {
|
|
22
|
-
id: 1,
|
|
23
|
-
name: "sonny",
|
|
24
|
-
organization: {
|
|
25
|
-
id: 1,
|
|
26
|
-
name: "enterprise"
|
|
27
|
-
}
|
|
28
|
-
}.to_json
|
|
29
|
-
|
|
30
|
-
assert_equal result, UserWithOrganizationSerializer.new(user).to_json
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
test "serializes object with a nil association" do
|
|
34
|
-
user = User.new(id: 1, name: "sonny")
|
|
35
|
-
user.organization = nil
|
|
36
|
-
|
|
37
|
-
result = {
|
|
38
|
-
id: 1,
|
|
39
|
-
name: "sonny",
|
|
40
|
-
organization: nil
|
|
41
|
-
}.to_json
|
|
42
|
-
|
|
43
|
-
assert_equal result, UserWithOrganizationSerializer.new(user).to_json
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
test "serializes array with association" do
|
|
47
|
-
users = [
|
|
48
|
-
User.new(id: 1, name: "sonny", organization: Organization.new(id: 1, name: "enterprise")),
|
|
49
|
-
User.new(id: 2, name: "anton", organization: Organization.new(id: 2, name: "evil"))
|
|
50
|
-
]
|
|
51
|
-
|
|
52
|
-
result = [
|
|
53
|
-
{
|
|
54
|
-
id: 1,
|
|
55
|
-
name: "sonny",
|
|
56
|
-
organization: {
|
|
57
|
-
id: 1,
|
|
58
|
-
name: "enterprise"
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
id: 2,
|
|
63
|
-
name: "anton",
|
|
64
|
-
organization: {
|
|
65
|
-
id: 2,
|
|
66
|
-
name: "evil"
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
].to_json
|
|
70
|
-
|
|
71
|
-
assert_equal result, UserWithOrganizationSerializer.new(users).to_json
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
class UserWithOrganizationsSerializer < JsonSerializer
|
|
75
|
-
attribute :id
|
|
76
|
-
attribute :name
|
|
77
|
-
attribute :organizations, :OrganizationSerializer
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
test "serializes object with collection" do
|
|
81
|
-
user = User.new(id: 1, name: "sonny")
|
|
82
|
-
user.organizations = [
|
|
83
|
-
Organization.new(id: 1, name: "enterprise"),
|
|
84
|
-
Organization.new(id: 2, name: "evil")
|
|
85
|
-
]
|
|
86
|
-
|
|
87
|
-
result = {
|
|
88
|
-
id: 1,
|
|
89
|
-
name: "sonny",
|
|
90
|
-
organizations: [
|
|
91
|
-
{
|
|
92
|
-
id: 1,
|
|
93
|
-
name: "enterprise"
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
id: 2,
|
|
97
|
-
name: "evil"
|
|
98
|
-
}
|
|
99
|
-
]
|
|
100
|
-
}.to_json
|
|
101
|
-
|
|
102
|
-
assert_equal result, UserWithOrganizationsSerializer.new(user).to_json
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
test "serializes array with nested collections" do
|
|
106
|
-
users = [
|
|
107
|
-
User.new(
|
|
108
|
-
id: 1,
|
|
109
|
-
name: "sonny",
|
|
110
|
-
organizations: [
|
|
111
|
-
Organization.new(id: 1, name: "enterprise"),
|
|
112
|
-
Organization.new(id: 2, name: "evil"),
|
|
113
|
-
]
|
|
114
|
-
),
|
|
115
|
-
User.new(
|
|
116
|
-
id: 2,
|
|
117
|
-
name: "anton",
|
|
118
|
-
organizations: [
|
|
119
|
-
Organization.new(id: 3, name: "showtek")
|
|
120
|
-
]
|
|
121
|
-
)
|
|
122
|
-
]
|
|
123
|
-
|
|
124
|
-
result = [
|
|
125
|
-
{
|
|
126
|
-
id: 1,
|
|
127
|
-
name: "sonny",
|
|
128
|
-
organizations: [
|
|
129
|
-
{
|
|
130
|
-
id: 1,
|
|
131
|
-
name: "enterprise"
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
id: 2,
|
|
135
|
-
name: "evil"
|
|
136
|
-
}
|
|
137
|
-
]
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
id: 2,
|
|
141
|
-
name: "anton",
|
|
142
|
-
organizations: [
|
|
143
|
-
{
|
|
144
|
-
id: 3,
|
|
145
|
-
name: "showtek"
|
|
146
|
-
}
|
|
147
|
-
]
|
|
148
|
-
}
|
|
149
|
-
].to_json
|
|
150
|
-
|
|
151
|
-
assert_equal result, UserWithOrganizationsSerializer.new(users).to_json
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
class UserWithCustomOrganizationSerializer < JsonSerializer
|
|
155
|
-
attribute :organizations, :OrganizationSerializer
|
|
156
|
-
|
|
157
|
-
def organizations
|
|
158
|
-
[Organization.new(id: 1, name: "enterprise")]
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
test "implements association method and returns different result" do
|
|
163
|
-
user = User.new
|
|
164
|
-
|
|
165
|
-
result = { organizations: [ { id: 1, name: "enterprise" } ] }.to_json
|
|
166
|
-
|
|
167
|
-
assert_equal result, UserWithCustomOrganizationSerializer.new(user).to_json
|
|
168
|
-
end
|
data/test/attribute.rb
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
require_relative "helper"
|
|
2
|
-
|
|
3
|
-
Post = OpenStruct
|
|
4
|
-
User = OpenStruct
|
|
5
|
-
|
|
6
|
-
class PostSerializer < JsonSerializer
|
|
7
|
-
attribute :id
|
|
8
|
-
attribute :title
|
|
9
|
-
attribute :slug
|
|
10
|
-
|
|
11
|
-
def slug
|
|
12
|
-
"#{ object.id }-#{ object.title }"
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
test "converts defined attributes into json" do
|
|
17
|
-
post = Post.new(id: 1, title: "tsunami")
|
|
18
|
-
|
|
19
|
-
result = {
|
|
20
|
-
id: 1,
|
|
21
|
-
title: "tsunami",
|
|
22
|
-
slug: "1-tsunami"
|
|
23
|
-
}.to_json
|
|
24
|
-
|
|
25
|
-
assert_equal result, PostSerializer.new(post).to_json
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
class UserSerializer < JsonSerializer
|
|
29
|
-
attribute :id
|
|
30
|
-
attribute :fullname
|
|
31
|
-
|
|
32
|
-
def fullname
|
|
33
|
-
object.name + " " + object.lastname
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
test "serializes array" do
|
|
38
|
-
users = [
|
|
39
|
-
User.new(id: 1, name: "sonny", lastname: "moore"),
|
|
40
|
-
User.new(id: 2, name: "anton", lastname: "zaslavski")
|
|
41
|
-
]
|
|
42
|
-
|
|
43
|
-
result = [
|
|
44
|
-
{ id: 1, fullname: "sonny moore" },
|
|
45
|
-
{ id: 2, fullname: "anton zaslavski" }
|
|
46
|
-
].to_json
|
|
47
|
-
|
|
48
|
-
assert_equal result, UserSerializer.new(users).to_json
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
class ParentSerializer < JsonSerializer
|
|
52
|
-
attribute :parent
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
class ChildSerializer < ParentSerializer
|
|
56
|
-
attribute :child
|
|
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
|
data/test/helper.rb
DELETED
data/test/root.rb
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
require_relative "helper"
|
|
2
|
-
|
|
3
|
-
Person = OpenStruct
|
|
4
|
-
|
|
5
|
-
class PersonSerializer < JsonSerializer
|
|
6
|
-
attribute :name
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
setup do
|
|
10
|
-
Person.new(name: "sonny")
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
test "serialized object includes root" do |person|
|
|
14
|
-
result = { person: person.to_h }.to_json
|
|
15
|
-
|
|
16
|
-
assert_equal result, PersonSerializer.new(person).to_json(root: :person)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
test "serialized array includes root" do |person|
|
|
20
|
-
result = { people: [person.to_h] }.to_json
|
|
21
|
-
|
|
22
|
-
assert_equal result, PersonSerializer.new([person]).to_json(root: :people)
|
|
23
|
-
end
|