Class: JSONx

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ JSONx

Returns a new instance of JSONx.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jsonx.rb', line 12

def initialize(h)

  types = {
    Hash:     ->(k, h){
      type = 'json:object'
      [type, '', (k.empty? ? {} : {name: k}), 
        *h.map {|k2,v| types[v.class.to_s.to_sym].call(k2, v)}
      ]
    },
    Fixnum:   ->(k, n){ ['json:number', n, k.empty? ? {} : {name: k}]},
    NilClass: ->(k, n){ ['json:null',  '', k.empty? ? {} : {name: k}]},
    String:   ->(k, s){ ['json:string', s, k.empty? ? {} : {name: k}]},
    Array:    ->(k, a){ 
      ['json:array', '', {name: k}, 
        *a.map{|y| types[y.class.to_s.to_sym].call('', y.to_s)}
      ]
    }
  }

  a =  types[:Hash].call '', h
  a[2] = {'xsi:schemaLocation' => "http://www.datapower.com/schemas/json jsonx.xsd",
  'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
  'xmlns:json' => "http://www.ibm.com/xmlns/prod/2009/jsonx"}
  @to_s = Rexle.new(a).xml pretty: true
end

Instance Attribute Details

#to_sObject (readonly)

Returns the value of attribute to_s.



10
11
12
# File 'lib/jsonx.rb', line 10

def to_s
  @to_s
end