Class: MultiJson::Adapters::Nsjsonserialization

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

Constant Summary collapse

ParseError =
::MultiJson::OkJson::Error

Instance Method Summary collapse

Methods inherited from MultiJson::Adapter

defaults, dump, load

Methods included from Options

#default_dump_options, #default_load_options, #dump_options, #dump_options=, #load_options, #load_options=

Instance Method Details

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



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

def 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



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

def load(string, options = {})
  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