multi_json 1.5.1 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/.travis.yml +2 -1
- data/.yardopts +6 -0
- data/CHANGELOG.md +116 -0
- data/CONTRIBUTING.md +46 -0
- data/Gemfile +28 -4
- data/LICENSE.md +1 -1
- data/README.md +31 -15
- data/Rakefile +2 -10
- data/lib/multi_json.rb +113 -106
- data/lib/multi_json/adapters/gson.rb +20 -0
- data/lib/multi_json/adapters/json_common.rb +18 -8
- data/lib/multi_json/adapters/json_gem.rb +1 -1
- data/lib/multi_json/adapters/json_pure.rb +1 -1
- data/lib/multi_json/adapters/nsjsonserialization.rb +4 -3
- data/lib/multi_json/adapters/oj.rb +9 -7
- data/lib/multi_json/adapters/ok_json.rb +22 -12
- data/lib/multi_json/adapters/yajl.rb +5 -3
- data/lib/multi_json/version.rb +1 -1
- data/multi_json.gemspec +20 -21
- data/spec/adapter_shared_example.rb +55 -20
- data/spec/helper.rb +20 -14
- data/spec/json_common_shared_example.rb +34 -0
- data/spec/multi_json_spec.rb +57 -24
- metadata +44 -61
- metadata.gz.sig +0 -0
data.tar.gz.sig
ADDED
Binary file
|
data/.travis.yml
CHANGED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
1.6.0
|
2
|
+
-----
|
3
|
+
* [Add gson.rb support](https://github.com/intridea/multi_json/pull/71)
|
4
|
+
* [Add MultiJson.default_options](https://github.com/intridea/multi_json/pull/70)
|
5
|
+
* [Add MultiJson.with_adapter](https://github.com/intridea/multi_json/pull/67)
|
6
|
+
* [Stringify all possible keys for ok_json](https://github.com/intridea/multi_json/pull/66)
|
7
|
+
* [Alias `MultiJson::DecodeError` to `MultiJson::LoadError`](https://github.com/intridea/multi_json/pull/79)
|
8
|
+
|
9
|
+
1.5.1
|
10
|
+
-----
|
11
|
+
* [Do not allow Oj or JSON to create symbols by searching for classes](https://github.com/intridea/multi_json/commit/193e28cf4dc61b6e7b7b7d80f06f74c76df65c41)
|
12
|
+
|
13
|
+
1.5.0
|
14
|
+
-----
|
15
|
+
* [Add `MultiJson.with_adapter` method](https://github.com/intridea/multi_json/commit/d14c5d28cae96557a0421298621b9499e1f28104)
|
16
|
+
* [Stringify all possible keys for `ok_json`](https://github.com/intridea/multi_json/commit/73998074058e1e58c557ffa7b9541d486d6041fa)
|
17
|
+
|
18
|
+
1.4.0
|
19
|
+
-----
|
20
|
+
* [Allow `load`/`dump` of JSON fragments](https://github.com/intridea/multi_json/commit/707aae7d48d39c85b38febbd2c210ba87f6e4a36)
|
21
|
+
|
22
|
+
1.3.7
|
23
|
+
-----
|
24
|
+
* [Fix rescue clause for MagLev](https://github.com/intridea/multi_json/commit/39abdf50199828c50e85b2ce8f8ba31fcbbc9332)
|
25
|
+
* [Remove unnecessary check for string version of options key](https://github.com/intridea/multi_json/commit/660101b70e962b3c007d0b90d45944fa47d13ec4)
|
26
|
+
* [Explicitly set default adapter when adapter is set to `nil` or `false`](https://github.com/intridea/multi_json/commit/a9e587d5a63eafb4baee9fb211265e4dd96a26bc)
|
27
|
+
* [Fix Oj `ParseError` mapping for Oj 1.4.0](https://github.com/intridea/multi_json/commit/7d9045338cc9029401c16f3c409d54ce97f275e2)
|
28
|
+
|
29
|
+
1.3.6
|
30
|
+
-----
|
31
|
+
* [Allow adapter-specific options to be passed through to Oj](https://github.com/intridea/multi_json/commit/d0e5feeebcba0bc69400dd203a295f5c30971223)
|
32
|
+
|
33
|
+
1.3.5
|
34
|
+
-----
|
35
|
+
* [Add pretty support to Oj adapter](https://github.com/intridea/multi_json/commit/0c8f75f03020c53bcf4c6be258faf433d24b2c2b)
|
36
|
+
|
37
|
+
1.3.4
|
38
|
+
-----
|
39
|
+
* [Use `class << self` instead of `module_function` to create aliases](https://github.com/intridea/multi_json/commit/ba1451c4c48baa297e049889be241a424cb05980)
|
40
|
+
|
41
|
+
1.3.3
|
42
|
+
-----
|
43
|
+
* [Remove deprecation warnings](https://github.com/intridea/multi_json/commit/36b524e71544eb0186826a891bcc03b2820a008f)
|
44
|
+
|
45
|
+
1.3.2
|
46
|
+
-----
|
47
|
+
* [Add ability to use adapter per call](https://github.com/intridea/multi_json/commit/106bbec469d5d0a832bfa31fffcb8c0f0cdc9bd3)
|
48
|
+
* [Add and deprecate `default_engine` method](https://github.com/intridea/multi_json/commit/fc3df0c7a3e2ab9ce0c2c7e7617a4da97dd13f6e)
|
49
|
+
|
50
|
+
1.3.1
|
51
|
+
-----
|
52
|
+
* [Only warn once for each instance a deprecated method is called](https://github.com/intridea/multi_json/commit/e21d6eb7da74b3f283995c1d27d5880e75f0ae84)
|
53
|
+
|
54
|
+
1.3.0
|
55
|
+
-----
|
56
|
+
* [Implement `load`/`dump`; deprecate `decode`/`encode`](https://github.com/intridea/multi_json/commit/e90fd6cb1b0293eb0c73c2f4eb0f7a1764370216)
|
57
|
+
* [Rename engines to adapters](https://github.com/intridea/multi_json/commit/ae7fd144a7949a9c221dcaa446196ec23db908df)
|
58
|
+
|
59
|
+
1.2.0
|
60
|
+
-----
|
61
|
+
* [Add support for Oj](https://github.com/intridea/multi_json/commit/acd06b233edabe6c44f226873db7b49dab560c60)
|
62
|
+
|
63
|
+
1.1.0
|
64
|
+
-----
|
65
|
+
* [`NSJSONSerialization` support for MacRuby](https://github.com/intridea/multi_json/commit/f862e2fc966cac8867fe7da3997fc76e8a6cf5d4)
|
66
|
+
|
67
|
+
1.0.4
|
68
|
+
-----
|
69
|
+
* [Set data context to `DecodeError` exception](https://github.com/intridea/multi_json/commit/19ddafd44029c6681f66fae2a0f6eabfd0f85176)
|
70
|
+
* [Allow `ok_json` to fallback to `to_json`](https://github.com/intridea/multi_json/commit/c157240b1193b283d06d1bd4d4b5b06bcf3761f8)
|
71
|
+
* [Add warning when using `ok_json`](https://github.com/intridea/multi_json/commit/dd4b68810c84f826fb98f9713bfb29ab96888d57)
|
72
|
+
* [Options can be passed to an engine on encode](https://github.com/intridea/multi_json/commit/e0a7ff5d5ff621ffccc61617ed8aeec5816e81f7)
|
73
|
+
|
74
|
+
1.0.3
|
75
|
+
-----
|
76
|
+
* [`Array` support for `stringify_keys`](https://github.com/intridea/multi_json/commit/644d1c5c7c7f6a27663b11668527b346094e38b9)
|
77
|
+
* [`Array` support for `symbolize_keys`](https://github.com/intridea/multi_json/commit/c885377d47a2aa39cb0d971fea78db2d2fa479a7)
|
78
|
+
|
79
|
+
1.0.2
|
80
|
+
-----
|
81
|
+
* [Allow encoding of rootless JSON when `ok_json` is used](https://github.com/intridea/multi_json/commit/d1cde7de97cb0f6152aef8daf14037521cdce8c6)
|
82
|
+
|
83
|
+
1.0.1
|
84
|
+
-----
|
85
|
+
* [Correct an issue with `ok_json` not being returned as the default engine](https://github.com/intridea/multi_json/commit/d33c141619c54cccd770199694da8fd1bd8f449d)
|
86
|
+
|
87
|
+
1.0.0
|
88
|
+
-----
|
89
|
+
* [Remove `ActiveSupport::JSON` support](https://github.com/intridea/multi_json/commit/c2f4140141d785a24b3f56e58811b0e561b37f6a)
|
90
|
+
* [Fix `@engine` ivar warning](https://github.com/intridea/multi_json/commit/3b978a8995721a8dffedc3b75a7f49e5494ec553)
|
91
|
+
* [Only `rescue` from parsing errors during decoding, not any `StandardError`](https://github.com/intridea/multi_json/commit/391d00b5e85294d42d41347605d8d46b4a7f66cc)
|
92
|
+
* [Rename `okjson` engine and vendored lib to `ok_json`](https://github.com/intridea/multi_json/commit/5bd1afc977a8208ddb0443e1d57cb79665c019f1)
|
93
|
+
* [Add `StringIO` support to `json` gem and `ok_json`](https://github.com/intridea/multi_json/commit/1706b11568db7f50af451fce5f4d679aeb3bbe8f)
|
94
|
+
|
95
|
+
0.0.5
|
96
|
+
-----
|
97
|
+
* [Trap all JSON decoding errors; raise `MultiJson::DecodeError`](https://github.com/intridea/multi_json/commit/dea9a1aef6dd1212aa1e5a37ab1669f9b045b732)
|
98
|
+
|
99
|
+
0.0.4
|
100
|
+
-----
|
101
|
+
* [Fix default_engine check for `json` gem](https://github.com/intridea/multi_json/commit/caced0c4e8c795922a109ebc00c3c4fa8635bed8)
|
102
|
+
* [Make requirement mapper an `Array` to preserve order in Ruby versions < 1.9](https://github.com/intridea/multi_json/commit/526f5f29a42131574a088ad9bbb43d7f48439b2c)
|
103
|
+
|
104
|
+
0.0.3
|
105
|
+
-----
|
106
|
+
* [Improved defaulting and documentation](https://github.com/sferik/twitter/commit/3a0e41b9e4b0909201045fa47704b78c9d949b73)
|
107
|
+
|
108
|
+
0.0.2
|
109
|
+
-----
|
110
|
+
|
111
|
+
* [Rename to `multi_json`](https://github.com/sferik/twitter/commit/461ab89ce071c8c9fabfc183581e0ec523788b62)
|
112
|
+
|
113
|
+
0.0.1
|
114
|
+
-----
|
115
|
+
|
116
|
+
* [Initial commit](https://github.com/sferik/twitter/commit/518c21ab299c500527491e6c049ab2229e22a805)
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
## Contributing
|
2
|
+
In the spirit of [free software][free-sw], **everyone** is encouraged to help
|
3
|
+
improve this project.
|
4
|
+
|
5
|
+
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
|
6
|
+
|
7
|
+
Here are some ways *you* can contribute:
|
8
|
+
|
9
|
+
* by using alpha, beta, and prerelease versions
|
10
|
+
* by reporting bugs
|
11
|
+
* by suggesting new features
|
12
|
+
* by writing or editing documentation
|
13
|
+
* by writing specifications
|
14
|
+
* by writing code (**no patch is too small**: fix typos, add comments, clean up
|
15
|
+
inconsistent whitespace)
|
16
|
+
* by refactoring code
|
17
|
+
* by closing [issues][]
|
18
|
+
* by reviewing patches
|
19
|
+
|
20
|
+
[issues]: https://github.com/intridea/multi_json/issues
|
21
|
+
|
22
|
+
## Submitting an Issue
|
23
|
+
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
24
|
+
submitting a bug report or feature request, check to make sure it hasn't
|
25
|
+
already been submitted. When submitting a bug report, please include a [Gist][]
|
26
|
+
that includes a stack trace and any details that may be necessary to reproduce
|
27
|
+
the bug, including your gem version, Ruby version, and operating system.
|
28
|
+
Ideally, a bug report should include a pull request with failing specs.
|
29
|
+
|
30
|
+
[gist]: https://gist.github.com/
|
31
|
+
|
32
|
+
## Submitting a Pull Request
|
33
|
+
1. [Fork the repository.][fork]
|
34
|
+
2. [Create a topic branch.][branch]
|
35
|
+
3. Add specs for your unimplemented feature or bug fix.
|
36
|
+
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
37
|
+
5. Implement your feature or bug fix.
|
38
|
+
6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
|
39
|
+
7. Run `open coverage/index.html`. If your changes are not completely covered
|
40
|
+
by your tests, return to step 3.
|
41
|
+
8. Add, commit, and push your changes.
|
42
|
+
9. [Submit a pull request.][pr]
|
43
|
+
|
44
|
+
[fork]: http://help.github.com/fork-a-repo/
|
45
|
+
[branch]: http://learn.github.com/p/branching.html
|
46
|
+
[pr]: http://help.github.com/send-pull-requests/
|
data/Gemfile
CHANGED
@@ -1,7 +1,31 @@
|
|
1
|
-
source
|
1
|
+
source :rubygems
|
2
2
|
|
3
|
-
gem '
|
4
|
-
gem '
|
5
|
-
|
3
|
+
gem 'rake', '>= 0.9'
|
4
|
+
gem 'yard', '>= 0.8'
|
5
|
+
|
6
|
+
platforms :ruby_18 do
|
7
|
+
gem 'json', '~> 1.4', :require => nil
|
8
|
+
end
|
9
|
+
|
10
|
+
gem 'json_pure', '~> 1.4', :require => nil
|
11
|
+
|
12
|
+
platforms :ruby, :mswin, :mingw do
|
13
|
+
gem 'oj', '~> 2.0', :require => nil
|
14
|
+
gem 'yajl-ruby', '~> 1.0', :require => nil
|
15
|
+
end
|
16
|
+
platforms :jruby do
|
17
|
+
gem 'gson', '>= 0.6', :require => nil
|
18
|
+
end
|
19
|
+
|
20
|
+
group :development do
|
21
|
+
gem 'kramdown', '>= 0.14'
|
22
|
+
gem 'pry'
|
23
|
+
gem 'pry-debugger', :platforms => :mri_19
|
24
|
+
end
|
25
|
+
|
26
|
+
group :test do
|
27
|
+
gem 'rspec', '>= 2.11'
|
28
|
+
gem 'simplecov', :require => false
|
29
|
+
end
|
6
30
|
|
7
31
|
gemspec
|
data/LICENSE.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2010 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober,
|
1
|
+
Copyright (c) 2010-2013 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober, Pavel Pravosud
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -1,19 +1,33 @@
|
|
1
|
-
# MultiJSON
|
1
|
+
# MultiJSON
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/multi_json.png)][gem]
|
3
|
+
[![Build Status](https://secure.travis-ci.org/intridea/multi_json.png?branch=master)][travis]
|
4
|
+
[![Dependency Status](https://gemnasium.com/intridea/multi_json.png?travis)][gemnasium]
|
5
|
+
[![Code Climate](https://codeclimate.com/github/intridea/multi_json.png)][codeclimate]
|
2
6
|
|
7
|
+
[gem]: https://rubygems.org/gems/multi_json
|
3
8
|
[travis]: http://travis-ci.org/intridea/multi_json
|
4
9
|
[gemnasium]: https://gemnasium.com/intridea/multi_json
|
10
|
+
[codeclimate]: https://codeclimate.com/github/intridea/multi_json
|
5
11
|
|
6
12
|
Lots of Ruby libraries parse JSON and everyone has their favorite JSON coder.
|
7
13
|
Instead of choosing a single JSON coder and forcing users of your library to be
|
8
14
|
stuck with it, you can use MultiJSON instead, which will simply choose the
|
9
15
|
fastest available JSON coder. Here's how to use it:
|
10
16
|
|
11
|
-
|
17
|
+
```ruby
|
18
|
+
require 'multi_json'
|
12
19
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
20
|
+
MultiJson.load('{"abc":"def"}') #=> {"abc" => "def"}
|
21
|
+
MultiJson.load('{"abc":"def"}', :symbolize_keys => true) #=> {:abc => "def"}
|
22
|
+
MultiJson.dump({:abc => 'def'}) # convert Ruby back to JSON
|
23
|
+
MultiJson.dump({:abc => 'def'}, :pretty => true) # encoded in a pretty form (if supported by the coder)
|
24
|
+
```
|
25
|
+
|
26
|
+
When loading invalid JSON, multiJSON will throw a `MultiJson::LoadError`. `MultiJson::DecodeError` is an alias for backwards compatibility.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
MultiJson.load('invalid json') #=> MultiJson::LoadError
|
30
|
+
```
|
17
31
|
|
18
32
|
The `use` method, which sets the MultiJson adapter, takes either a symbol or a
|
19
33
|
class (to allow for custom JSON parsers) that responds to both `.load` and `.dump`
|
@@ -43,6 +57,7 @@ implementations:
|
|
43
57
|
* Ruby 1.8.7
|
44
58
|
* Ruby 1.9.2
|
45
59
|
* Ruby 1.9.3
|
60
|
+
* Ruby 2.0.0
|
46
61
|
* [JRuby][]
|
47
62
|
* [Rubinius][]
|
48
63
|
* [MacRuby][] (not tested on Travis CI)
|
@@ -51,8 +66,7 @@ implementations:
|
|
51
66
|
[rubinius]: http://rubini.us/
|
52
67
|
[macruby]: http://www.macruby.org/
|
53
68
|
|
54
|
-
If something doesn't work on one of these interpreters, it
|
55
|
-
a bug.
|
69
|
+
If something doesn't work on one of these interpreters, it's a bug.
|
56
70
|
|
57
71
|
This library may inadvertently work (or seem to work) on other Ruby
|
58
72
|
implementations, however support will only be provided for the versions listed
|
@@ -61,9 +75,9 @@ above.
|
|
61
75
|
If you would like this library to support another Ruby version, you may
|
62
76
|
volunteer to be a maintainer. Being a maintainer entails making sure all tests
|
63
77
|
run and pass on that implementation. When something breaks on your
|
64
|
-
implementation, you will be
|
65
|
-
|
66
|
-
|
78
|
+
implementation, you will be responsible for providing patches in a timely
|
79
|
+
fashion. If critical issues for a particular implementation exist at the time
|
80
|
+
of a major release, support for that Ruby version may be dropped.
|
67
81
|
|
68
82
|
## Versioning
|
69
83
|
|
@@ -76,13 +90,15 @@ introduced with new major versions. As a result of this policy, you can (and
|
|
76
90
|
should) specify a dependency on this gem using the [Pessimistic Version
|
77
91
|
Constraint][pvc] with two digits of precision. For example:
|
78
92
|
|
79
|
-
|
93
|
+
```ruby
|
94
|
+
spec.add_dependency 'multi_json', '~> 1.0'
|
95
|
+
```
|
80
96
|
|
81
97
|
[semver]: http://semver.org/
|
82
98
|
[pvc]: http://docs.rubygems.org/read/chapter/16#page74
|
83
99
|
|
84
100
|
## Copyright
|
85
|
-
Copyright (c) 2010 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober,
|
86
|
-
See [LICENSE][] for details.
|
101
|
+
Copyright (c) 2010-2013 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober,
|
102
|
+
and Pavel Pravosud. See [LICENSE][] for details.
|
87
103
|
|
88
|
-
[license]:
|
104
|
+
[license]: LICENSE.md
|
data/Rakefile
CHANGED
@@ -8,13 +8,5 @@ RSpec::Core::RakeTask.new(:spec)
|
|
8
8
|
task :default => :spec
|
9
9
|
task :test => :spec
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
require File.expand_path('../lib/multi_json/version', __FILE__)
|
14
|
-
RDoc::Task.new do |rdoc|
|
15
|
-
rdoc.rdoc_dir = 'rdoc'
|
16
|
-
rdoc.title = "multi_json #{MultiJson::VERSION}"
|
17
|
-
rdoc.main = 'README.md'
|
18
|
-
rdoc.rdoc_files.include('README.md', 'LICENSE.md', 'lib/**/*.rb')
|
19
|
-
end
|
20
|
-
end
|
11
|
+
require 'yard'
|
12
|
+
YARD::Rake::YardocTask.new
|
data/lib/multi_json.rb
CHANGED
@@ -1,130 +1,137 @@
|
|
1
1
|
module MultiJson
|
2
|
-
|
2
|
+
extend self
|
3
|
+
|
4
|
+
class LoadError < StandardError
|
3
5
|
attr_reader :data
|
4
|
-
def initialize(message=
|
6
|
+
def initialize(message='', backtrace=[], data='')
|
5
7
|
super(message)
|
6
8
|
self.set_backtrace(backtrace)
|
7
9
|
@data = data
|
8
10
|
end
|
9
11
|
end
|
12
|
+
DecodeError = LoadError # Legacy support
|
13
|
+
|
10
14
|
|
11
|
-
@
|
15
|
+
@default_options = {}
|
16
|
+
attr_accessor :default_options
|
12
17
|
|
13
18
|
REQUIREMENT_MAP = [
|
14
|
-
[
|
15
|
-
[
|
16
|
-
[
|
17
|
-
[
|
19
|
+
['oj', :oj],
|
20
|
+
['yajl', :yajl],
|
21
|
+
['json', :json_gem],
|
22
|
+
['gson', :gson],
|
23
|
+
['json/pure', :json_pure]
|
18
24
|
]
|
19
25
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
return :json_gem if defined?(::JSON)
|
30
|
-
|
31
|
-
REQUIREMENT_MAP.each do |(library, adapter)|
|
32
|
-
begin
|
33
|
-
require library
|
34
|
-
return adapter
|
35
|
-
rescue LoadError
|
36
|
-
next
|
37
|
-
end
|
38
|
-
end
|
26
|
+
# The default adapter based on what you currently
|
27
|
+
# have loaded and installed. First checks to see
|
28
|
+
# if any adapters are already loaded, then checks
|
29
|
+
# to see which are installed if none are loaded.
|
30
|
+
def default_adapter
|
31
|
+
return :oj if defined?(::Oj)
|
32
|
+
return :yajl if defined?(::Yajl)
|
33
|
+
return :json_gem if defined?(::JSON)
|
34
|
+
return :gson if defined?(::Gson)
|
39
35
|
|
40
|
-
|
41
|
-
:ok_json
|
42
|
-
end
|
43
|
-
# :nodoc:
|
44
|
-
alias :default_engine :default_adapter
|
45
|
-
|
46
|
-
# Get the current adapter class.
|
47
|
-
def adapter
|
48
|
-
return @adapter if @adapter
|
49
|
-
self.use self.default_adapter
|
50
|
-
@adapter
|
51
|
-
end
|
52
|
-
# :nodoc:
|
53
|
-
alias :engine :adapter
|
54
|
-
|
55
|
-
# Set the JSON parser utilizing a symbol, string, or class.
|
56
|
-
# Supported by default are:
|
57
|
-
#
|
58
|
-
# * <tt>:oj</tt>
|
59
|
-
# * <tt>:json_gem</tt>
|
60
|
-
# * <tt>:json_pure</tt>
|
61
|
-
# * <tt>:ok_json</tt>
|
62
|
-
# * <tt>:yajl</tt>
|
63
|
-
# * <tt>:nsjsonserialization</tt> (MacRuby only)
|
64
|
-
def use(new_adapter)
|
65
|
-
@adapter = load_adapter(new_adapter)
|
66
|
-
end
|
67
|
-
alias :adapter= :use
|
68
|
-
# :nodoc:
|
69
|
-
alias :engine= :use
|
70
|
-
|
71
|
-
def load_adapter(new_adapter)
|
72
|
-
case new_adapter
|
73
|
-
when String, Symbol
|
74
|
-
require "multi_json/adapters/#{new_adapter}"
|
75
|
-
MultiJson::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
|
76
|
-
when NilClass, FalseClass
|
77
|
-
default_adapter = self.default_adapter
|
78
|
-
require "multi_json/adapters/#{default_adapter}"
|
79
|
-
MultiJson::Adapters.const_get(:"#{default_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
|
80
|
-
when Class
|
81
|
-
new_adapter
|
82
|
-
else
|
83
|
-
raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
# Decode a JSON string into Ruby.
|
88
|
-
#
|
89
|
-
# <b>Options</b>
|
90
|
-
#
|
91
|
-
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
|
92
|
-
# <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
|
93
|
-
def load(string, options={})
|
94
|
-
adapter = current_adapter(options)
|
36
|
+
REQUIREMENT_MAP.each do |(library, adapter)|
|
95
37
|
begin
|
96
|
-
|
97
|
-
|
98
|
-
|
38
|
+
require library
|
39
|
+
return adapter
|
40
|
+
rescue ::LoadError
|
41
|
+
next
|
99
42
|
end
|
100
43
|
end
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
44
|
+
|
45
|
+
Kernel.warn '[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance.'
|
46
|
+
:ok_json
|
47
|
+
end
|
48
|
+
# :nodoc:
|
49
|
+
alias :default_engine :default_adapter
|
50
|
+
|
51
|
+
# Get the current adapter class.
|
52
|
+
def adapter
|
53
|
+
return @adapter if defined?(@adapter) && @adapter
|
54
|
+
|
55
|
+
self.use nil # load default adapter
|
56
|
+
|
57
|
+
@adapter
|
58
|
+
end
|
59
|
+
# :nodoc:
|
60
|
+
alias :engine :adapter
|
61
|
+
|
62
|
+
# Set the JSON parser utilizing a symbol, string, or class.
|
63
|
+
# Supported by default are:
|
64
|
+
#
|
65
|
+
# * <tt>:oj</tt>
|
66
|
+
# * <tt>:json_gem</tt>
|
67
|
+
# * <tt>:json_pure</tt>
|
68
|
+
# * <tt>:ok_json</tt>
|
69
|
+
# * <tt>:yajl</tt>
|
70
|
+
# * <tt>:nsjsonserialization</tt> (MacRuby only)
|
71
|
+
# * <tt>:gson</tt> (JRuby only)
|
72
|
+
def use(new_adapter)
|
73
|
+
@adapter = load_adapter(new_adapter)
|
74
|
+
end
|
75
|
+
alias :adapter= :use
|
76
|
+
# :nodoc:
|
77
|
+
alias :engine= :use
|
78
|
+
|
79
|
+
def load_adapter(new_adapter)
|
80
|
+
case new_adapter
|
81
|
+
when String, Symbol
|
82
|
+
require "multi_json/adapters/#{new_adapter}"
|
83
|
+
MultiJson::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
|
84
|
+
when NilClass, FalseClass
|
85
|
+
load_adapter default_adapter
|
86
|
+
when Class, Module
|
87
|
+
new_adapter
|
88
|
+
else
|
89
|
+
raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
|
110
90
|
end
|
91
|
+
end
|
111
92
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
93
|
+
# Decode a JSON string into Ruby.
|
94
|
+
#
|
95
|
+
# <b>Options</b>
|
96
|
+
#
|
97
|
+
# <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
|
98
|
+
# <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
|
99
|
+
def load(string, options={})
|
100
|
+
adapter = current_adapter(options)
|
101
|
+
begin
|
102
|
+
adapter.load(string, options)
|
103
|
+
rescue adapter::ParseError => exception
|
104
|
+
raise LoadError.new(exception.message, exception.backtrace, string)
|
116
105
|
end
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
106
|
+
end
|
107
|
+
# :nodoc:
|
108
|
+
alias :decode :load
|
109
|
+
|
110
|
+
def current_adapter(options)
|
111
|
+
if new_adapter = (options || {}).delete(:adapter)
|
112
|
+
load_adapter(new_adapter)
|
113
|
+
else
|
114
|
+
adapter
|
125
115
|
end
|
126
|
-
|
116
|
+
end
|
117
|
+
|
118
|
+
# Encodes a Ruby object as JSON.
|
119
|
+
def dump(object, options={})
|
120
|
+
options = default_options.merge(options)
|
121
|
+
adapter = current_adapter(options)
|
122
|
+
adapter.dump(object, options)
|
123
|
+
end
|
124
|
+
# :nodoc:
|
125
|
+
alias :encode :dump
|
127
126
|
|
127
|
+
# Executes passed block using specified adapter.
|
128
|
+
def with_adapter(new_adapter)
|
129
|
+
old_adapter, self.adapter = adapter, new_adapter
|
130
|
+
yield
|
131
|
+
ensure
|
132
|
+
self.adapter = old_adapter
|
128
133
|
end
|
134
|
+
# :nodoc:
|
135
|
+
alias :with_engine :with_adapter
|
129
136
|
|
130
137
|
end
|