Module: BubbleWrap::JSON

Defined in:
motion/core/json.rb

Overview

Handles JSON encoding and decoding in a similar way Ruby 1.9 does.

Defined Under Namespace

Classes: ParserError

Class Method Summary (collapse)

Class Method Details

+ (Object) generate(obj)



30
31
32
# File 'motion/core/json.rb', line 30

def self.generate(obj)
  NSJSONSerialization.dataWithJSONObject(obj, options:0, error:nil).to_str
end

+ (Hash, ...) parse(str_data, &block)

Parses a string or data object and converts it in data structure.

TODO: support options like the C Ruby module does

Raises:

  • (ParserError)

    If the parsing of the passed string/data isn't valid.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'motion/core/json.rb', line 15

def self.parse(str_data, &block)
  return nil unless str_data
  data = str_data.respond_to?(:to_data) ? str_data.to_data : str_data
  opts = NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves | NSJSONReadingAllowFragments
  error = Pointer.new(:id)
  obj = NSJSONSerialization.JSONObjectWithData(data, options:opts, error:error)
  raise ParserError, error[0].description if error[0]
  if block_given?
    yield obj
  else 
    obj
  end
  
end