multi_json 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -2
- data/Gemfile +1 -1
- data/README.md +64 -36
- data/lib/multi_json.rb +3 -0
- data/lib/multi_json/engines/nsjsonserialization.rb +34 -0
- data/lib/multi_json/version.rb +1 -1
- data/spec/helper.rb +8 -7
- data/spec/multi_json_spec.rb +10 -3
- metadata +12 -11
data/.rspec
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,35 +1,50 @@
|
|
1
|
-
# MultiJSON
|
1
|
+
# MultiJSON [![Build Status](https://secure.travis-ci.org/intridea/multi_json.png?branch=master)][travis] [![Dependency Status](https://gemnasium.com/intridea/multi_json.png?travis)][gemnasium]
|
2
|
+
|
3
|
+
[travis]: http://travis-ci.org/intridea/multi_json
|
4
|
+
[gemnasium]: https://gemnasium.com/intridea/multi_json
|
5
|
+
|
2
6
|
Lots of Ruby libraries utilize JSON parsing in some form, and everyone has
|
3
7
|
their favorite JSON library. In order to best support multiple JSON parsers and
|
4
|
-
libraries,
|
5
|
-
|
8
|
+
libraries, `multi_json` is a general-purpose swappable JSON backend library.
|
9
|
+
You use it like so:
|
6
10
|
|
7
11
|
require 'multi_json'
|
8
12
|
|
9
13
|
MultiJson.engine = :yajl
|
10
14
|
MultiJson.decode('{ "abc":"def" }') # decoded using Yajl
|
11
15
|
|
16
|
+
MultiJson.decode('{ "abc":"def" }', :symbolize_keys => true) # for symbol keys: {:abc => "def"}
|
17
|
+
|
12
18
|
MultiJson.engine = :json_gem
|
13
19
|
MultiJson.engine = MultiJson::Engines::JsonGem # equivalent to previous line
|
14
20
|
MultiJson.encode({ :abc => 'def' }) # encoded using the JSON gem
|
15
21
|
|
16
22
|
MultiJson.encode({ :abc => 'def' }, :pretty => true) # encoded in a pretty form (ignored if engine is ok_json)
|
17
23
|
|
18
|
-
The
|
19
|
-
|
20
|
-
<tt>.encode</tt> at the class level.
|
24
|
+
The `engine` setter takes either a symbol or a class (to allow for custom JSON
|
25
|
+
parsers) that responds to both `.decode` and `.encode` at the class level.
|
21
26
|
|
22
27
|
MultiJSON tries to have intelligent defaulting. That is, if you have any of the
|
23
28
|
supported engines already loaded, it will utilize them before attempting to
|
24
29
|
load any. When loading, libraries are ordered by speed. First Yajl-Ruby, then
|
25
30
|
the JSON gem, then JSON pure. If no JSON library is available, MultiJSON falls
|
26
|
-
back to a bundled version of [OkJson]
|
31
|
+
back to a bundled version of [OkJson][].
|
32
|
+
|
33
|
+
[okjson]: https://github.com/kr/okjson
|
34
|
+
|
35
|
+
## Supported JSON Engines
|
36
|
+
|
37
|
+
* [`:yajl`](https://github.com/brianmario/yajl-ruby) Yet another json library, C extension
|
38
|
+
* [`:json_gem`](https://github.com/genki/json) Json gem as C extension
|
39
|
+
* [`:json_pure`](https://github.com/genki/json) Pure ruby implementation of the json gem
|
40
|
+
* [`:ok_json`][okjson] Pure ruby implementation, aiming for maximum compatibility
|
41
|
+
* [`:nsjsonserialization`](https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html) Wrapper for Apple's NSJSONSerialization out of the Cocoa Framework (MacRuby only)
|
27
42
|
|
28
|
-
## <a name="
|
29
|
-
[
|
43
|
+
## <a name="contributing"></a>Contributing
|
44
|
+
In the spirit of [free software][free-sw], **everyone** is encouraged to help
|
45
|
+
improve this project.
|
30
46
|
|
31
|
-
|
32
|
-
In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project.
|
47
|
+
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
|
33
48
|
|
34
49
|
Here are some ways *you* can contribute:
|
35
50
|
|
@@ -38,42 +53,53 @@ Here are some ways *you* can contribute:
|
|
38
53
|
* by suggesting new features
|
39
54
|
* by writing or editing documentation
|
40
55
|
* by writing specifications
|
41
|
-
* by writing code (**no patch is too small**: fix typos, add comments, clean up
|
56
|
+
* by writing code (**no patch is too small**: fix typos, add comments, clean up
|
57
|
+
inconsistent whitespace)
|
42
58
|
* by refactoring code
|
43
|
-
* by closing [issues]
|
59
|
+
* by closing [issues][]
|
44
60
|
* by reviewing patches
|
45
61
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
that may be necessary to reproduce the
|
54
|
-
version, and operating system. Ideally, a
|
55
|
-
request with failing specs.
|
56
|
-
|
57
|
-
|
62
|
+
[issues]: https://github.com/intridea/multi_json/issues
|
63
|
+
|
64
|
+
## <a name="issues"></a>Submitting an Issue
|
65
|
+
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
66
|
+
submitting a bug report or feature request, check to make sure it hasn't
|
67
|
+
already been submitted. You can indicate support for an existing issuse by
|
68
|
+
voting it up. When submitting a bug report, please include a [Gist][] that
|
69
|
+
includes a stack trace and any details that may be necessary to reproduce the
|
70
|
+
bug, including your gem version, Ruby version, and operating system. Ideally, a
|
71
|
+
bug report should include a pull request with failing specs.
|
72
|
+
|
73
|
+
[gist]: https://gist.github.com/
|
74
|
+
|
75
|
+
## <a name="pulls"></a>Submitting a Pull Request
|
58
76
|
1. Fork the project.
|
59
77
|
2. Create a topic branch.
|
60
78
|
3. Implement your feature or bug fix.
|
61
79
|
4. Add specs for your feature or bug fix.
|
62
|
-
5. Run
|
80
|
+
5. Run `bundle exec rake spec`. If your changes are not 100% covered, go back
|
81
|
+
to step 4.
|
63
82
|
6. Commit and push your changes.
|
64
|
-
7. Submit a pull request. Please do not include changes to the gemspec,
|
83
|
+
7. Submit a pull request. Please do not include changes to the gemspec,
|
84
|
+
version, or history file. (If you want to create your own version for some
|
85
|
+
reason, please do so in a separate commit.)
|
65
86
|
|
66
|
-
## <a name="
|
67
|
-
This library aims to support and is [tested
|
68
|
-
against](http://travis-ci.org/intridea/multi_json) the following Ruby
|
87
|
+
## <a name="versions"></a>Supported Ruby Versions
|
88
|
+
This library aims to support and is [tested against][travis] the following Ruby
|
69
89
|
implementations:
|
70
90
|
|
71
91
|
* Ruby 1.8.7
|
72
|
-
* Ruby 1.9.1
|
73
92
|
* Ruby 1.9.2
|
74
|
-
*
|
75
|
-
* [
|
76
|
-
* [
|
93
|
+
* Ruby 1.9.3
|
94
|
+
* [JRuby][]
|
95
|
+
* [Rubinius][]
|
96
|
+
* [Ruby Enterprise Edition][ree]
|
97
|
+
* [MacRuby][] (not tested on Travis CI)
|
98
|
+
|
99
|
+
[jruby]: http://www.jruby.org/
|
100
|
+
[rubinius]: http://rubini.us/
|
101
|
+
[ree]: http://www.rubyenterpriseedition.com/
|
102
|
+
[macruby]: http://www.macruby.org/
|
77
103
|
|
78
104
|
If something doesn't work on one of these interpreters, it should be considered
|
79
105
|
a bug.
|
@@ -89,6 +115,8 @@ implementation, you will be personally responsible for providing patches in a
|
|
89
115
|
timely fashion. If critical issues for a particular implementation exist at the
|
90
116
|
time of a major release, support for that Ruby version may be dropped.
|
91
117
|
|
92
|
-
## <a name="copyright">Copyright
|
118
|
+
## <a name="copyright"></a>Copyright
|
93
119
|
Copyright (c) 2010 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober, and Intridea, Inc.
|
94
|
-
See [LICENSE]
|
120
|
+
See [LICENSE][] for details.
|
121
|
+
|
122
|
+
[license]: https://github.com/intridea/multi_json/blob/master/LICENSE.md
|
data/lib/multi_json.rb
CHANGED
@@ -55,11 +55,14 @@ module MultiJson
|
|
55
55
|
# * <tt>:json_pure</tt>
|
56
56
|
# * <tt>:ok_json</tt>
|
57
57
|
# * <tt>:yajl</tt>
|
58
|
+
# * <tt>:nsjsonserialization</tt> (MacRuby only)
|
58
59
|
def engine=(new_engine)
|
59
60
|
case new_engine
|
60
61
|
when String, Symbol
|
61
62
|
require "multi_json/engines/#{new_engine}"
|
62
63
|
@engine = MultiJson::Engines.const_get("#{new_engine.to_s.split('_').map{|s| s.capitalize}.join('')}")
|
64
|
+
when NilClass
|
65
|
+
@engine = nil
|
63
66
|
when Class
|
64
67
|
@engine = new_engine
|
65
68
|
else
|
@@ -0,0 +1,34 @@
|
|
1
|
+
framework 'Foundation'
|
2
|
+
require 'multi_json/engines/ok_json'
|
3
|
+
|
4
|
+
module MultiJson
|
5
|
+
module Engines
|
6
|
+
class Nsjsonserialization < MultiJson::Engines::OkJson
|
7
|
+
ParseError = ::MultiJson::OkJson::Error
|
8
|
+
|
9
|
+
def self.decode(string, options = {})
|
10
|
+
string = string.read if string.respond_to?(:read)
|
11
|
+
data = string.dataUsingEncoding(NSUTF8StringEncoding)
|
12
|
+
object = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves, error: nil)
|
13
|
+
if object
|
14
|
+
object = symbolize_keys(object) if options[:symbolize_keys]
|
15
|
+
object
|
16
|
+
else
|
17
|
+
super(string, options = {})
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.encode(object, options = {})
|
22
|
+
pretty = options[:pretty] ? NSJSONWritingPrettyPrinted : 0
|
23
|
+
object = object.as_json if object.respond_to?(:as_json)
|
24
|
+
if NSJSONSerialization.isValidJSONObject(object)
|
25
|
+
data = NSJSONSerialization.dataWithJSONObject(object, options: pretty, error: nil)
|
26
|
+
NSMutableString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
|
27
|
+
else
|
28
|
+
super(object, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/multi_json/version.rb
CHANGED
data/spec/helper.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
def macruby?
|
2
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'
|
3
3
|
end
|
4
4
|
|
5
5
|
require 'simplecov'
|
6
|
-
SimpleCov.start
|
7
|
-
|
6
|
+
SimpleCov.start unless macruby?
|
8
7
|
require 'multi_json'
|
9
8
|
require 'rspec'
|
10
9
|
|
11
|
-
|
12
10
|
class MockDecoder
|
13
11
|
def self.decode(string, options = {})
|
14
12
|
{'abc' => 'def'}
|
@@ -25,11 +23,14 @@ class TimeWithZone
|
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
28
|
-
|
29
26
|
def yajl_on_travis(engine)
|
30
27
|
ENV['TRAVIS'] && engine == 'yajl' && jruby?
|
31
28
|
end
|
32
29
|
|
30
|
+
def nsjsonserialization_on_other_than_macruby(engine)
|
31
|
+
engine == 'nsjsonserialization' && !macruby?
|
32
|
+
end
|
33
|
+
|
33
34
|
def jruby?
|
34
35
|
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
|
35
|
-
end
|
36
|
+
end
|
data/spec/multi_json_spec.rb
CHANGED
@@ -3,8 +3,11 @@ require 'stringio'
|
|
3
3
|
|
4
4
|
describe "MultiJson" do
|
5
5
|
context 'engines' do
|
6
|
+
before do
|
7
|
+
MultiJson.engine = nil
|
8
|
+
end
|
6
9
|
context 'when no other json implementations are available' do
|
7
|
-
before
|
10
|
+
before do
|
8
11
|
@old_map = MultiJson::REQUIREMENT_MAP
|
9
12
|
@old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
|
10
13
|
@old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
|
@@ -15,7 +18,7 @@ describe "MultiJson" do
|
|
15
18
|
Object.send :remove_const, :JSON if @old_json
|
16
19
|
end
|
17
20
|
|
18
|
-
after
|
21
|
+
after do
|
19
22
|
@old_map.each_with_index do |(library, engine), index|
|
20
23
|
MultiJson::REQUIREMENT_MAP[index] = [library, engine]
|
21
24
|
end
|
@@ -54,11 +57,15 @@ describe "MultiJson" do
|
|
54
57
|
end
|
55
58
|
end
|
56
59
|
|
57
|
-
%w(json_gem json_pure ok_json yajl).each do |engine|
|
60
|
+
%w(json_gem json_pure ok_json yajl nsjsonserialization).each do |engine|
|
58
61
|
if yajl_on_travis(engine)
|
59
62
|
puts "Yajl with JRuby is not tested on Travis as C-exts are turned off due to there experimental nature"
|
60
63
|
next
|
61
64
|
end
|
65
|
+
if nsjsonserialization_on_other_than_macruby(engine)
|
66
|
+
puts "NSJSONSerialization is exclusively available for MacRuby only."
|
67
|
+
next
|
68
|
+
end
|
62
69
|
|
63
70
|
context engine do
|
64
71
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2012-02-18 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rake
|
18
|
-
requirement: &
|
18
|
+
requirement: &70295944803660 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '0.9'
|
24
24
|
type: :development
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70295944803660
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rdoc
|
29
|
-
requirement: &
|
29
|
+
requirement: &70295944803060 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ~>
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: '3.9'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70295944803060
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: rspec
|
40
|
-
requirement: &
|
40
|
+
requirement: &70295944802460 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: '2.6'
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70295944802460
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: simplecov
|
51
|
-
requirement: &
|
51
|
+
requirement: &70295944801920 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ~>
|
@@ -56,7 +56,7 @@ dependencies:
|
|
56
56
|
version: '0.4'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *70295944801920
|
60
60
|
description: A gem to provide swappable JSON backends utilizing Yajl::Ruby, the JSON
|
61
61
|
gem, JSON pure, or a vendored version of okjson.
|
62
62
|
email:
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- lib/multi_json/engines/json_common.rb
|
83
83
|
- lib/multi_json/engines/json_gem.rb
|
84
84
|
- lib/multi_json/engines/json_pure.rb
|
85
|
+
- lib/multi_json/engines/nsjsonserialization.rb
|
85
86
|
- lib/multi_json/engines/ok_json.rb
|
86
87
|
- lib/multi_json/engines/yajl.rb
|
87
88
|
- lib/multi_json/vendor/ok_json.rb
|
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
109
|
version: 1.3.6
|
109
110
|
requirements: []
|
110
111
|
rubyforge_project:
|
111
|
-
rubygems_version: 1.8.
|
112
|
+
rubygems_version: 1.8.17
|
112
113
|
signing_key:
|
113
114
|
specification_version: 3
|
114
115
|
summary: A gem to provide swappable JSON backends.
|