Module: ActiveSupport::JSON

Defined in:
lib/active_support/json.rb,
lib/active_support/json/decoding.rb,
lib/active_support/json/encoding.rb,
lib/active_support/json/variable.rb,
lib/active_support/json/encoders/string.rb

Defined Under Namespace

Modules: Encoding Classes: CircularReferenceError, ParseError, Variable

Constant Summary collapse

RESERVED_WORDS =
%w(
  abstract      delete        goto          private       transient
  boolean       do            if            protected     try
  break         double        implements    public        typeof
  byte          else          import        return        var
  case          enum          in            short         void
  catch         export        instanceof    static        volatile
  char          extends       int           super         while
  class         final         interface     switch        with
  const         finally       long          synchronized
  continue      float         native        this
  debugger      for           new           throw
  default       function      package       throws
)
REFERENCE_STACK_VARIABLE =

:nodoc:

:json_reference_stack

Class Method Summary collapse

Class Method Details

.decode(json) ⇒ Object

Converts a JSON string into a Ruby object.



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

def decode(json)
  YAML.load(convert_json_to_yaml(json))
rescue ArgumentError => e
  raise ParseError, "Invalid JSON string"
end

.encode(value, options = {}) ⇒ Object

Converts a Ruby object into a JSON string.



20
21
22
23
24
# File 'lib/active_support/json/encoding.rb', line 20

def encode(value, options = {})
  raise_on_circular_reference(value) do
    value.send(:to_json, options)
  end
end

.reserved_word?(key) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_support/json.rb', line 26

def reserved_word?(key) #:nodoc:
  RESERVED_WORDS.include?(key.to_s)
end

.valid_identifier?(key) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


22
23
24
# File 'lib/active_support/json.rb', line 22

def valid_identifier?(key) #:nodoc:
  key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ && !reserved_word?(key)
end