jsoning 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b62e31437ea7ae41f6aabc42ce7096cba6475eca
4
- data.tar.gz: fdb1858c496487d96805d98a00640af0bb8e97d8
3
+ metadata.gz: 3fd493cccd6b9c1cfb0d8db8dd60cd9a6b61e8b9
4
+ data.tar.gz: 90afc5cd5c89dd9ae57ff0eb68059454aecfec28
5
5
  SHA512:
6
- metadata.gz: 65ebaacd3211f4a650965b474bd90fc8b6a72117080fb30966a5a9015f98ffe1917b63108647e2cc3df26fef554b68b689a93713d462d714460dc48fe787a9e6
7
- data.tar.gz: 7d6c24558e6fc76fbe06b52df2f91288563b83379edc68cae955a45f24f784169a7842b05128a9a8110a1601482f871c3ca7089c3d596fbd9dbdba9496f6672e
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).
@@ -1,3 +1,3 @@
1
1
  module Jsoning
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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.iso8601 }
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.send(:iso8601) }
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.send(:iso8601) }
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.send(:iso8601) }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsoning
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Pahlevi