brew/Library/Homebrew/vendor/multi_json/adapters/nsjsonserialization.rb
Jack Nagel 997f9d0075 Update multi_json to 1.5.0
This contains updates to the OkJson library that allow objects to define
to_json for serialization, and this will be used in the upcoming options
and deps work.
2013-01-17 21:39:43 -06:00

35 lines
1.1 KiB
Ruby

framework 'Foundation'
require 'multi_json/adapters/ok_json'
module MultiJson
module Adapters
class Nsjsonserialization < MultiJson::Adapters::OkJson
ParseError = ::MultiJson::OkJson::Error
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
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
end
end
end