firm 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/firm/serializer/json.rb +9 -2
- 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: 2f6498bd122a2588336365f1120c424cfdf65fd5e05d87b4c5b3175c92d72965
|
4
|
+
data.tar.gz: bbf7816402e9f0c1b977dd47cfefc6b600e389c6e3710436a8d62fc9eee42a3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fcc10bf8e5d448f02482fe453fc20433ca26781d08c55a0dba0e0b0947e17aae91d4f97d45c858ae5f73b170c37cb0c646caad3904d573745b8737ac6e61aac
|
7
|
+
data.tar.gz: 7bc179e75ea98e4505156d3ff59d53f7d07931c0d24b40e4adb127ae82be1bcf205a19b0811bebd557f4c519dd507f8802c8e7f25c0ed60ef9d6c75f2fee30ef
|
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
@@ -198,8 +198,15 @@ module FIRM
|
|
198
198
|
else
|
199
199
|
# register anchor object **before** serializing properties to properly handle cycling (bidirectional
|
200
200
|
# references)
|
201
|
-
|
202
|
-
|
201
|
+
if defined? JRUBY_VERSION
|
202
|
+
# JRuby has a problem modifying a hash while iterating it so we create a new copy here which we
|
203
|
+
# merge with the original because that may be updated with an anchor id
|
204
|
+
json_data['data'] = for_serialize(Serializable::Aliasing.register_anchor_object(self, {}))
|
205
|
+
json_data['data'].merge!(json_data['data'].transform_values { |v| v.respond_to?(:as_json) ? v.as_json : v })
|
206
|
+
else
|
207
|
+
json_data['data'] = for_serialize(Serializable::Aliasing.register_anchor_object(self, {}))
|
208
|
+
json_data['data'].transform_values! { |v| v.respond_to?(:as_json) ? v.as_json : v }
|
209
|
+
end
|
203
210
|
end
|
204
211
|
json_data
|
205
212
|
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
|