Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
Get the current adapter class.
# File lib/multi_json.rb, line 47 47: def adapter 48: return @adapter if @adapter 49: self.use self.default_adapter 50: @adapter 51: end
# File lib/multi_json.rb, line 100 100: def current_adapter(options) 101: if new_adapter = (options || {}).delete(:adapter) 102: load_adapter(new_adapter) 103: else 104: adapter 105: end 106: end
The default adapter based on what you currently have loaded and installed. First checks to see if any adapters are already loaded, then checks to see which are installed if none are loaded.
# File lib/multi_json.rb, line 26 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
Encodes a Ruby object as JSON.
# File lib/multi_json.rb, line 109 109: def dump(object, options={}) 110: adapter = current_adapter(options) 111: adapter.dump(object, options) 112: end
Decode a JSON string into Ruby.
Options
:symbolize_keys | If true, will use symbols instead of strings for the keys. |
:adapter | If set, the selected engine will be used just for the call. |
# File lib/multi_json.rb, line 91 91: def load(string, options={}) 92: adapter = current_adapter(options) 93: adapter.load(string, options) 94: rescue adapter::ParseError => exception 95: raise DecodeError.new(exception.message, exception.backtrace, string) 96: end
# File lib/multi_json.rb, line 71 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
Set the JSON parser utilizing a symbol, string, or class. Supported by default are:
:oj
:json_gem
:json_pure
:ok_json
:yajl
:nsjsonserialization (MacRuby only)
# File lib/multi_json.rb, line 64 64: def use(new_adapter) 65: @adapter = load_adapter(new_adapter) 66: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.