Module: XToJson
- Defined in:
- lib/x_to_json.rb,
lib/x_to_json/engines/yajl.rb,
lib/x_to_json/engines/json_gem.rb,
lib/x_to_json/engines/json_pure.rb,
lib/x_to_json/engines/active_support.rb
Defined Under Namespace
Modules: Engines
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.
-
.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.
45 46 47 |
# File 'lib/x_to_json.rb', line 45 def decode(string, = {}) engine.decode(string, ) end |
.default_engine ⇒ Object
The default engine based on what you currently have loaded. Tries Yajl first, then JSON gem, then ActiveSupport and JSON pure.
14 15 16 17 18 19 |
# File 'lib/x_to_json.rb', line 14 def default_engine return :yajl if defined?(::Yajl) return :json_gem if defined?(::JSON) return :active_support if defined?(::ActiveSupport::JSON) :json_pure end |
.encode(object) ⇒ Object
Encodes a Ruby object as JSON.
50 51 52 |
# File 'lib/x_to_json.rb', line 50 def encode(object) engine.encode(object) end |
.engine ⇒ Object
Get the current engine class.
5 6 7 8 9 |
# File 'lib/x_to_json.rb', line 5 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
-
:active_support
(useful for inside Rails apps) -
:yajl
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/x_to_json.rb', line 28 def engine=(new_engine) case new_engine when String, Symbol require "x_to_json/engines/#{new_engine}" @engine = XToJson::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 |