jsonx 0.1.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8898d96ee7232278f7c977ec5046f8f88218dd2d
4
- data.tar.gz: 8639012c53055125b16c40d1aae08e57ad79fdbd
3
+ metadata.gz: cfc86c10385dec7e29e0f761cfb276164c9af3ae
4
+ data.tar.gz: 5a1dc9a9e813fbe96be1a77ab88f73a648b67d37
5
5
  SHA512:
6
- metadata.gz: 823b0a230687fcc09017cebc580fd809127bdc2f2f9385a74791256bde5da9651cb36371e1b9b6210683835bc4e687ceeac2bbda36ca8e5a03c10e854daece13
7
- data.tar.gz: 359eddda7fc202c5b55da90c37e4b0edf0cc609bee59a3384efe8629cd937a86c45b1ebae757abbf7c3297f2c2b4d55b9a708b0fb0403a9fcb756e835634ca85
6
+ metadata.gz: 81c6bc752a339500b9eeeb0624482d34e1ff6e87c687677fd8cc365375049df789a325598f2f591dedde6ec44ec49c56a7e109739a19919b30fae80c61755694
7
+ data.tar.gz: 418fb454548b75c4baff1b7e1b79cd2aedeba2293a320ff7c6029da533a312556b61b777992f386db6ab5a6bef6fe5ddf5c33bb368063d7fcd842ff4d163492b
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -8,12 +8,26 @@ require 'rexle'
8
8
 
9
9
  class JSONx
10
10
 
11
- attr_reader :to_s
11
+ attr_reader :to_s, :to_json
12
12
 
13
13
  def initialize(obj)
14
14
 
15
- h = obj.is_a?(String) ? JSON.parse(obj) : obj
15
+ if obj.is_a?(String) then
16
+
17
+ if obj.lstrip[0] == '<' then
18
+ jsonx_to_json obj
19
+ else
20
+ json_to_jsonx JSON.parse(obj)
21
+ end
22
+
23
+ else
24
+ json_to_jsonx obj # transform the hash
25
+ end
16
26
 
27
+
28
+ end
29
+
30
+ def json_to_jsonx(h)
17
31
  types = {
18
32
  Hash: ->(k, h){
19
33
  type = 'json:object'
@@ -39,8 +53,32 @@ class JSONx
39
53
  a[2] = {'xsi:schemaLocation' => "http://www.datapower.com/schemas/json jsonx.xsd",
40
54
  'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
41
55
  'xmlns:json' => "http://www.ibm.com/xmlns/prod/2009/jsonx"}
42
- @to_s = Rexle.new(a).xml pretty: true
56
+ @to_s = Rexle.new(a).xml pretty: true
57
+ end
58
+
59
+ def jsonx_to_json(xml)
60
+
61
+ doc = Rexle.new xml
62
+
63
+ types = {
64
+ boolean: ->(e){ e.text.downcase == 'true' },
65
+ string: ->(e){e.text},
66
+ null: ->(e){nil},
67
+ number: ->(e){ s = e.text; s.to_i == s.to_f ? s.to_i : s.to_f},
68
+ array: ->(e){
69
+ e.elements.map {|x| types[x.name[/\w+$/].to_sym].call x }
70
+ },
71
+ object: ->(e){
72
+ e.elements.inject({}) do |r, x|
73
+ name = x.name[/\w+$/]
74
+ r.merge(x.attributes[:name] => types[name.to_sym].call(x))
75
+ end
76
+ }
77
+ }
78
+ @to_json = types[:object].call doc.root
79
+
43
80
  end
81
+
44
82
 
45
83
  end
46
84
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file