Module: RSpec::EnrichedJson::ExpectationHelperWrapper::Serializer

Extended by:
Serializer
Included in:
Serializer
Defined in:
lib/rspec/enriched_json/expectation_helper_wrapper.rb

Constant Summary collapse

OJ_OPTIONS =
{
  mode: :object,
  circular: true,
  class_cache: false,
  create_additions: false,
  symbol_keys: false,
  auto_define: false,
  create_id: nil,
  use_to_json: false,
  use_as_json: false,
  use_raw_json: false,
  bigdecimal_as_decimal: true,
  nan: :word
}

Instance Method Summary collapse

Instance Method Details

#serialize_value(value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec/enriched_json/expectation_helper_wrapper.rb', line 46

def serialize_value(value)
  if value.is_a?(Regexp)
    return Oj.dump(value.inspect, mode: :compat)
  elsif value.is_a?(Proc)
    original_stdout = $stdout
    $stdout = StringIO.new

    value.call

    output = $stdout.string
    $stdout = original_stdout

    return output
  end

  Oj.dump(value, OJ_OPTIONS)
rescue => e
  Oj.dump({
    "_serialization_error" => e.message,
    "_class" => value.class.name,
    "_to_s" => begin
      value.to_s
    rescue
      "[to_s failed]"
    end
  }, mode: :compat)
end