Class: MultiJson::Adapters::Nsjsonserialization

Inherits:
OkJson
  • Object
show all
Defined in:
lib/multi_json/adapters/nsjsonserialization.rb

Constant Summary collapse

ParseError =
::MultiJson::OkJson::Error

Class Method Summary collapse

Methods inherited from OkJson

modify_keys, stringify_keys, symbolize_keys

Class Method Details

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


21
22
23
24
25
26
27
28
29
30
# File 'lib/multi_json/adapters/nsjsonserialization.rb', line 21

def self.dump(object, options={})
  pretty = options[:pretty] ? NSJSONWritingPrettyPrinted : 0
  object = object.as_json if object.respond_to?(:as_json)
  if NSJSONSerialization.isValidJSONObject(object)
    data = NSJSONSerialization.dataWithJSONObject(object, options: pretty, error: nil)
    NSMutableString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
  else
    super(object, options)
  end
end

.load(string, options = {}) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/multi_json/adapters/nsjsonserialization.rb', line 9

def self.load(string, options={})
  string = string.read if string.respond_to?(:read)
  data = string.dataUsingEncoding(NSUTF8StringEncoding)
  object = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves, error: nil)
  if object
    object = symbolize_keys(object) if options[:symbolize_keys]
    object
  else
    super(string, options={})
  end
end