Class: MultiJson::Adapters::Nsjsonserialization

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

Constant Summary collapse

ParseError =
::MultiJson::OkJson::Error

Instance Attribute Summary

Attributes included from Options

#dump_options, #load_options

Instance Method Summary collapse

Methods inherited from MultiJson::Adapter

activate!, defaults, dump, load

Methods included from Options

#default_dump_options, #default_load_options

Instance 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 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 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