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)
- + (Object) generate(obj)
-
+ (Hash, ...) parse(str_data, &block)
Parses a string or data object and converts it in data structure.
Class Method Details
+ (Object) generate(obj)
29 30 31 32 33 |
# File 'motion/core/json.rb', line 29 def self.generate(obj) # opts = NSJSONWritingPrettyPrinted data = NSJSONSerialization.dataWithJSONObject(obj, options:0, error:nil) data.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
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'motion/core/json.rb', line 15 def self.parse(str_data, &block) 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 |