json_array_serializer 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -13
- data/lib/json_array_serializer/version.rb +1 -1
- data/lib/json_array_serializer.rb +43 -21
- data/{ext → spec/ext}/hash.rb +9 -8
- data/{ext → spec/ext}/open_struct.rb +0 -0
- data/spec/json_array_serializer_spec.rb +40 -0
- data/spec/spec_helper.rb +2 -2
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e7a4e281f8c0cd7f9375760778ee6f8e6caaf76
|
4
|
+
data.tar.gz: 7db5e8490e317f3a57c95c8b787aa6d31aa07cdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa1c7b226056e7812621d8becbab7dc302117f054cef04ed77ce3cce04b2264b3a011169c135cfb963d6cb112241313925b9b5a5d8406a003c34504b82cc15f4
|
7
|
+
data.tar.gz: 10c856a3be1bc47e44d4359cb25584f6037643b94395b578887fb72143419e531bb124d9c645036f68b47789e65e02e3a61602856abcf49da08133483ab5ef32
|
data/README.md
CHANGED
@@ -1,24 +1,18 @@
|
|
1
1
|
# JSONArraySerializer
|
2
2
|
|
3
|
-
A class to serialize and deserialize arrays of JSON strings. This is useful when doing things like saving arrays of objects to a database or file, or sending them over the wire.
|
3
|
+
A class to serialize and deserialize arrays of JSON strings. This is useful when doing things like saving arrays of objects to a database or file, or sending them over the wire. The main focus of this gem is aimed at Rails's `ActiveRecord::Base#serialize`.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install json_array_serializer
|
9
|
+
```ruby
|
10
|
+
gem 'json_array_serializer'
|
11
|
+
```
|
18
12
|
|
19
13
|
## Usage
|
20
14
|
|
21
|
-
You can use the `JSONArraySerizalizer` directly on a attribute of a Rails model as follows.
|
15
|
+
You can use the `JSONArraySerizalizer` directly on a attribute of a Rails model as follows. This will save the `bar` attribute as a string of JSON in the database, where `bar` is an array of hashes.
|
22
16
|
|
23
17
|
```ruby
|
24
18
|
class Foo < ActiveRecord::Base
|
@@ -71,7 +65,7 @@ The `array_class` you pass to `JSONArraySerializer.new` __must__ have the follow
|
|
71
65
|
|
72
66
|
```
|
73
67
|
# A.new : Array -> A
|
74
|
-
# a.to_a : -> Array (where a is an instance of A)
|
68
|
+
# a.to_a : -> Array (where a is an instance of A)
|
75
69
|
```
|
76
70
|
|
77
71
|
### Specifying the Database Type
|
@@ -102,4 +96,4 @@ end
|
|
102
96
|
class Company < ActiveRecord::Base
|
103
97
|
serialize :developers, JSONArraySerializer.new(array_class: DeveloperArray, element_class: Developer)
|
104
98
|
end
|
105
|
-
```
|
99
|
+
```
|
@@ -9,45 +9,67 @@ class JSONArraySerializer
|
|
9
9
|
# class. The element_class is what will be used to represent
|
10
10
|
# each element of the stored JSON.
|
11
11
|
#
|
12
|
-
# The
|
12
|
+
# The option :array_class MUST implement two methods:
|
13
|
+
#
|
14
|
+
# A.new : Array -> A
|
15
|
+
# a.to_a : -> Array (where a is an instance of A)
|
16
|
+
#
|
17
|
+
# The option :element_class MUST implement two methods:
|
13
18
|
#
|
14
19
|
# A.new : Hash -> A
|
15
20
|
# a.to_h : -> Hash (where a is an instance of A)
|
16
21
|
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
# The option :column_type MUST be one of :string | :text | :array.
|
23
|
+
#
|
24
|
+
# The option :allow_nil is a boolean which determines whether or not
|
25
|
+
# to load/dump nil values. If set to true (the default) it will load/dump
|
26
|
+
# nil when given nil. If set to false it will treat nil as an empty array.
|
27
|
+
#
|
28
|
+
def initialize(options = {})
|
29
|
+
options = {
|
30
|
+
array_class: Array,
|
31
|
+
element_class: Hash,
|
32
|
+
column_type: :text,
|
33
|
+
allow_nil: true
|
34
|
+
}.merge(options)
|
35
|
+
|
36
|
+
@array_class = options[:array_class]
|
37
|
+
@element_class = options[:element_class]
|
38
|
+
@column_type = options[:column_type]
|
39
|
+
@allow_nil = options[:allow_nil]
|
21
40
|
end
|
22
41
|
|
23
42
|
# [JSON String] || JSON String -> array_class<element_class>
|
24
|
-
# Takes
|
25
|
-
#
|
43
|
+
# Takes the data from the database, and loads them into an
|
44
|
+
# instance of `array_class` with elements of `element_class`.
|
26
45
|
#
|
27
46
|
def load(data)
|
28
|
-
return
|
47
|
+
return (@allow_nil ? nil : []) if data.nil?
|
29
48
|
|
30
|
-
array =
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
49
|
+
array =
|
50
|
+
case @column_type
|
51
|
+
when :array
|
52
|
+
data.map do |json|
|
53
|
+
hash = JSON.load(json)
|
54
|
+
(element_class == Hash) ? hash : element_class.new(hash)
|
55
|
+
end
|
56
|
+
when :string, :text
|
57
|
+
if element_class == Hash
|
58
|
+
JSON.load(data)
|
59
|
+
else
|
60
|
+
JSON.load(data).map { |hash| element_class.new(hash) }
|
61
|
+
end
|
39
62
|
end
|
40
|
-
end
|
41
63
|
|
42
64
|
(array_class == Array) ? array : array_class.new(array)
|
43
65
|
end
|
44
66
|
|
45
67
|
# array_class<element_class> -> [JSON String] || JSON String
|
46
|
-
# Takes an
|
47
|
-
#
|
68
|
+
# Takes an instance of `array_class` with `element_class` elements
|
69
|
+
# and dumps them either a array of JSON or JSON itself for the database.
|
48
70
|
#
|
49
71
|
def dump(data)
|
50
|
-
return
|
72
|
+
return (@allow_nil ? nil : []) if data.nil?
|
51
73
|
|
52
74
|
case @column_type
|
53
75
|
when :array
|
data/{ext → spec/ext}/hash.rb
RENAMED
@@ -7,14 +7,15 @@ class Hash
|
|
7
7
|
#
|
8
8
|
def stringify
|
9
9
|
inject({}) do |acc, (key, value)|
|
10
|
-
acc[key.to_s] =
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
acc[key.to_s] =
|
11
|
+
case value
|
12
|
+
when Symbol
|
13
|
+
value.to_s
|
14
|
+
when Hash
|
15
|
+
value.stringify
|
16
|
+
else
|
17
|
+
value
|
18
|
+
end
|
18
19
|
acc
|
19
20
|
end
|
20
21
|
end
|
File without changes
|
@@ -125,6 +125,10 @@ describe JSONArraySerializer do
|
|
125
125
|
expect(serializer.dump(array)).to be_a(String)
|
126
126
|
end
|
127
127
|
|
128
|
+
it 'returns a string given an array of another class' do
|
129
|
+
expect(serializer.dump([OpenStruct.new(foo: 'bar')])).to be_a(String)
|
130
|
+
end
|
131
|
+
|
128
132
|
it 'JSON can load it' do
|
129
133
|
expect { JSON.load(serializer.dump(array)) }.not_to raise_error
|
130
134
|
end
|
@@ -160,6 +164,10 @@ describe JSONArraySerializer do
|
|
160
164
|
expect(serializer.dump(array)).to be_a(String)
|
161
165
|
end
|
162
166
|
|
167
|
+
it 'returns a string given an array of hashes' do
|
168
|
+
expect(serializer.dump([{foo: 'bar'}])).to be_a(String)
|
169
|
+
end
|
170
|
+
|
163
171
|
it 'JSON can load it' do
|
164
172
|
expect { JSON.load(serializer.dump(array)) }.not_to raise_error
|
165
173
|
end
|
@@ -215,4 +223,36 @@ describe JSONArraySerializer do
|
|
215
223
|
end
|
216
224
|
end
|
217
225
|
end
|
226
|
+
|
227
|
+
context 'with allow_nil set to false' do
|
228
|
+
let(:serializer) { JSONArraySerializer.new(allow_nil: false) }
|
229
|
+
|
230
|
+
describe '#load' do
|
231
|
+
it 'loads nil as []' do
|
232
|
+
expect(serializer.load(nil)).to eq([])
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe '#dump' do
|
237
|
+
it 'dumps nil as []' do
|
238
|
+
expect(['[]', []]).to include(serializer.dump(nil))
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context 'with allow_nil set to true' do
|
244
|
+
let(:serializer) { JSONArraySerializer.new(allow_nil: true) }
|
245
|
+
|
246
|
+
describe '#load' do
|
247
|
+
it 'loads nil as nil' do
|
248
|
+
expect(serializer.load(nil)).to eq(nil)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
describe '#dump' do
|
253
|
+
it 'dumps nil as nil' do
|
254
|
+
expect(serializer.dump(nil)).to eq(nil)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
218
258
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,8 +3,8 @@ require 'ostruct'
|
|
3
3
|
require 'json_array_serializer'
|
4
4
|
|
5
5
|
# Connivance extensions.
|
6
|
-
require_relative '
|
7
|
-
require_relative '
|
6
|
+
require_relative 'ext/hash'
|
7
|
+
require_relative 'ext/open_struct'
|
8
8
|
|
9
9
|
RSpec.configure do |config|
|
10
10
|
config.color_enabled = true
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_array_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Lilienthal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -65,11 +65,11 @@ files:
|
|
65
65
|
- LICENSE.txt
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
|
-
- ext/hash.rb
|
69
|
-
- ext/open_struct.rb
|
70
68
|
- json_array_serializer.gemspec
|
71
69
|
- lib/json_array_serializer.rb
|
72
70
|
- lib/json_array_serializer/version.rb
|
71
|
+
- spec/ext/hash.rb
|
72
|
+
- spec/ext/open_struct.rb
|
73
73
|
- spec/json_array_serializer_spec.rb
|
74
74
|
- spec/spec_helper.rb
|
75
75
|
homepage: https://github.com/Americastestkitchen/json_array_serializer
|
@@ -97,5 +97,7 @@ signing_key:
|
|
97
97
|
specification_version: 4
|
98
98
|
summary: Simple serialization for arrays of things to arrays of JSON.
|
99
99
|
test_files:
|
100
|
+
- spec/ext/hash.rb
|
101
|
+
- spec/ext/open_struct.rb
|
100
102
|
- spec/json_array_serializer_spec.rb
|
101
103
|
- spec/spec_helper.rb
|