jsoning 0.4.0 → 0.5.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 +6 -0
- data/lib/jsoning/version.rb +1 -1
- data/lib/jsoning.rb +9 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fd493cccd6b9c1cfb0d8db8dd60cd9a6b61e8b9
|
4
|
+
data.tar.gz: 90afc5cd5c89dd9ae57ff0eb68059454aecfec28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 979a26bc4aadde42006c99ed4b1b137e36c1c24a1efe6f400cca71708f695ac37395630c84ebbb4bb412a28245fc1056f34b377503ad4ea04da44aa3ab4c64db
|
7
|
+
data.tar.gz: 48e6a895798ecd4d255f632c669d366d26dca4d954f3dcfd1906e45da69783de43ca0f7c59e0dd0a61316d94fa253dbfb5f32f3337c45ef2f709d943806dd60d
|
data/README.md
CHANGED
@@ -268,6 +268,12 @@ Calling: `Jsoning.parse(the_json_string, My::User)` will yield a Hash as follow:
|
|
268
268
|
1. When passing a proc as default value, it will be executed to assign default value when value is nil.
|
269
269
|
2. Parsing JSON string as hash by using `Jsoning.parse`
|
270
270
|
|
271
|
+
== Version 0.5.0
|
272
|
+
|
273
|
+
1. Bugfix: when Jsoning to Hash, if encountering data-like datatype, error is
|
274
|
+
raised due to Jsoning does not know how to extract value from them. However, if the object
|
275
|
+
is converted to JSON string, everything is working as expected.
|
276
|
+
|
271
277
|
## License
|
272
278
|
|
273
279
|
The gem is proudly available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/jsoning/version.rb
CHANGED
data/lib/jsoning.rb
CHANGED
@@ -43,20 +43,20 @@ module Jsoning
|
|
43
43
|
|
44
44
|
# generate the json document
|
45
45
|
def generate(object, options = {})
|
46
|
-
initialize_type_extensions
|
47
46
|
protocol = protocol_for!(object.class)
|
48
47
|
protocol.generate(object, options)
|
49
48
|
end
|
50
49
|
|
51
50
|
@@type_extension_initialized = false
|
51
|
+
# define value extractor/interpreter for commonly used ruby datatypes that are not
|
52
|
+
# part of standard types supported by JSON
|
52
53
|
def initialize_type_extensions
|
53
54
|
@@type_extension_initialized = true if !!@@type_extension_initialized
|
54
55
|
return if @@type_extension_initialized
|
55
56
|
|
56
57
|
begin
|
57
|
-
require "time"
|
58
58
|
::Time
|
59
|
-
self.add_type Time, processor: proc { |time| time.
|
59
|
+
self.add_type Time, processor: proc { |time| time.strftime("%FT%T%z") }
|
60
60
|
rescue
|
61
61
|
end
|
62
62
|
|
@@ -64,27 +64,28 @@ module Jsoning
|
|
64
64
|
# try to define value extractor for ActiveSupport::TimeWithZone which is in common use
|
65
65
|
# for AR model
|
66
66
|
::ActiveSupport::TimeWithZone
|
67
|
-
self.add_type ActiveSupport::TimeWithZone, processor: proc { |time| time.
|
67
|
+
self.add_type ActiveSupport::TimeWithZone, processor: proc { |time| time.strftime("%FT%T%z") }
|
68
68
|
rescue
|
69
69
|
# nothing, don't add
|
70
70
|
end
|
71
71
|
|
72
72
|
begin
|
73
73
|
::DateTime
|
74
|
-
self.add_type DateTime, processor: proc { |date| date.
|
74
|
+
self.add_type DateTime, processor: proc { |date| date.strftime("%FT%T%z") }
|
75
75
|
rescue => e
|
76
76
|
# nothing, don't add
|
77
77
|
end
|
78
78
|
|
79
79
|
begin
|
80
80
|
::Date
|
81
|
-
self.add_type Date, processor: proc { |date| date.
|
81
|
+
self.add_type Date, processor: proc { |date| date.strftime("%FT%T%z") }
|
82
82
|
rescue
|
83
83
|
# nothing, don't add
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
87
|
def [](object)
|
88
|
+
Jsoning.initialize_type_extensions
|
88
89
|
protocol = protocol_for!(object.class)
|
89
90
|
protocol.retrieve_values_from(object)
|
90
91
|
end
|
@@ -101,12 +102,14 @@ module Jsoning
|
|
101
102
|
private
|
102
103
|
|
103
104
|
def Jsoning(object, options = {})
|
105
|
+
Jsoning.initialize_type_extensions
|
104
106
|
Jsoning.generate(object, options)
|
105
107
|
end
|
106
108
|
end
|
107
109
|
|
108
110
|
# parse the JSON String to Hash
|
109
111
|
def parse(json_string, klass)
|
112
|
+
Jsoning.initialize_type_extensions
|
110
113
|
protocol = protocol_for!(klass)
|
111
114
|
protocol.construct_hash_from(json_string)
|
112
115
|
end
|