module Rufus::Json
Constants
- ACTIVE
- ACTIVE_SUPPORT
The Rails ActiveSupport::JSON decoder
- E_REGEX
- JRJACKSON
- JSON
- NONE
The “raise an exception because there’s no backend” backend
- OJ
- VERSION
- YAJL
Public Class Methods
Returns :yajl|:json|:active|:none (an identifier for the current backend)
# File lib/rufus/json.rb, line 179 def self.backend %w[ yajl json active oj jrjackson none ].find { |b| Rufus::Json.const_get(b.upcase) == @backend }.to_sym end
Forces a decoder JSON/ACTIVE_SUPPORT or any lambda pair that knows how to deal with JSON
.
It’s OK to pass a symbol as well, :yajl, :json, :active (or :none).
# File lib/rufus/json.rb, line 191 def self.backend=(b) b = { 'yajl' => YAJL, 'yajl-ruby' => YAJL, 'json' => JSON, 'json-pure' => JSON, 'active' => ACTIVE, 'active-support' => ACTIVE, 'oj' => OJ, 'jrjackson' => JRJACKSON, 'none' => NONE }[b.to_s.gsub(/[_\/]/, '-')] if b.is_a?(String) or b.is_a?(Symbol) @backend = b end
Decodes the given JSON
string.
# File lib/rufus/json.rb, line 227 def self.decode(s) @backend[:decode][s] rescue @backend[:error][] => e raise ParserError.new(e.message) end
Let’s ActiveSupport do the E number notation.
# File lib/rufus/json.rb, line 259 def self.decode_e(s) s.match(E_REGEX) ? eval(s) : false end
[Re-]Attempts to detect a JSON
backend
# File lib/rufus/json.rb, line 149 def self.detect_backend @backend = if defined?(::JrJackson) JRJACKSON elsif defined?(::Oj) OJ elsif defined?(::Yajl) YAJL elsif defined?(::JSON) JSON elsif defined?(ActiveSupport::JSON) ACTIVE_SUPPORT else NONE end end
An alias for .encode
# File lib/rufus/json.rb, line 220 def self.dump(o, opts={}) encode(o, opts) end
Duplicates an object by turning it into JSON
and back.
Don’t laugh, yajl-ruby makes that faster than a Marshal copy.
# File lib/rufus/json.rb, line 246 def self.dup(o) if @backend == NONE syms_to_s(Marshal.load(Marshal.dump(o))) else decode(encode(o)) end end
Encodes the given object to a JSON
string.
# File lib/rufus/json.rb, line 206 def self.encode(o, opts={}) @backend[:encode][o, opts] end
Used to handle parsers that do not support raw value encoding (i.e. JrJackson)
# File lib/rufus/json.rb, line 277 def self.fix_raw_value(o) case o when FalseClass, TrueClass, Fixnum, Float o.to_s when NilClass 'null' else o end end
Returns true if there is a backend set for parsing/encoding JSON
# File lib/rufus/json.rb, line 172 def self.has_backend? (@backend != NONE) end
An alias for .decode
# File lib/rufus/json.rb, line 237 def self.load(s) decode(s) end
In the given order, attempts to load a json lib and sets it as the backend of rufus-json.
Returns the name of lib found if sucessful.
Returns nil if no lib could be set.
The default order / list of backends is yajl, active_support, json, json/pure. When specifying a custom order/list, unspecified backends won’t be tried for.
# File lib/rufus/json.rb, line 130 def self.load_backend(*order) order = %w[ yajl oj jrjackson active_support json json/pure ] if order.empty? order.each do |lib| begin require(lib) Rufus::Json.backend = lib return lib rescue LoadError => le end end nil end
Pretty encoding
# File lib/rufus/json.rb, line 213 def self.pretty_encode(o) @backend[:pretty_encode][o] end
Used to get a uniform behaviour among encoders.
# File lib/rufus/json.rb, line 266 def self.syms_to_s(o) return o.to_s if o.is_a?(Symbol) return o unless o.is_a?(Hash) o.inject({}) { |h, (k, v)| h[k.to_s] = syms_to_s(v); h } end