firm 0.9.1 → 0.9.3
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 +2 -1
- data/lib/firm/serializer/json.rb +13 -11
- data/lib/firm/serializer/xml.rb +14 -4
- data/lib/firm/version.rb +1 -1
- data/tests/serializer_tests.rb +11 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c1c7b999e2db422d608d8aaf8e28a9bace6824640f50a8e5dabb30e2ca89a36
|
4
|
+
data.tar.gz: a16d62247a09cc2e1d8b3c4d2a990ffefb7b48c18f6f1d8f27e12c770ff103eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c45a0bc530a143ad8b37de7e00f6fdd4a2af91ffb52b18ce86f91ffac390ea05825d58b042e29361a96fd762be58d210d141bdb88d9748c3232e7c30232b8cc
|
7
|
+
data.tar.gz: 939fe265f4f55f1255851e5f246b73ca416c6649af2ffcdad6aa8fb63341289ef64c71678bc02a7372c66acd6a91a26111ebc049d2a571b5f3f189f7010d3fb2
|
data/README.md
CHANGED
@@ -8,7 +8,8 @@
|
|
8
8
|
|
9
9
|
## Introduction
|
10
10
|
|
11
|
-
FIRM is a pure Ruby library
|
11
|
+
FIRM is a pure Ruby library that works across different Ruby implementations like MRI Ruby and JRuby providing output
|
12
|
+
format independent object (de-)serialization support.
|
12
13
|
|
13
14
|
FIRM is explicitly **NOT** intended as a non-discriminative marshaling library (dumping any object's attributes)
|
14
15
|
but rather as structured and safe serialization library requiring users to think about what state they want
|
data/lib/firm/serializer/json.rb
CHANGED
@@ -148,20 +148,20 @@ module FIRM
|
|
148
148
|
def json_create(object)
|
149
149
|
data = object['data']
|
150
150
|
# deserializing (anchor) object or alias
|
151
|
-
if
|
152
|
-
if Serializable::Aliasing.restored?(self,
|
151
|
+
if object.has_key?('*id')
|
152
|
+
if Serializable::Aliasing.restored?(self, object['*id'])
|
153
153
|
# resolving an already restored anchor for this alias
|
154
|
-
Serializable::Aliasing.resolve_anchor(self,
|
154
|
+
Serializable::Aliasing.resolve_anchor(self, object['*id'])
|
155
155
|
else
|
156
156
|
# in case of cyclic references JSON will restore aliases before the anchors
|
157
157
|
# so in this case we allocate an instance here and register it as
|
158
158
|
# the anchor; when the anchor is restored it will re-use this instance to initialize & restore
|
159
159
|
# the properties
|
160
|
-
Serializable::Aliasing.restore_anchor(
|
160
|
+
Serializable::Aliasing.restore_anchor(object['*id'], self.allocate)
|
161
161
|
end
|
162
162
|
else
|
163
|
-
instance = if
|
164
|
-
anchor_id =
|
163
|
+
instance = if object.has_key?('&id')
|
164
|
+
anchor_id = object['&id'] # extract anchor id
|
165
165
|
if Serializable::Aliasing.restored?(self, anchor_id)
|
166
166
|
# in case of cyclic references an alias will already have restored the anchor instance
|
167
167
|
# (default constructed); retrieve that instance here for deserialization of properties
|
@@ -192,14 +192,16 @@ module FIRM
|
|
192
192
|
anchor_data = Serializable::Aliasing.get_anchor_data(self)
|
193
193
|
# retroactively insert the anchor in the anchored instance's serialization data
|
194
194
|
anchor_data['&id'] = anchor unless anchor_data.has_key?('&id')
|
195
|
-
json_data[
|
196
|
-
'*id' => anchor
|
197
|
-
}
|
195
|
+
json_data['*id'] = anchor
|
198
196
|
else
|
199
197
|
# register anchor object **before** serializing properties to properly handle cycling (bidirectional
|
200
198
|
# references)
|
201
|
-
|
202
|
-
|
199
|
+
Serializable::Aliasing.register_anchor_object(self, json_data)
|
200
|
+
data = for_serialize({})
|
201
|
+
unless data.empty?
|
202
|
+
json_data['data'] = data
|
203
|
+
json_data['data'].transform_values! { |v| v.respond_to?(:as_json) ? v.as_json : v }
|
204
|
+
end
|
203
205
|
end
|
204
206
|
json_data
|
205
207
|
end
|
data/lib/firm/serializer/xml.rb
CHANGED
@@ -182,7 +182,11 @@ module FIRM
|
|
182
182
|
xml
|
183
183
|
end
|
184
184
|
def from_xml(xml)
|
185
|
-
xml
|
185
|
+
# in case the xml was somehow formatted it may be additional text nodes
|
186
|
+
# get inserted because of added space and/or newlines
|
187
|
+
# (like with JRuby's Nokogiri when outputting pretty formatted XML)
|
188
|
+
# so just in case look up the one CDATA child and only use that one's content
|
189
|
+
xml.children.find { |child| child.cdata? }&.text || ''
|
186
190
|
end
|
187
191
|
end
|
188
192
|
|
@@ -212,8 +216,9 @@ module FIRM
|
|
212
216
|
xml
|
213
217
|
end
|
214
218
|
def from_xml(xml)
|
215
|
-
|
216
|
-
|
219
|
+
s = xml.children.find { |child| child.cdata? }&.text
|
220
|
+
case s
|
221
|
+
when nil, 'NaN' then :Float::NAN
|
217
222
|
when 'Infinity' then ::Float::INFINITY
|
218
223
|
when '-Infinity' then -::Float::INFINITY
|
219
224
|
else
|
@@ -307,7 +312,12 @@ module FIRM
|
|
307
312
|
xml
|
308
313
|
end
|
309
314
|
def from_xml(xml)
|
310
|
-
|
315
|
+
# in case the xml was somehow formatted it may be additional text nodes
|
316
|
+
# get inserted because of added space and/or newlines
|
317
|
+
# (like with JRuby's Nokogiri when outputting pretty formatted XML)
|
318
|
+
# so just in case look up the one CDATA child and only use that one's content
|
319
|
+
data = xml.children.find { |child| child.cdata? }&.text
|
320
|
+
::BigDecimal._load(data || '')
|
311
321
|
end
|
312
322
|
end
|
313
323
|
end
|
data/lib/firm/version.rb
CHANGED
data/tests/serializer_tests.rb
CHANGED
@@ -228,7 +228,7 @@ module SerializerTestMixin
|
|
228
228
|
assert_equal(obj, obj_new)
|
229
229
|
|
230
230
|
Struct.new('MyStruct', :one, :two) unless defined? Struct::MyStruct
|
231
|
-
obj = Struct::MyStruct.new(
|
231
|
+
obj = Struct::MyStruct.new(Point.new(10, 90), Point.new(20, 80))
|
232
232
|
obj_serial = obj.serialize
|
233
233
|
assert_nothing_raised { obj_new = FIRM.deserialize(obj_serial) }
|
234
234
|
assert_equal(obj, obj_new)
|
@@ -705,10 +705,12 @@ module SerializerTestMixin
|
|
705
705
|
end
|
706
706
|
end
|
707
707
|
|
708
|
+
require 'base64'
|
709
|
+
|
708
710
|
class NestedSerializer
|
709
711
|
include FIRM::Serializable
|
710
712
|
|
711
|
-
property
|
713
|
+
property nested: :marshall_nested
|
712
714
|
|
713
715
|
def initialize(serializable=nil)
|
714
716
|
@nested = serializable
|
@@ -718,11 +720,13 @@ module SerializerTestMixin
|
|
718
720
|
|
719
721
|
protected
|
720
722
|
|
721
|
-
def marshall_nested(
|
723
|
+
def marshall_nested(*val)
|
722
724
|
if val.empty?
|
723
|
-
|
725
|
+
# as this results in another XML string to embed it is probably best to encode
|
726
|
+
# it (at least JRuby's Nokogiri version seems to have problems with this without encoding)
|
727
|
+
Base64.urlsafe_encode64(@nested.serialize)
|
724
728
|
else
|
725
|
-
@nested = FIRM.deserialize(val.first)
|
729
|
+
@nested = FIRM.deserialize(Base64.urlsafe_decode64(val.first))
|
726
730
|
nil
|
727
731
|
end
|
728
732
|
end
|
@@ -746,9 +750,8 @@ module SerializerTestMixin
|
|
746
750
|
ref_obj = RefUser.new(container.map[:seven].id, container.map[:eight].id, container.map[:nine].id)
|
747
751
|
container.map[:ten] = ref_obj
|
748
752
|
nest_obj = NestedSerializer.new(container)
|
749
|
-
obj_serial = [nest_obj, container.map[:one], container.map[:two], container.map[:five], [container.map[:three], container.map[:four], container.map[:six]], ref_obj].serialize(
|
750
|
-
obj_new =
|
751
|
-
assert_nothing_raised { obj_new = FIRM.deserialize(obj_serial) }
|
753
|
+
obj_serial = [nest_obj, container.map[:one], container.map[:two], container.map[:five], [container.map[:three], container.map[:four], container.map[:six]], ref_obj].serialize(pretty: true)
|
754
|
+
obj_new = assert_nothing_raised { FIRM.deserialize(obj_serial) }
|
752
755
|
assert_instance_of(::Array, obj_new)
|
753
756
|
assert_instance_of(NestedSerializer, obj_new[0])
|
754
757
|
container = obj_new[0].nested
|