Module: Kilden::CanonicalJSON Private

Defined in:
lib/kilden/canonical_json.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

The canonical JSON form frozen by the spec (§6.1) so identity tokens are byte-identical across the five SDKs: object keys sorted lexicographically (byte order) at every nesting level, compact separators, UTF-8 preserved, and the three HTML-unsafe ASCII characters escaped the way Go's encoding/json does — the platform's reference generator.

Constant Summary collapse

GO_ESCAPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

&, <, > plus the JS line separators U+2028/U+2029, escaped the way Go's encoding/json does (SPEC §6.1).

{
  "&" => "\\u0026", "<" => "\\u003c", ">" => "\\u003e",
  "\u2028" => "\\u2028", "\u2029" => "\\u2029"
}.freeze

Class Method Summary collapse

Class Method Details

.generate(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kilden/canonical_json.rb', line 22

def generate(value)
  case value
  when Hash
    pairs = value.keys.map(&:to_s).sort.map do |key|
      raw = value.key?(key) ? value[key] : value[key.to_sym]
      "#{string(key)}:#{generate(raw)}"
    end
    "{#{pairs.join(',')}}"
  when Array
    "[#{value.map { |v| generate(v) }.join(',')}]"
  when String
    string(value)
  when Integer, Float, TrueClass, FalseClass
    JSON.generate(value)
  when nil
    "null"
  else
    string(value.to_s)
  end
end

.string(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/kilden/canonical_json.rb', line 43

def string(value)
  JSON.generate(value).gsub(/[&<>\u2028\u2029]/, GO_ESCAPES)
end