module Rufus::Json

Constants

ACTIVE
ACTIVE_SUPPORT

The Rails ActiveSupport::JSON decoder

E_REGEX
JRJACKSON

github.com/guyboertje/jrjackson

JSON

The JSON / JSON pure decoder

NONE

The “raise an exception because there’s no backend” backend

OJ

github.com/ohler55/oj

VERSION
YAJL

github.com/brianmario/yajl-ruby/

Public Class Methods

backend() click to toggle source

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
backend=(b) click to toggle source

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
decode(s) click to toggle source

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
decode_e(s) click to toggle source

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
detect_backend() click to toggle source

[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
dump(o, opts={}) click to toggle source

An alias for .encode

# File lib/rufus/json.rb, line 220
def self.dump(o, opts={})

  encode(o, opts)
end
dup(o) click to toggle source

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
encode(o, opts={}) click to toggle source

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
fix_raw_value(o) click to toggle source

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
has_backend?() click to toggle source

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
load(s) click to toggle source

An alias for .decode

# File lib/rufus/json.rb, line 237
def self.load(s)

  decode(s)
end
load_backend(*order) click to toggle source

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_encode(o) click to toggle source

Pretty encoding

# File lib/rufus/json.rb, line 213
def self.pretty_encode(o)

  @backend[:pretty_encode][o]
end
syms_to_s(o) click to toggle source

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