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
- .generate(obj) ⇒ Object
-
.parse(str_data, &block) ⇒ Hash, ...
Parses a string or data object and converts it in data structure.
Class Method Details
.generate(obj) ⇒ Object
30 31 32 33 34 |
# File 'motion/core/json.rb', line 30 def self.generate(obj) # opts = NSJSONWritingPrettyPrinted data = NSJSONSerialization.dataWithJSONObject(obj, options:0, error:nil) data.to_str end |
.parse(str_data, &block) ⇒ Hash, ...
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 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 |