File: nsjsonserialization.rb

package info (click to toggle)
ruby-multi-json 1.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 180 kB
  • sloc: ruby: 845; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,176 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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