Module: ActiveSupport::JSON

Defined in:
lib/active_support/json/decoding.rb,
lib/active_support/json/encoding.rb,
lib/active_support/json/variable.rb,
lib/active_support/json/backends/yajl.rb,
lib/active_support/json/backends/yaml.rb,
lib/active_support/json/backends/jsongem.rb

Defined Under Namespace

Modules: Backends, Encoding Classes: Variable

Constant Summary collapse

DECODERS =

Listed in order of preference.

%w(Yajl Yaml)
DATE_REGEX =

matches YAML-formatted dates

/^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
CircularReferenceError =
Deprecation::DeprecatedConstantProxy.new('ActiveSupport::JSON::CircularReferenceError', Encoding::CircularReferenceError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.parse_errorObject (readonly)

Returns the value of attribute parse_error.



13
14
15
# File 'lib/active_support/json/decoding.rb', line 13

def parse_error
  @parse_error
end

Class Method Details

.backendObject



16
17
18
19
# File 'lib/active_support/json/decoding.rb', line 16

def backend
  set_default_backend unless defined?(@backend)
  @backend
end

.backend=(name) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/active_support/json/decoding.rb', line 21

def backend=(name)
  if name.is_a?(Module)
    @backend = name
  else
    require "active_support/json/backends/#{name.to_s.downcase}"
    @backend = ActiveSupport::JSON::Backends::const_get(name)
  end
  @parse_error = @backend::ParseError
end

.encode(value, options = nil) ⇒ Object

Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.



29
30
31
# File 'lib/active_support/json/encoding.rb', line 29

def self.encode(value, options = nil)
  Encoding::Encoder.new(options).encode(value)
end

.set_default_backendObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_support/json/decoding.rb', line 38

def set_default_backend
  DECODERS.find do |name|
    begin
      self.backend = name
      true
    rescue LoadError
      # Try next decoder.
      false
    end
  end
end

.with_backend(name) ⇒ Object



31
32
33
34
35
36
# File 'lib/active_support/json/decoding.rb', line 31

def with_backend(name)
  old_backend, self.backend = backend, name
  yield
ensure
  self.backend = old_backend
end