File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ def default_engine
5555 # * <tt>:json_pure</tt>
5656 # * <tt>:ok_json</tt>
5757 # * <tt>:yajl</tt>
58+ # * <tt>:nsjsonserialization</tt> (MacRuby only)
5859 def engine = ( new_engine )
5960 case new_engine
6061 when String , Symbol
Original file line number Diff line number Diff line change 1+ require 'multi_json/engines/ok_json'
2+
3+ module MultiJson
4+ module Engines
5+ class Nsjsonserialization < MultiJson ::Engines ::OkJson
6+ ParseError = ::MultiJson ::OkJson ::Error
7+
8+ def self . decode ( string , options = { } )
9+ string = string . read if string . respond_to? ( :read )
10+ data = string . dataUsingEncoding ( NSUTF8StringEncoding )
11+ object = NSJSONSerialization . JSONObjectWithData ( data , options : NSJSONReadingMutableContainers , error : nil )
12+ if object
13+ object = symbolize_keys ( object ) if options [ :symbolize_keys ]
14+ object
15+ else
16+ super ( string , options = { } )
17+ end
18+ end
19+
20+ def self . encode ( object , options = { } )
21+ pretty = options [ :pretty ] ? NSJSONWritingPrettyPrinted : 0
22+ object = object . as_json if object . respond_to? ( :as_json )
23+ if NSJSONSerialization . isValidJSONObject ( object )
24+ data = NSJSONSerialization . dataWithJSONObject ( object , options : pretty , error : nil )
25+ NSString . alloc . initWithData ( data , encoding : NSUTF8StringEncoding )
26+ else
27+ super ( object , options )
28+ end
29+ end
30+
31+ end
32+ end
33+ end
Original file line number Diff line number Diff line change 1+ def macruby?
2+ defined? ( RUBY_ENGINE ) && RUBY_ENGINE == 'macruby'
3+ end
4+
5+ if macruby?
6+ framework 'Cocoa'
7+ end
8+
19require 'simplecov'
2- SimpleCov . start
10+ SimpleCov . start unless macruby?
311require 'multi_json'
412require 'rspec'
513
@@ -23,6 +31,10 @@ def yajl_on_travis(engine)
2331 ENV [ 'TRAVIS' ] && engine == 'yajl' && jruby?
2432end
2533
34+ def nsjsonserialization_on_other_than_macruby ( engine )
35+ engine == 'nsjsonserialization' && !macruby?
36+ end
37+
2638def jruby?
2739 defined? ( RUBY_ENGINE ) && RUBY_ENGINE == 'jruby'
2840end
Original file line number Diff line number Diff line change 5757 end
5858 end
5959
60- %w( json_gem json_pure ok_json yajl ) . each do |engine |
60+ %w( json_gem json_pure ok_json yajl nsjsonserialization ) . each do |engine |
6161 if yajl_on_travis ( engine )
6262 puts "Yajl with JRuby is not tested on Travis as C-exts are turned off due to there experimental nature"
6363 next
6464 end
65+ if nsjsonserialization_on_other_than_macruby ( engine )
66+ puts "NSJSONSerialization is exclusively available for MacRuby only."
67+ next
68+ end
6569
6670 context engine do
6771 before do
You can’t perform that action at this time.
0 commit comments