jsoning 0.6.0 → 0.7.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/README.md +15 -3
- data/lib/jsoning/dsl/for_dsl.rb +10 -1
- data/lib/jsoning/foundations/mapper.rb +10 -2
- data/lib/jsoning/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b677bf0eef2f3f5ec7b07e6132e038ab33e4111
|
4
|
+
data.tar.gz: b7db25000fd4b3473d178b7cbaf488a7412d52e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ebff10b0fa858c2c787d1f5c14f658fd272e4e99fd7d74880772e23da97a6001e4780a98418a23ce101f329a94780fc71c21f12b2bf5769f8134a4fe1073417
|
7
|
+
data.tar.gz: ca07af734f5508bb36218ae69c601ee636e8f8d379b9e08666ffacb154439ec530cda86c84a795d8c8db8426549eb52aa585e61ca53009cbd2cc2b5dd860afba
|
data/README.md
CHANGED
@@ -29,6 +29,7 @@ Or install it yourself as:
|
|
29
29
|
|
30
30
|
1. Generating JSON from your object
|
31
31
|
2. Generating Hash from your object
|
32
|
+
3. Versioning the JSON/Hash result
|
32
33
|
|
33
34
|
## Assumptions
|
34
35
|
|
@@ -68,6 +69,8 @@ Say, we want to serialize User. First, we have to define the serializer for `My:
|
|
68
69
|
```ruby
|
69
70
|
Jsoning.for(My::User) do
|
70
71
|
key :name
|
72
|
+
# demonstrating value post-processor
|
73
|
+
key :upcase_name, from: :name, value: { |name| name.upcase }
|
71
74
|
key :years_old, from: :age
|
72
75
|
key :gender, default: "male"
|
73
76
|
key :books
|
@@ -112,7 +115,7 @@ Jsoning(user)
|
|
112
115
|
Which will return:
|
113
116
|
|
114
117
|
```json
|
115
|
-
{"name":"Adam Baihaqi","years_old":21,"gender":"male","books":[{"name":"Quiet: The Power of Introvert"},{"name":"Harry Potter and the Half-Blood Prince"}],"degree_detail":null}
|
118
|
+
{"name":"Adam Baihaqi","upcase_name":"ADAM BAIHAQI","years_old":21,"gender":"male","books":[{"name":"Quiet: The Power of Introvert"},{"name":"Harry Potter and the Half-Blood Prince"}],"degree_detail":null}
|
116
119
|
```
|
117
120
|
|
118
121
|
We can also pretty-print the value, which usually is bit slower though, by calling:
|
@@ -126,6 +129,7 @@ Which will return:
|
|
126
129
|
```json
|
127
130
|
{
|
128
131
|
"name": "Adam Baihaqi",
|
132
|
+
"upcase_name": "ADAM BAIHAQI",
|
129
133
|
"years_old": 21,
|
130
134
|
"gender": "male",
|
131
135
|
"books": [
|
@@ -154,6 +158,7 @@ Jsoning the user with pretty set to true, will return:
|
|
154
158
|
```json
|
155
159
|
{
|
156
160
|
"name": "Adam Baihaqi",
|
161
|
+
"upcase_name": "ADAM BAIHAQI",
|
157
162
|
"years_old": 21,
|
158
163
|
"gender": "male",
|
159
164
|
"books": [
|
@@ -181,6 +186,7 @@ The syntax above will return ruby hash object:
|
|
181
186
|
|
182
187
|
```ruby
|
183
188
|
{"name"=>"Adam Baihaqi",
|
189
|
+
"upcase_name"=>"ADAM BAIHAQI",
|
184
190
|
"years_old"=>21,
|
185
191
|
"gender"=>"male",
|
186
192
|
"books"=>[{"name"=>"Quiet: The Power of Introvert"}, {"name"=>"Harry Potter and the Half-Blood Prince"}],
|
@@ -231,6 +237,7 @@ end
|
|
231
237
|
|
232
238
|
the_json_string = %Q{
|
233
239
|
{"name":"Adam Baihaqi",
|
240
|
+
"upcase_name"=>"ADAM BAIHAQI",
|
234
241
|
"years_old":21,
|
235
242
|
"gender":"male",
|
236
243
|
"books":[{"name":"Mathematics 6A"},{"name":"Physics A2"}],
|
@@ -243,6 +250,7 @@ Calling: `Jsoning.parse(the_json_string, My::User)` will yield a Hash as follow:
|
|
243
250
|
|
244
251
|
```ruby
|
245
252
|
{"name"=>"Adam Baihaqi",
|
253
|
+
"upcase_name"=>"ADAM BAIHAQI",
|
246
254
|
"years_old"=>21,
|
247
255
|
"gender"=>"male",
|
248
256
|
"books"=>[{"name"=>"Mathematics 6A"}, {"name"=>"Physics A2"}],
|
@@ -288,9 +296,9 @@ We can also generate the whole user json/hash, too:
|
|
288
296
|
|
289
297
|
```ruby
|
290
298
|
json = Jsoning.generate(user, version: :v1)
|
291
|
-
expect(JSON.parse(json)).to eq({"name"=>"Adam Baihaqi", "years_old"=>21, "gender"=>"male", "books"=>[{"name"=>"Quiet: The Power of Introvert"}, {"name"=>"Harry Potter and the Half-Blood Prince"}], "degree_detail"=>nil, "registered_at"=>"2015-11-01T14:41:09+0000"})
|
299
|
+
expect(JSON.parse(json)).to eq({"name"=>"Adam Baihaqi", "upcase_name"=>"ADAM BAIHAQI", "years_old"=>21, "gender"=>"male", "books"=>[{"name"=>"Quiet: The Power of Introvert"}, {"name"=>"Harry Potter and the Half-Blood Prince"}], "degree_detail"=>nil, "registered_at"=>"2015-11-01T14:41:09+0000"})
|
292
300
|
json = Jsoning.generate(user, version: :v2)
|
293
|
-
expect(JSON.parse(json)).to eq({"name"=>"Adam Baihaqi", "years_old"=>21, "gender"=>"male", "books"=>[{"book_name"=>"Quiet: The Power of Introvert"}, {"book_name"=>"Harry Potter and the Half-Blood Prince"}], "degree_detail"=>nil, "registered_at"=>"2015-11-01T14:41:09+0000"})
|
301
|
+
expect(JSON.parse(json)).to eq({"name"=>"Adam Baihaqi", "upcase_name"=>"ADAM BAIHAQI", "years_old"=>21, "gender"=>"male", "books"=>[{"book_name"=>"Quiet: The Power of Introvert"}, {"book_name"=>"Harry Potter and the Half-Blood Prince"}], "degree_detail"=>nil, "registered_at"=>"2015-11-01T14:41:09+0000"})
|
294
302
|
```
|
295
303
|
|
296
304
|
If for when generating object, the requested versioning is undefined, the default version will be used.
|
@@ -325,6 +333,10 @@ If for when generating object, the requested versioning is undefined, the defaul
|
|
325
333
|
|
326
334
|
1. Versioning the way JSON/Hash is de-serialized/serialized
|
327
335
|
|
336
|
+
== Version 0.7.0
|
337
|
+
|
338
|
+
1. Add value post-processor
|
339
|
+
|
328
340
|
## License
|
329
341
|
|
330
342
|
The gem is proudly available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/jsoning/dsl/for_dsl.rb
CHANGED
@@ -33,10 +33,13 @@ class Jsoning::ForDsl
|
|
33
33
|
mapped_from = nil
|
34
34
|
options = {}
|
35
35
|
|
36
|
+
# iterate the args given, it contains a string/symbol indicating the key name
|
37
|
+
# and options, that specify further about the behaviour/mechanism of the key
|
36
38
|
args.each do |arg|
|
37
39
|
if arg.is_a?(String) || arg.is_a?(Symbol)
|
38
40
|
mapped_to = arg
|
39
41
|
elsif arg.is_a?(Hash)
|
42
|
+
# extract the options if given
|
40
43
|
options = arg
|
41
44
|
end
|
42
45
|
end
|
@@ -61,7 +64,13 @@ class Jsoning::ForDsl
|
|
61
64
|
mapper.nullable = options.delete(:null)
|
62
65
|
mapper.nullable = options.delete("null") if mapper.nullable?.nil?
|
63
66
|
|
64
|
-
|
67
|
+
# if value is given, it has a value processor to be executed
|
68
|
+
# after value is determined
|
69
|
+
if options[:value] || options['value']
|
70
|
+
mapper.value_processor = options.delete(:value) || options.delete('value')
|
71
|
+
end
|
72
|
+
|
73
|
+
options.keys.each { |key| raise Jsoning::Error, "Undefined option: #{key}" }
|
65
74
|
|
66
75
|
# add mapper to the version
|
67
76
|
version_instance.add_mapper(mapper)
|
@@ -10,7 +10,7 @@ class Jsoning::Mapper
|
|
10
10
|
attr_writer :nullable
|
11
11
|
# what variable in the object will be used to obtain the value
|
12
12
|
attr_accessor :parallel_variable
|
13
|
-
attr_accessor :
|
13
|
+
attr_accessor :value_processor
|
14
14
|
|
15
15
|
def initialize(version_instance)
|
16
16
|
self.parallel_variable = nil
|
@@ -48,7 +48,15 @@ class Jsoning::Mapper
|
|
48
48
|
raise Jsoning::Error, "Null value given for '#{name}' when serializing #{object}"
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
|
+
extracted_value = deep_parse(target_value, requested_version_name)
|
53
|
+
|
54
|
+
# apply extractor to extracted value, if processor is defined
|
55
|
+
if value_processor
|
56
|
+
extracted_value = value_processor.(extracted_value)
|
57
|
+
end
|
58
|
+
|
59
|
+
target_hash[name] = extracted_value
|
52
60
|
end
|
53
61
|
|
54
62
|
def default_value(version_name = self.version.version_name)
|
data/lib/jsoning/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsoning
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Pahlevi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|