Module: JSON

Defined in:
lib/yajl/json_gem/parsing.rb,
lib/yajl/json_gem/encoding.rb

Defined Under Namespace

Classes: GeneratorError, JSONError, ParserError

Class Method Summary collapse

Class Method Details

.default_optionsObject



8
9
10
# File 'lib/yajl/json_gem/parsing.rb', line 8

def self.default_options
  @default_options ||= {:symbolize_keys => false}
end

.dump(obj, io = nil, *args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/yajl/json_gem/encoding.rb', line 45

def self.dump(obj, io=nil, *args)
  begin
    Yajl::Encoder.encode(obj, io)
  rescue Yajl::EncodeError => e
    raise JSON::GeneratorError, e.message
  end
end

.generate(obj, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yajl/json_gem/encoding.rb', line 21

def self.generate(obj, opts={})
  begin
    options_map = {}
    if opts.has_key?(:indent)
      options_map[:pretty] = true
      options_map[:indent] = opts[:indent]
    end
    Yajl::Encoder.encode(obj, options_map)
  rescue Yajl::EncodeError => e
    raise JSON::GeneratorError, e.message
  end
end

.load(input, *args) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/yajl/json_gem/parsing.rb', line 20

def self.load(input, *args)
  begin
    Yajl::Parser.parse(input, default_options)
  rescue Yajl::ParseError => e
    raise JSON::ParserError, e.message
  end
end

.parse(str, opts = JSON.default_options) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/yajl/json_gem/parsing.rb', line 12

def self.parse(str, opts=JSON.default_options)
  begin
    Yajl::Parser.parse(str, opts)
  rescue Yajl::ParseError => e
    raise JSON::ParserError, e.message
  end
end

.pretty_generate(obj, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/yajl/json_gem/encoding.rb', line 34

def self.pretty_generate(obj, opts={})
  begin
    options_map = {}
    options_map[:pretty] = true
    options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
    Yajl::Encoder.encode(obj, options_map)
  rescue Yajl::EncodeError => e
    raise JSON::GeneratorError, e.message
  end
end