Method: Webhookdb::Json::Encoder#jsonify

Defined in:
lib/webhookdb/json.rb

#jsonify(value) ⇒ Object

Convert an object into a “JSON-ready” representation composed of primitives like Hash, Array, String, Numeric, and true/false/nil. Recursively calls #as_json to the object to recursively build a fully JSON-ready object.

This allows developers to implement #as_json without having to worry about what base types of objects they are allowed to return or having to remember to call #as_json recursively.

Note: the options hash passed to object.to_json is only passed to object.as_json, not any of this method’s recursive #as_json calls.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/webhookdb/json.rb', line 54

def jsonify(value)
  case value
    when Rational
      value.to_s
    when String, Numeric, NilClass, TrueClass, FalseClass
      value.as_json
    when Hash
      result = {}
      value.each do |k, v|
        result[jsonify(k)] = jsonify(v)
      end
      result
    when Array
      value.map { |v| jsonify(v) }
    else
      jsonify value.as_json
  end
end