Skip to content

Commit f862e2f

Browse files
committed
NSJSONSerialization support for MacRuby
Implemented NSJSONSerialization as a subclass of MultiJson::Engines::OKJson for best compatibility to the MultiJson spec. :nsjsonserialization is exclusively available for MacRuby.
1 parent b1c3868 commit f862e2f

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

lib/multi_json.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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

spec/helper.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
def macruby?
2+
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'
3+
end
4+
5+
if macruby?
6+
framework 'Cocoa'
7+
end
8+
19
require 'simplecov'
2-
SimpleCov.start
10+
SimpleCov.start unless macruby?
311
require 'multi_json'
412
require 'rspec'
513

@@ -23,6 +31,10 @@ def yajl_on_travis(engine)
2331
ENV['TRAVIS'] && engine == 'yajl' && jruby?
2432
end
2533

34+
def nsjsonserialization_on_other_than_macruby(engine)
35+
engine == 'nsjsonserialization' && !macruby?
36+
end
37+
2638
def jruby?
2739
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
2840
end

spec/multi_json_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@
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

0 commit comments

Comments
 (0)