Module: MultiJson
- Defined in:
- lib/multi_json.rb,
lib/multi_json/version.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
Defined Under Namespace
Modules: Engines Classes: DecodeError
Constant Summary collapse
- REQUIREMENT_MAP =
[ ["yajl", :yajl], ["json", :json_gem], ["json/pure", :json_pure] ]
- VERSION =
"1.0.3"
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) ⇒ 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.
64 65 66 67 68 |
# File 'lib/multi_json.rb', line 64 def decode(string, = {}) engine.decode(string, ) rescue engine::ParseError => exception raise DecodeError, exception., exception.backtrace 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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/multi_json.rb', line 24 def default_engine 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 :ok_json end |
.encode(object) ⇒ Object
Encodes a Ruby object as JSON.
71 72 73 |
# File 'lib/multi_json.rb', line 71 def encode(object) engine.encode(object) end |
.engine ⇒ Object
Get the current engine class.
8 9 10 11 12 |
# File 'lib/multi_json.rb', line 8 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
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/multi_json.rb', line 47 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 Class @engine = new_engine else raise "Did not recognize your engine specification. Please specify either a symbol or a class." end end |