Module: Sensu::JSON

Defined in:
lib/sensu/json.rb,
lib/sensu/json/oj.rb,
lib/sensu/json/jrjackson.rb,
lib/sensu/json/parse_error.rb

Overview

The Sensu JSON parser abstraction library.

Defined Under Namespace

Classes: JrJackson, Oj, ParseError

Class Method Summary collapse

Class Method Details

.dump(object, options = {}) ⇒ Object

Dump (generate) a JSON string from a Ruby object.

Parameters:

  • object (Object)
  • options (Hash) (defaults to: {})


42
43
44
# File 'lib/sensu/json.rb', line 42

def dump(object, options={})
  @@parser.dump(object, options)
end

.load(string) ⇒ Object

Load (and parse) a JSON string.

Parameters:

  • string (String)

Returns:

  • (Object)


30
31
32
33
34
35
36
# File 'lib/sensu/json.rb', line 30

def load(string)
  begin
    @@parser.load(string)
  rescue => error
    raise Sensu::JSON::ParseError.build(error, string)
  end
end

.setup!Object

Set up the JSON parser. This method must be called before any attempt to use the parser. This method is currently called at the bottom of this file. The appropriate JSON parser will be loaded for the current platform.

Returns:

  • (Object)

    parser.



15
16
17
18
19
20
21
22
23
24
# File 'lib/sensu/json.rb', line 15

def setup!
  @@parser = case
  when RUBY_PLATFORM =~ /java/
    require "sensu/json/jrjackson"
    Sensu::JSON::JrJackson.new
  else
    require "sensu/json/oj"
    Sensu::JSON::Oj.new
  end
end