jsonx 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/jsonx.rb +41 -3
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfc86c10385dec7e29e0f761cfb276164c9af3ae
|
4
|
+
data.tar.gz: 5a1dc9a9e813fbe96be1a77ab88f73a648b67d37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81c6bc752a339500b9eeeb0624482d34e1ff6e87c687677fd8cc365375049df789a325598f2f591dedde6ec44ec49c56a7e109739a19919b30fae80c61755694
|
7
|
+
data.tar.gz: 418fb454548b75c4baff1b7e1b79cd2aedeba2293a320ff7c6029da533a312556b61b777992f386db6ab5a6bef6fe5ddf5c33bb368063d7fcd842ff4d163492b
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/jsonx.rb
CHANGED
@@ -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
|
-
|
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
metadata.gz.sig
CHANGED
Binary file
|