Module: AmazingPrint::JSONHelper

Included in:
Formatters::ArrayFormatter, Formatters::HashFormatter
Defined in:
lib/amazing_print/json_helper.rb

Constant Summary collapse

VALUE_CLASSES_NOT_TO_CONVERT =
%w[Array BigDecimal Float Hash Integer String].freeze

Instance Method Summary collapse

Instance Method Details

#json_awesome(object, is_key: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/amazing_print/json_helper.rb', line 10

def json_awesome(object, is_key: false)
  return inspector.awesome(object) unless options[:hash_format] == :json && object.respond_to?(:to_json)

  if object.nil?
    # Color null like we do nil
    colorize(object.to_json, :nilclass)
  elsif is_key && object.is_a?(Numeric)
    # JSON keys should be a string
    inspector.awesome(object.to_s)
  elsif VALUE_CLASSES_NOT_TO_CONVERT.include?(object.class.name) || !object.respond_to?(:to_json)
    # These objects should not be converted to strings with #to_json so we can treat them normally
    inspector.awesome(object)
  else
    # Remove surrounding quotes added by #to_json from the standard library since
    # inspector.awesome is going to add those for us for strings.
    inspector.awesome(object.to_json.gsub(/\A"/, '').gsub(/"\z/, ''))
  end
end