Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
Decode a JSON string into Ruby.
Options
:symbolize_keys
If true, will use symbols instead of strings for the keys.
# File lib/multi_json.rb, line 80 def decode(string, options = {}) engine.decode(string, options) rescue engine::ParseError => exception raise DecodeError.new(exception.message, exception.backtrace, string) end
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.
# 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
Encodes a Ruby object as JSON.
# File lib/multi_json.rb, line 87 def encode(object, options = {}) engine.encode(object, options) end
Get the current engine class.
# File lib/multi_json.rb, line 16 def engine return @engine if @engine self.engine = self.default_engine @engine end
Set the JSON parser utilizing a symbol, string, or class. Supported by default are:
:json_gem
:json_pure
:ok_json
:yajl
:nsjsonserialization
(MacRuby only)
# 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