Module: Webhookdb::Json

Defined in:
lib/webhookdb/json.rb

Overview

Helpers for JSON encoding and (to a lesser degree) decoding. In general, decoding JSON in Ruby is simple, as long as we don’t try and deal with automatically converting types (which we don’t).

But encoding JSON is complex, like for example encoding a Time. ActiveSupport uses #as_json to convert Ruby types to JSON-native types. In order to use this, we must call Webhookdb::Json.encode, or go through ActiveSupport (which we patch here).

That is, using JSON.dump(Time.now) would give you ‘“1970-01-01 02:00:00 0200”’ but using Webhookdb::Json.encode(Time.now) gives you ‘“1970-01-01T02:00:00.12302:00”’

Anyway, this is all largely under the hood and handled for us using ActiveSupport, but while we switched Yajl to Oj, we went ahead and added this shim to bypass the worst part of ActiveSupport (its still very tied to Rails needs).

Defined Under Namespace

Classes: Encoder

Constant Summary collapse

DEFAULT_OPTIONS =
{}.freeze
PRETTY_OPTIONS =
{indent: "  ", space: " ", object_nl: "\n", array_nl: "\n"}.freeze

Class Method Summary collapse

Class Method Details

.encode(value, options = nil) ⇒ Object

Dump as compact, standard JSON, using iso8601 for times.



88
89
90
# File 'lib/webhookdb/json.rb', line 88

def encode(value, options=nil)
  Encoder.new(options).encode(value)
end

.pretty_generate(value) ⇒ Object

Dump as pretty JSON, similar to JSON.pretty_generate but with iso8601 times.



93
94
95
# File 'lib/webhookdb/json.rb', line 93

def pretty_generate(value)
  Webhookdb::Json.encode(value, PRETTY_OPTIONS)
end