Class: Zona::OZonesJSON

Inherits:
Object
  • Object
show all
Defined in:
zona/OZonesJSON.rb

Overview

Several class methods regarding the handling of JSON descriptions for the OZones utilities.

Class Method Summary (collapse)

Class Method Details

+ (Hash, Zona::Error) build_json(json_str, root_element)

Build an element description hash.

Parameters:

  • json_str (String)

    JSON description of the element

  • root_element (#to_sym)

    root element of the JSON object

Returns:

  • (Hash, Zona::Error)

    The parsed JSON hash, or Error



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'zona/OZonesJSON.rb', line 30

def self.build_json(json_str, root_element)
    begin
        parser = JSON.parser.new(json_str, {:symbolize_names => true})
        hash = parser.parse

        root_sym = root_element.to_sym

        if hash.has_key?(root_sym)
            return hash[root_sym]
        end

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

    rescue => e
        Error.new(e.message)
    end
end

+ (Object) parse_json(json_str, root_element)

See Also:



49
50
51
# File 'zona/OZonesJSON.rb', line 49

def self.parse_json(json_str, root_element)
    OZonesJSON.build_json(json_str, root_element)
end

+ (String, Zona::Error) to_json(hash_to_convert)

Generates a pretty JSON string from a hash

Parameters:

  • hash_to_convert (Hash)

    a hash to be converted

Returns:

  • (String, Zona::Error)

    JSON string or Error if conversion fails



56
57
58
59
60
61
62
# File 'zona/OZonesJSON.rb', line 56

def self.to_json(hash_to_convert)
    begin
        JSON.pretty_generate(hash_to_convert)
    rescue Exception => e
        Error.new(e.message)
    end
end