Class: ActiveSupport::JsonWithMarshalFallback

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/json_with_marshal_fallback.rb

Constant Summary collapse

MARSHAL_SIGNATURE =
"\x04\x08"

Class Method Summary collapse

Class Method Details

.dump(value) ⇒ Object


19
20
21
22
23
24
25
# File 'activesupport/lib/active_support/json_with_marshal_fallback.rb', line 19

def dump(value)
  if self.use_marshal_serialization
    Marshal.dump(value)
  else
    JSON.encode(value)
  end
end

.load(value) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'activesupport/lib/active_support/json_with_marshal_fallback.rb', line 27

def load(value)
  if self.fallback_to_marshal_deserialization
    if value.start_with?(MARSHAL_SIGNATURE)
      logger.warn("JsonWithMarshalFallback: Marshal load fallback occurred.") if logger
      Marshal.load(value)
    else
      JSON.decode(value)
    end
  else
    raise ::JSON::ParserError if value.start_with?(MARSHAL_SIGNATURE)
    JSON.decode(value)
  end
end

.loggerObject


11
12
13
14
15
16
17
# File 'activesupport/lib/active_support/json_with_marshal_fallback.rb', line 11

def logger
  if defined?(Rails) && Rails.respond_to?(:logger)
    Rails.logger
  else
    nil
  end
end