jsoning 0.2.0 → 0.3.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 +19 -2
- data/lib/jsoning/foundations/mapper.rb +21 -15
- data/lib/jsoning/version.rb +1 -1
- data/lib/jsoning.rb +49 -0
- 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: 2edd1def968d6f13f1ba2cc36243718163cd7dcc
|
4
|
+
data.tar.gz: 2e18b982c6b0269727b827d782ea72695f904d7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2799fd887a3db49f97de68228f5fc4a5634f7b91466a0f119b56b7c3565e10bbb4856d5896c0422bddc5767fcd32284031873ef7dae1d97c5d68faef45b57ab
|
7
|
+
data.tar.gz: 5622840a2c4ddf1efa7141fa17c01793aff7bde1468113e407d676a099e08616a14a17b8f0b1f5721aaf60d219e7a283de7716582da50e374d74c6cf0f1d69e3
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ any Ruby object there is. Kiss good bye to complexity
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
14
14
|
```ruby
|
15
|
-
gem '
|
15
|
+
gem 'jsoning'
|
16
16
|
```
|
17
17
|
|
18
18
|
And then execute:
|
@@ -187,6 +187,18 @@ The syntax above will return ruby hash object:
|
|
187
187
|
"degree_detail"=>{"faculty"=>"School of IT", "degree"=>"B.Sc. (Hons) Computer Science"}}
|
188
188
|
```
|
189
189
|
|
190
|
+
## Supporting custom data type
|
191
|
+
|
192
|
+
JSON, by default support data type such as boolean, nil, string, and number. If you have class like
|
193
|
+
`MyFancyString` and would tell Jsoning how to interpret and extract value from them, use this syntax:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
Jsoning.add_type MyFancyString, processor: { |fancy_string| fancy_string.get_string }
|
197
|
+
```
|
198
|
+
|
199
|
+
Internally, it is how Jsoning convert date-like data type (`Date`, `DateTime`, `Time`, `ActiveSupport::TimeWithZone`) to
|
200
|
+
ISO8601 which can be parsed by compliant JavaScript interpreter in the browser (or somewhere else).
|
201
|
+
|
190
202
|
## Changelog
|
191
203
|
|
192
204
|
== Version 0.1.0
|
@@ -195,7 +207,12 @@ The syntax above will return ruby hash object:
|
|
195
207
|
|
196
208
|
== Version 0.2.0
|
197
209
|
|
198
|
-
|
210
|
+
1. Ability to turn object into a hash
|
211
|
+
|
212
|
+
== Version 0.3.0
|
213
|
+
|
214
|
+
1. Allow user to specify how Jsoning would extract value from a custom data type
|
215
|
+
2. Date, DateTime, Time, ActiveSupport::TimeWithZone now is by default parsed to ISO8601 format.
|
199
216
|
|
200
217
|
## License
|
201
218
|
|
@@ -50,24 +50,30 @@ class Jsoning::Mapper
|
|
50
50
|
def deep_parse(object)
|
51
51
|
parsed_data = nil
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
parsed_data << deep_parse(each_obj)
|
57
|
-
end
|
58
|
-
elsif object.is_a?(Hash)
|
59
|
-
parsed_data = {}
|
60
|
-
object.each do |obj_key_name, obj_val|
|
61
|
-
parsed_data[obj_key_name] = deep_parse(obj_val)
|
62
|
-
end
|
63
|
-
elsif object.is_a?(Integer) || object.is_a?(Float) || object.is_a?(String) ||
|
64
|
-
object.is_a?(TrueClass) || object.is_a?(FalseClass) || object.is_a?(NilClass)
|
65
|
-
parsed_data = object
|
53
|
+
value_extractor = Jsoning::TYPE_EXTENSIONS[object.class.to_s]
|
54
|
+
if value_extractor # is defined
|
55
|
+
parsed_data = value_extractor.(object)
|
66
56
|
else
|
67
|
-
|
68
|
-
|
57
|
+
if object.is_a?(Array)
|
58
|
+
parsed_data = []
|
59
|
+
object.each do |each_obj|
|
60
|
+
parsed_data << deep_parse(each_obj)
|
61
|
+
end
|
62
|
+
elsif object.is_a?(Hash)
|
63
|
+
parsed_data = {}
|
64
|
+
object.each do |obj_key_name, obj_val|
|
65
|
+
parsed_data[obj_key_name] = deep_parse(obj_val)
|
66
|
+
end
|
67
|
+
elsif object.is_a?(Integer) || object.is_a?(Float) || object.is_a?(String) ||
|
68
|
+
object.is_a?(TrueClass) || object.is_a?(FalseClass) || object.is_a?(NilClass)
|
69
|
+
parsed_data = object
|
70
|
+
else
|
71
|
+
protocol = Jsoning.protocol_for!(object.class)
|
72
|
+
parsed_data = protocol.parse(object)
|
73
|
+
end
|
69
74
|
end
|
70
75
|
|
76
|
+
|
71
77
|
parsed_data
|
72
78
|
end
|
73
79
|
end
|
data/lib/jsoning/version.rb
CHANGED
data/lib/jsoning.rb
CHANGED
@@ -9,6 +9,8 @@ require "json"
|
|
9
9
|
|
10
10
|
module Jsoning
|
11
11
|
PROTOCOLS = {}
|
12
|
+
# if type is defined here, we will use it to extract its value for the key
|
13
|
+
TYPE_EXTENSIONS = {}
|
12
14
|
|
13
15
|
module_function
|
14
16
|
|
@@ -37,10 +39,57 @@ module Jsoning
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def generate(object, options = {})
|
42
|
+
initialize_type_extensions
|
40
43
|
protocol = protocol_for!(object.class)
|
41
44
|
protocol.generate(object, options)
|
42
45
|
end
|
43
46
|
|
47
|
+
@@type_extension_initialized = false
|
48
|
+
def initialize_type_extensions
|
49
|
+
@@type_extension_initialized = true if !!@@type_extension_initialized
|
50
|
+
return if @@type_extension_initialized
|
51
|
+
|
52
|
+
begin
|
53
|
+
require "time"
|
54
|
+
::Time
|
55
|
+
self.add_type Time, processor: proc { |time| time.iso8601 }
|
56
|
+
rescue
|
57
|
+
end
|
58
|
+
|
59
|
+
begin
|
60
|
+
# try to define value extractor for ActiveSupport::TimeWithZone which is in common use
|
61
|
+
# for AR model
|
62
|
+
::ActiveSupport::TimeWithZone
|
63
|
+
self.add_type ActiveSupport::TimeWithZone, processor: proc { |time| time.send(:iso8601) }
|
64
|
+
rescue
|
65
|
+
# nothing, don't add
|
66
|
+
end
|
67
|
+
|
68
|
+
begin
|
69
|
+
::DateTime
|
70
|
+
self.add_type DateTime, processor: proc { |date| date.send(:iso8601) }
|
71
|
+
rescue => e
|
72
|
+
# nothing, don't add
|
73
|
+
end
|
74
|
+
|
75
|
+
begin
|
76
|
+
::Date
|
77
|
+
self.add_type Date, processor: proc { |date| date.send(:iso8601) }
|
78
|
+
rescue
|
79
|
+
# nothing, don't add
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class << self
|
84
|
+
def add_type(klass, options = {})
|
85
|
+
processor = options[:processor]
|
86
|
+
raise Jsoning::Error, "Pass in processor that is a proc explaining how to extract the value" unless processor.is_a?(Proc)
|
87
|
+
|
88
|
+
TYPE_EXTENSIONS[klass.to_s] = processor
|
89
|
+
nil
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
44
93
|
def self.[](object)
|
45
94
|
protocol = protocol_for!(object.class)
|
46
95
|
protocol.parse(object)
|
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.3.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-10-
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|