Module: MultiJson
- Defined in:
- lib/multi_json/vendor/ok_json.rb,
lib/multi_json.rb,
lib/multi_json/version.rb,
lib/multi_json/engines/oj.rb,
lib/multi_json/engines/yajl.rb,
lib/multi_json/engines/ok_json.rb,
lib/multi_json/engines/json_gem.rb,
lib/multi_json/engines/json_pure.rb,
lib/multi_json/engines/json_common.rb,
lib/multi_json/engines/nsjsonserialization.rb
Overview
Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
Defined Under Namespace
Modules: Engines, OkJson Classes: DecodeError
Constant Summary collapse
- REQUIREMENT_MAP =
[ ["oj", :oj], ["yajl", :yajl], ["json", :json_gem], ["json/pure", :json_pure] ]
- DEFAULT_ENGINE_WARNING =
'Warning: multi_json is using default ok_json engine. Suggested action: require and load an appropriate JSON library.'
- VERSION =
"1.2.0"
Class Method Summary collapse
-
.decode(string, options = {}) ⇒ Object
Decode a JSON string into Ruby.
-
.default_engine ⇒ Object
The default engine based on what you currently have loaded and installed.
-
.encode(object, options = {}) ⇒ Object
Encodes a Ruby object as JSON.
-
.engine ⇒ Object
Get the current engine class.
-
.engine=(new_engine) ⇒ Object
Set the JSON parser utilizing a symbol, string, or class.
Class Method Details
.decode(string, options = {}) ⇒ Object
Decode a JSON string into Ruby.
Options
:symbolize_keys
-
If true, will use symbols instead of strings for the keys.
80 81 82 83 84 |
# File 'lib/multi_json.rb', line 80 def decode(string, = {}) engine.decode(string, ) rescue engine::ParseError => exception raise DecodeError.new(exception., exception.backtrace, string) end |
.default_engine ⇒ Object
The default engine based on what you currently have loaded and installed. First checks to see if any engines are already loaded, then checks to see which are installed if none are loaded.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/multi_json.rb', line 35 def default_engine return :oj if defined?(::Oj) return :yajl if defined?(::Yajl) return :json_gem if defined?(::JSON) REQUIREMENT_MAP.each do |(library, engine)| begin require library return engine rescue LoadError next end end Kernel.warn DEFAULT_ENGINE_WARNING :ok_json end |
.encode(object, options = {}) ⇒ Object
Encodes a Ruby object as JSON.
87 88 89 |
# File 'lib/multi_json.rb', line 87 def encode(object, = {}) engine.encode(object, ) end |
.engine ⇒ Object
Get the current engine class.
16 17 18 19 20 |
# File 'lib/multi_json.rb', line 16 def engine return @engine if @engine self.engine = self.default_engine @engine end |
.engine=(new_engine) ⇒ Object
Set the JSON parser utilizing a symbol, string, or class. Supported by default are:
-
:json_gem
-
:json_pure
-
:ok_json
-
:yajl
-
:nsjsonserialization
(MacRuby only)
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/multi_json.rb', line 61 def engine=(new_engine) case new_engine when String, Symbol require "multi_json/engines/#{new_engine}" @engine = MultiJson::Engines.const_get("#{new_engine.to_s.split('_').map{|s| s.capitalize}.join('')}") when NilClass @engine = nil when Class @engine = new_engine else raise "Did not recognize your engine specification. Please specify either a symbol or a class." end end |