json-serializer 0.0.9 → 1.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 +4 -4
- data/LICENSE +1 -1
- data/README.md +53 -10
- data/lib/json_serializer.rb +22 -16
- data/test/association_test.rb +178 -0
- data/test/attribute_test.rb +65 -0
- data/test/helper.rb +12 -2
- data/test/root_test.rb +25 -0
- metadata +62 -22
- 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/root.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab535952b239a5ac4719d466197e12030773fddf
|
4
|
+
data.tar.gz: 4ecfc2356129405540b9f22ac43e4da2f8de6358
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fede701fc5c873531b15c0b2501983843032082f9eb3629644612124b8a98937707bea26d38fcaa57def76a7318666aa9ad724e4f134a0e46bbb169b53e3cbcd
|
7
|
+
data.tar.gz: 644198f622a0b6b90353c568ec7436fc5610a52c1ab20b819a6fb23cb38d7fca50a93f2a788f0d90e896f4ac93d48499f053bf4894f9f4947cc37b0e33ef8fdc
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,22 +1,32 @@
|
|
1
|
-
json-serializer
|
1
|
+
json-serializer [](https://travis-ci.org/frodsan/json-serializer)
|
2
2
|
===============
|
3
3
|
|
4
4
|
Customizes JSON output through serializer objects.
|
5
5
|
|
6
|
-
A minimal implementation of [active_model_serializers][active_model_serializers] gem.
|
7
|
-
|
8
6
|
Installation
|
9
7
|
------------
|
10
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
|
+
|
11
23
|
```
|
12
|
-
gem install json-serializer
|
24
|
+
$ gem install json-serializer
|
13
25
|
```
|
14
26
|
|
15
27
|
Usage
|
16
28
|
-----
|
17
29
|
|
18
|
-
### Basics
|
19
|
-
|
20
30
|
Here's a simple example:
|
21
31
|
|
22
32
|
```ruby
|
@@ -48,7 +58,8 @@ UserSerializer.new(user).to_json(root: :user)
|
|
48
58
|
# => "{\"user\":{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}}"
|
49
59
|
```
|
50
60
|
|
51
|
-
|
61
|
+
Arrays
|
62
|
+
------
|
52
63
|
|
53
64
|
A serializer can be used for objects contained in an array:
|
54
65
|
|
@@ -77,7 +88,8 @@ Given the example above, it will return a json output like:
|
|
77
88
|
]
|
78
89
|
```
|
79
90
|
|
80
|
-
|
91
|
+
Attributes
|
92
|
+
----------
|
81
93
|
|
82
94
|
By default, before looking up the attribute on the object, it checks the presence
|
83
95
|
of a method with the name of the attribute. This allow serializes to include
|
@@ -116,7 +128,7 @@ class UserSerializer < JsonSerializer
|
|
116
128
|
attribute :first_name
|
117
129
|
attribute :last_name
|
118
130
|
|
119
|
-
|
131
|
+
attr_reader :current_user
|
120
132
|
|
121
133
|
def initialize(object, current_user)
|
122
134
|
super(object)
|
@@ -131,7 +143,8 @@ class UserSerializer < JsonSerializer
|
|
131
143
|
end
|
132
144
|
```
|
133
145
|
|
134
|
-
|
146
|
+
Attributes with Custom Serializer
|
147
|
+
---------------------------------
|
135
148
|
|
136
149
|
You can specify a serializer class for a defined attribute. This is very useful
|
137
150
|
for serializing each element of an association.
|
@@ -192,4 +205,34 @@ The example above returns the following json output:
|
|
192
205
|
}
|
193
206
|
```
|
194
207
|
|
208
|
+
Contributing
|
209
|
+
------------
|
210
|
+
|
211
|
+
Fork the project with:
|
212
|
+
|
213
|
+
```
|
214
|
+
$ git clone git@github.com:frodsan/json-serializer.git
|
215
|
+
```
|
216
|
+
|
217
|
+
To install dependencies, use:
|
218
|
+
|
219
|
+
```
|
220
|
+
$ bundle install
|
221
|
+
```
|
222
|
+
|
223
|
+
To run the test suite, do:
|
224
|
+
|
225
|
+
```
|
226
|
+
$ rake test
|
227
|
+
```
|
228
|
+
|
229
|
+
For bug reports and pull requests use [GitHub][issues].
|
230
|
+
|
231
|
+
License
|
232
|
+
-------
|
233
|
+
|
234
|
+
This gem is released under the [MIT License][mit].
|
235
|
+
|
195
236
|
[active_model_serializers]: https://github.com/rails-api/active_model_serializers
|
237
|
+
[mit]: http://www.opensource.org/licenses/MIT
|
238
|
+
[issues]: https://github.com/frodsan/json-serializer/issues
|
data/lib/json_serializer.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "json"
|
2
4
|
|
3
5
|
class JsonSerializer
|
@@ -25,37 +27,41 @@ class JsonSerializer
|
|
25
27
|
@attributes ||= {}
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
attr_reader :object
|
29
31
|
|
30
32
|
def initialize(object)
|
31
33
|
@object = object
|
32
34
|
end
|
33
35
|
|
34
|
-
def to_json(
|
35
|
-
|
36
|
-
|
37
|
-
else
|
38
|
-
serializable_object.to_json
|
39
|
-
end
|
40
|
-
end
|
36
|
+
def to_json(root: nil)
|
37
|
+
result = decorate
|
38
|
+
result = { root => result } if root
|
41
39
|
|
42
|
-
|
40
|
+
result.to_json
|
41
|
+
end
|
43
42
|
|
44
|
-
def
|
45
|
-
return nil unless
|
43
|
+
def decorate
|
44
|
+
return nil unless object
|
46
45
|
|
47
|
-
if
|
48
|
-
|
46
|
+
if object.respond_to?(:to_a)
|
47
|
+
to_arry
|
49
48
|
else
|
50
49
|
to_hash
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
53
|
+
protected
|
54
|
+
|
55
|
+
def to_arry
|
56
|
+
object.to_a.map { |o| self.class.new(o).to_hash }
|
57
|
+
end
|
58
|
+
|
54
59
|
def to_hash
|
55
60
|
self.class.attributes.each_with_object({}) do |(name, serializer), hash|
|
56
|
-
|
57
|
-
|
58
|
-
|
61
|
+
res = self.class.method_defined?(name) ? send(name) : object.send(name)
|
62
|
+
res = Utils.const(self.class, serializer).new(res).decorate if serializer
|
63
|
+
|
64
|
+
hash[name] = res
|
59
65
|
end
|
60
66
|
end
|
61
67
|
end
|
@@ -0,0 +1,178 @@
|
|
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
|
@@ -0,0 +1,65 @@
|
|
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
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "minitest/autorun"
|
5
|
+
require "minitest/pride"
|
6
|
+
require "minitest/sugar"
|
3
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
ADDED
@@ -0,0 +1,25 @@
|
|
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
|
metadata
CHANGED
@@ -1,49 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francesco Rodríguez
|
8
|
-
- Mayn Kjær
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2016-04-12 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
14
|
+
name: minitest
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
16
|
requirements:
|
18
|
-
- - "
|
17
|
+
- - "~>"
|
19
18
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
19
|
+
version: '5.8'
|
21
20
|
type: :development
|
22
21
|
prerelease: false
|
23
22
|
version_requirements: !ruby/object:Gem::Requirement
|
24
23
|
requirements:
|
25
|
-
- - "
|
24
|
+
- - "~>"
|
26
25
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
version: '5.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest-sugar
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '11.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '11.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
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
|
32
71
|
executables: []
|
33
72
|
extensions: []
|
34
73
|
extra_rdoc_files: []
|
35
74
|
files:
|
36
|
-
- ".gems"
|
37
75
|
- LICENSE
|
38
76
|
- README.md
|
39
|
-
- json-serializer.gemspec
|
40
77
|
- lib/json_serializer.rb
|
41
|
-
-
|
42
|
-
- test/
|
43
|
-
- test/attribute.rb
|
78
|
+
- test/association_test.rb
|
79
|
+
- test/attribute_test.rb
|
44
80
|
- test/helper.rb
|
45
|
-
- test/
|
46
|
-
homepage: https://github.com/
|
81
|
+
- test/root_test.rb
|
82
|
+
homepage: https://github.com/frodsan/json-serializer
|
47
83
|
licenses:
|
48
84
|
- MIT
|
49
85
|
metadata: {}
|
@@ -63,8 +99,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
99
|
version: '0'
|
64
100
|
requirements: []
|
65
101
|
rubyforge_project:
|
66
|
-
rubygems_version: 2.
|
102
|
+
rubygems_version: 2.5.1
|
67
103
|
signing_key:
|
68
104
|
specification_version: 4
|
69
|
-
summary: Customize JSON ouput through serializer objects
|
70
|
-
test_files:
|
105
|
+
summary: Customize JSON ouput through serializer objects
|
106
|
+
test_files:
|
107
|
+
- test/association_test.rb
|
108
|
+
- test/attribute_test.rb
|
109
|
+
- test/helper.rb
|
110
|
+
- test/root_test.rb
|
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/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
|