multi_json-maglev- 1.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ LICENSE.md
2
+ README.md
3
+ bin/*
4
+ features/**/*.feature
5
+ lib/**/*.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - rbx-18mode
4
+ - rbx-19mode
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - 1.8.7
8
+ - 1.9.2
9
+ - 1.9.3
10
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'json', '~> 1.4', :require => nil
4
+ gem 'oj', '~> 1.0', :require => nil, :platforms => [:ruby, :mswin, :mingw]
5
+ gem 'yajl-ruby', '~> 1.0', :require => nil, :platforms => [:ruby, :mswin, :mingw]
6
+
7
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober, and Intridea, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
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
+
6
+ Lots of Ruby libraries parse JSON and everyone has their favorite JSON coder.
7
+ Instead of choosing a single JSON coder and forcing users of your library to be
8
+ stuck with it, you can use MultiJSON instead, which will simply choose the
9
+ fastest available JSON coder. Here's how to use it:
10
+
11
+ require 'multi_json'
12
+
13
+ MultiJson.load('{"abc":"def"}') #=> {"abc" => "def"}
14
+ MultiJson.load('{"abc":"def"}', :symbolize_keys => true) #=> {:abc => "def"}
15
+ MultiJson.dump({:abc => 'def'}) # convert Ruby back to JSON
16
+ MultiJson.dump({:abc => 'def'}, :pretty => true) # encoded in a pretty form (if supported by the coder)
17
+
18
+ The `use` method, which sets the MultiJson adapter, takes either a symbol or a
19
+ class (to allow for custom JSON parsers) that responds to both `.load` and `.dump`
20
+ at the class level.
21
+
22
+ MultiJSON tries to have intelligent defaulting. That is, if you have any of the
23
+ supported engines already loaded, it will utilize them before attempting to
24
+ load any. When loading, libraries are ordered by speed. First Oj, then Yajl,
25
+ then the JSON gem, then JSON pure. If no other JSON library is available,
26
+ MultiJSON falls back to [OkJson][], a simple, vendorable JSON parser.
27
+
28
+ [okjson]: https://github.com/kr/okjson
29
+
30
+ ## Supported JSON Engines
31
+
32
+ * [Oj](https://github.com/ohler55/oj) Optimized JSON by Peter Ohler
33
+ * [Yajl](https://github.com/brianmario/yajl-ruby) Yet Another JSON Library by Brian Lopez
34
+ * [JSON](https://github.com/flori/json) The default JSON gem with C-extensions (ships with Ruby 1.9)
35
+ * [JSON Pure](https://github.com/flori/json) A Ruby variant of the JSON gem
36
+ * [NSJSONSerialization](https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html) Wrapper for Apple's NSJSONSerialization in the Cocoa Framework (MacRuby only)
37
+ * [OkJson][okjson] A simple, vendorable JSON parser
38
+
39
+ ## Contributing
40
+ In the spirit of [free software][free-sw], **everyone** is encouraged to help
41
+ improve this project.
42
+
43
+ [free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
44
+
45
+ Here are some ways *you* can contribute:
46
+
47
+ * by using alpha, beta, and prerelease versions
48
+ * by reporting bugs
49
+ * by suggesting new features
50
+ * by writing or editing documentation
51
+ * by writing specifications
52
+ * by writing code (**no patch is too small**: fix typos, add comments, clean up
53
+ inconsistent whitespace)
54
+ * by refactoring code
55
+ * by closing [issues][]
56
+ * by reviewing patches
57
+
58
+ [issues]: https://github.com/intridea/multi_json/issues
59
+
60
+ ## Submitting an Issue
61
+ We use the [GitHub issue tracker][issues] to track bugs and features. Before
62
+ submitting a bug report or feature request, check to make sure it hasn't
63
+ already been submitted. When submitting a bug report, please include a [Gist][]
64
+ that includes a stack trace and any details that may be necessary to reproduce
65
+ the bug, including your gem version, Ruby version, and operating system.
66
+ Ideally, a bug report should include a pull request with failing specs.
67
+
68
+ [gist]: https://gist.github.com/
69
+
70
+ ## Submitting a Pull Request
71
+ 1. [Fork the repository.][fork]
72
+ 2. [Create a topic branch.][branch]
73
+ 3. Add specs for your unimplemented feature or bug fix.
74
+ 4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
75
+ 5. Implement your feature or bug fix.
76
+ 6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
77
+ 7. Run `open coverage/index.html`. If your changes are not completely covered
78
+ by your tests, return to step 3.
79
+ 8. Add, commit, and push your changes.
80
+ 9. [Submit a pull request.][pr]
81
+
82
+ [fork]: http://help.github.com/fork-a-repo/
83
+ [branch]: http://learn.github.com/p/branching.html
84
+ [pr]: http://help.github.com/send-pull-requests/
85
+
86
+ ## Supported Ruby Versions
87
+ This library aims to support and is [tested against][travis] the following Ruby
88
+ implementations:
89
+
90
+ * Ruby 1.8.7
91
+ * Ruby 1.9.2
92
+ * Ruby 1.9.3
93
+ * [JRuby][]
94
+ * [Rubinius][]
95
+ * [MacRuby][] (not tested on Travis CI)
96
+
97
+ [jruby]: http://www.jruby.org/
98
+ [rubinius]: http://rubini.us/
99
+ [macruby]: http://www.macruby.org/
100
+
101
+ If something doesn't work on one of these interpreters, it should be considered
102
+ a bug.
103
+
104
+ This library may inadvertently work (or seem to work) on other Ruby
105
+ implementations, however support will only be provided for the versions listed
106
+ above.
107
+
108
+ If you would like this library to support another Ruby version, you may
109
+ volunteer to be a maintainer. Being a maintainer entails making sure all tests
110
+ run and pass on that implementation. When something breaks on your
111
+ implementation, you will be personally responsible for providing patches in a
112
+ timely fashion. If critical issues for a particular implementation exist at the
113
+ time of a major release, support for that Ruby version may be dropped.
114
+
115
+ ## Copyright
116
+ Copyright (c) 2010 Michael Bleigh, Josh Kalderimis, Erik Michaels-Ober, and Intridea, Inc.
117
+ See [LICENSE][] for details.
118
+
119
+ [license]: https://github.com/intridea/multi_json/blob/master/LICENSE.md
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ desc "Run all examples"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+ task :test => :spec
10
+
11
+ namespace :doc do
12
+ require 'rdoc/task'
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
data/lib/multi_json.rb ADDED
@@ -0,0 +1,120 @@
1
+ module MultiJson
2
+ class DecodeError < StandardError
3
+ attr_reader :data
4
+ def initialize(message="", backtrace=[], data="")
5
+ super(message)
6
+ self.set_backtrace(backtrace)
7
+ @data = data
8
+ end
9
+ end
10
+
11
+ @adapter = nil
12
+
13
+ REQUIREMENT_MAP = [
14
+ ["oj", :oj],
15
+ ["yajl", :yajl],
16
+ ["json", :json_gem],
17
+ ["json/pure", :json_pure]
18
+ ]
19
+
20
+ class << self
21
+
22
+ # The default adapter based on what you currently
23
+ # have loaded and installed. First checks to see
24
+ # if any adapters are already loaded, then checks
25
+ # to see which are installed if none are loaded.
26
+ def default_adapter
27
+ return :oj if defined?(::Oj)
28
+ return :yajl if defined?(::Yajl)
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
39
+
40
+ Kernel.warn "[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance."
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
77
+ nil
78
+ when Class
79
+ new_adapter
80
+ else
81
+ raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
82
+ end
83
+ end
84
+
85
+ # Decode a JSON string into Ruby.
86
+ #
87
+ # <b>Options</b>
88
+ #
89
+ # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
90
+ # <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
91
+ def load(string, options={})
92
+ adapter = current_adapter(options)
93
+ begin
94
+ adapter.load(string, options)
95
+ rescue adapter::ParseError => exception
96
+ raise DecodeError.new(exception.message, exception.backtrace, string)
97
+ end
98
+ end
99
+ # :nodoc:
100
+ alias :decode :load
101
+
102
+ def current_adapter(options)
103
+ if new_adapter = (options || {}).delete(:adapter)
104
+ load_adapter(new_adapter)
105
+ else
106
+ adapter
107
+ end
108
+ end
109
+
110
+ # Encodes a Ruby object as JSON.
111
+ def dump(object, options={})
112
+ adapter = current_adapter(options)
113
+ adapter.dump(object, options)
114
+ end
115
+ # :nodoc:
116
+ alias :encode :dump
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,25 @@
1
+ module MultiJson
2
+ module Adapters
3
+ module JsonCommon
4
+
5
+ def load(string, options={})
6
+ string = string.read if string.respond_to?(:read)
7
+ ::JSON.parse(string, :symbolize_names => options[:symbolize_keys])
8
+ end
9
+
10
+ def dump(object, options={})
11
+ object.to_json(process_options(options))
12
+ end
13
+
14
+ protected
15
+
16
+ def process_options(options={})
17
+ return options if options.empty?
18
+ opts = {}
19
+ opts.merge!(JSON::PRETTY_STATE_PROTOTYPE.to_h) if options.delete(:pretty)
20
+ opts.merge!(options)
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ require 'json' unless defined?(::JSON)
2
+ require 'multi_json/adapters/json_common'
3
+
4
+ module MultiJson
5
+ module Adapters
6
+ # Use the JSON gem to dump/load.
7
+ class JsonGem
8
+ ParseError = ::JSON::ParserError
9
+ extend JsonCommon
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'json/pure' unless defined?(::JSON)
2
+ require 'multi_json/adapters/json_common'
3
+
4
+ module MultiJson
5
+ module Adapters
6
+ # Use JSON pure to dump/load.
7
+ class JsonPure
8
+ ParseError = ::JSON::ParserError
9
+ extend JsonCommon
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ framework 'Foundation'
2
+ require 'multi_json/adapters/ok_json'
3
+
4
+ module MultiJson
5
+ module Adapters
6
+ class Nsjsonserialization < MultiJson::Adapters::OkJson
7
+ ParseError = ::MultiJson::OkJson::Error
8
+
9
+ def self.load(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.dump(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
@@ -0,0 +1,22 @@
1
+ require 'oj' unless defined?(::Oj)
2
+
3
+ module MultiJson
4
+ module Adapters
5
+ # Use the Oj library to dump/load.
6
+ class Oj
7
+ ParseError = SyntaxError
8
+
9
+ ::Oj.default_options = {:mode => :compat}
10
+
11
+ def self.load(string, options={}) #:nodoc:
12
+ options.merge!(:symbol_keys => options[:symbolize_keys] || options['symbolize_keys'])
13
+ ::Oj.load(string, options)
14
+ end
15
+
16
+ def self.dump(object, options={}) #:nodoc:
17
+ options.merge!(:indent => 2) if options[:pretty] || options['pretty']
18
+ ::Oj.dump(object, options)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ require 'multi_json/vendor/okjson'
2
+
3
+ module MultiJson
4
+ module Adapters
5
+ class OkJson
6
+ ParseError = ::MultiJson::OkJson::Error
7
+
8
+ def self.load(string, options={}) #:nodoc:
9
+ string = string.read if string.respond_to?(:read)
10
+ result = ::MultiJson::OkJson.decode(string)
11
+ options[:symbolize_keys] ? symbolize_keys(result) : result
12
+ end
13
+
14
+ def self.dump(object, options={}) #:nodoc:
15
+ ::MultiJson::OkJson.valenc(stringify_keys(object))
16
+ end
17
+
18
+ def self.symbolize_keys(object) #:nodoc:
19
+ modify_keys(object) do |key|
20
+ key.is_a?(String) ? key.to_sym : key
21
+ end
22
+ end
23
+
24
+ def self.stringify_keys(object) #:nodoc:
25
+ modify_keys(object) do |key|
26
+ key.is_a?(Symbol) ? key.to_s : key
27
+ end
28
+ end
29
+
30
+ def self.modify_keys(object, &modifier) #:nodoc:
31
+ case object
32
+ when Array
33
+ object.map do |value|
34
+ modify_keys(value, &modifier)
35
+ end
36
+ when Hash
37
+ object.inject({}) do |result, (key, value)|
38
+ new_key = modifier.call(key)
39
+ new_value = modify_keys(value, &modifier)
40
+ result.merge! new_key => new_value
41
+ end
42
+ else
43
+ object
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end