Top Level Namespace

Defined Under Namespace

Modules: Rubyang Classes: Array, Example

Instance Method Summary collapse

Instance Method Details

#json_to_xml(json_str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/rubyang/database/helper.rb', line 7

def json_to_xml( json_str )
  hash = JSON.parse( json_str )
  if hash.keys.size > 1
    raise 'root key size must be 1'
  end
  #doc_xml = REXML::Document.new( "<#{hash.keys.first} />" )
  doc_xml = REXML::Document.new
  json_to_xml_recursive( hash.keys.first, hash.values.first, doc_xml )
  return doc_xml.to_s
end

#json_to_xml_recursive(_key, _value, _doc_xml) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubyang/database/helper.rb', line 18

def json_to_xml_recursive( _key, _value, _doc_xml )
  doc_xml = _doc_xml.add_element( _key )
  case _value
  when Hash
    _value.each{ |key, value|
      case value
      when Hash
        json_to_xml_recursive( key, value, doc_xml )
      when Array
        value.each{ |hash|
          json_to_xml_recursive( key, hash, doc_xml )
        }
      else
        doc_xml.add_element( key ).add_text value
      end
    }
  else
    raise "case value when other than Hash is not implemented"
  end
end

#make_json_schema(schema_tree) ⇒ Object

vim: et ts=2 sw=2



4
5
6
7
8
# File 'lib/rubyang/webui/make_json_schema.rb', line 4

def make_json_schema schema_tree
  json_schema_tree = Array.new
  make_json_schema_recursive json_schema_tree, schema_tree
  return JSON.pretty_generate(json_schema_tree)
end

#make_json_schema_recursive(json_schema_tree, schema_tree) ⇒ Object



10
11
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
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubyang/webui/make_json_schema.rb', line 10

def make_json_schema_recursive json_schema_tree, schema_tree
  case schema_tree.model
  when nil
    json_schema_tree.push Hash.new
    json_schema_tree.last[:root] = Hash.new
    json_schema_tree.last[:root][:name] = "root"
    json_schema_tree.last[:root][:description] = "root"
    json_schema_tree.last[:root][:children] = Array.new
    schema_tree.children.each{ |child_schema_tree|
      make_json_schema_recursive json_schema_tree.last[:root][:children], child_schema_tree
    }
  when Rubyang::Model::Container
    json_schema_tree.push Hash.new
    json_schema_tree.last[:container] = Hash.new
    json_schema_tree.last[:container][:name] = schema_tree.model.arg
    json_schema_tree.last[:container][:description] = schema_tree.model.substmt('description').first.arg
    json_schema_tree.last[:container][:children] = Array.new
    schema_tree.children.each{ |child_schema_tree|
      make_json_schema_recursive json_schema_tree.last[:container][:children], child_schema_tree
    }
  when Rubyang::Model::List
    json_schema_tree.push Hash.new
    json_schema_tree.last[:list] = Hash.new
    json_schema_tree.last[:list][:key] = schema_tree.model.substmt('key')[0].arg
    json_schema_tree.last[:list][:name] = schema_tree.model.arg
    json_schema_tree.last[:list][:description] = schema_tree.model.substmt('description').first.arg
    json_schema_tree.last[:list][:children] = Array.new
    schema_tree.children.each{ |child_schema_tree|
      make_json_schema_recursive json_schema_tree.last[:list][:children], child_schema_tree
    }
  when Rubyang::Model::Leaf
    json_schema_tree.push Hash.new
    json_schema_tree.last[:leaf] = Hash.new
    json_schema_tree.last[:leaf][:name] = schema_tree.model.arg
    json_schema_tree.last[:leaf][:description] = schema_tree.model.substmt('description').first.arg
    json_schema_tree.last[:leaf][:type] = schema_tree.type.arg
  end
end