Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / OpenNebulaJSON / JSONUtils.rb @ aaccdf92

History | View | Annotate | Download (5.13 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
module OpenNebulaJSON
18

    
19
    require 'json'
20

    
21
    module JSONUtils
22
        def to_json
23
            begin
24
                JSON.pretty_generate self.to_hash
25
            rescue Exception => e
26
                OpenNebula::Error.new(e.message)
27
            end
28
        end
29

    
30
        def self.parse_json(json_str, root_element)
31
            begin
32
                hash = JSON.parse(json_str)
33
            rescue Exception => e
34
                return OpenNebula::Error.new(e.message)
35
            end
36

    
37
            if hash.has_key?(root_element)
38
                return hash[root_element]
39
            else
40
                return OpenNebula::Error.new("Error parsing JSON: Wrong resource type")
41
            end
42
        end
43

    
44
        def parse_json(json_str, root_element)
45
            JSONUtils.parse_json(json_str, root_element)
46
        end
47

    
48
        def parse_json_sym(json_str, root_element)
49
            begin
50
                parser = JSON.parser.new(json_str, {:symbolize_names => true})
51
                hash = parser.parse
52

    
53
                if hash.has_key?(root_element)
54
                    return hash[root_element]
55
                end
56

    
57
                Error.new("Error parsing JSON:\ root element not present")
58

    
59
            rescue => e
60
                Error.new(e.message)
61
            end
62
        end
63

    
64
        def template_to_str(attributes, indent=true)
65
             if indent
66
                 ind_enter="\n"
67
                 ind_tab='  '
68
             else
69
                 ind_enter=''
70
                 ind_tab=' '
71
             end
72

    
73
             str = attributes.collect do |key, value|
74
                 next if value.nil? || value.empty?
75
                 
76
                 str_line = ""
77

    
78
                 case value
79
                 when Array
80
                     value.each do |i|
81
                        next if i.nil? || i.empty?
82

    
83
                        case i
84
                        when Hash
85
                            str_line << key.to_s.upcase << "=[" << ind_enter
86
                            str_line << i.collect do |k, j|
87
                                str = ind_tab + k.to_s.upcase + "="
88
                                str += "\"#{j.to_s}\"" if j
89
                                str
90
                            end.compact.join(",\n")
91
                            str_line << "\n]\n"
92
                        else
93
                            str_line << key.to_s.upcase << "=\"#{i.to_s}\"\n"
94
                        end
95
                     end
96
                when Hash
97
                    str_line << key.to_s.upcase << "=[" << ind_enter
98

    
99
                    str_line << value.collect do |key3, value3|
100
                        str = ind_tab + key3.to_s.upcase + "="
101
                        str += "\"#{value3.to_s}\"" if value3
102
                        str
103
                    end.compact.join(",\n")
104

    
105
                    str_line << "\n]\n"
106

    
107
                else
108
                    str_line<<key.to_s.upcase << "=" << "\"#{value.to_s}\""
109
                end
110

    
111
                str_line
112
             end.compact.join("\n")
113

    
114
             str
115
         end
116

    
117
         def hash_to_str(template_hash, delete_values)
118
            for del in delete_values
119
                template_hash.delete(del)
120
            end
121

    
122
            if !template_hash.empty?
123
                template_str = ""
124
                template_hash.collect do |key,value|
125
                    if value.kind_of?(Array)
126
                        template_str << key.to_s.upcase << " = \["
127
                        for obj in value
128
                            if obj.kind_of?(Hash)
129
                                obj.collect do |key,value|
130
                                    template_str << key.to_s.upcase << " = \""<< value.to_s << "\"\n"
131
                                end
132
                            end
133
                        end
134
                        template_str << "\]\n"
135
                    else
136
                        template_str << key.to_s.upcase << " = \""<< value.to_s << "\"\n"
137
                    end
138
                end
139
            end
140
            return template_str
141
        end
142
     end
143
end