Namespace

Class/Module Index [+]

Quicksearch

Rufus::Json

Constants

ACTIVE
ACTIVE_SUPPORT

The Rails ActiveSupport::JSON decoder

E_REGEX
JSON

The JSON / JSON pure decoder

NONE

The "raise an exception because there's no backend" backend

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 99
def self.backend

  ] yajl json active 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 111
def self.backend= (b)

  if b.is_a?(Symbol)
    b = { :yajl => YAJL, :json => JSON, :active => ACTIVE, :none => NONE }[b]
  end

  @backend = b
end
decode(s) click to toggle source

Decodes the given JSON string.

# File lib/rufus/json.rb, line 143
def self.decode (s)

  @backend[1].call(s)

rescue @backend[2].call => 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 164
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 74
def self.detect_backend

  @backend = if defined?(::Yajl)
    YAJL
  elsif defined?(::JSON)
    JSON
  elsif defined?(ActiveSupport::JSON)
    ACTIVE_SUPPORT
  else
    NONE
  end
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 155
def self.dup (o)

  (@backend == NONE) ? Marshal.load(Marshal.dump(o)) : decode(encode(o))
end
encode(o, opts={}) click to toggle source

Encodes the given object to a JSON string.

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

  @backend[0].call(o, opts)
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 92
def self.has_backend?

  (@backend != NONE)
end
pretty_encode(o) click to toggle source

Pretty encoding

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

  case @backend
    when JSON
      encode(o, :indent => '  ', :object_nl => "\n", :array_nl => "\n", :space => ' ')
    when YAJL
      encode(o, :pretty => true, :indent => '  ')
    else
      encode(o)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.