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

HTML_UNSAFE =

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.

{ "&" => "\\u0026", "<" => "\\u003c", ">" => "\\u003e" }.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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kilden/canonical_json.rb', line 17

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.



38
39
40
# File 'lib/kilden/canonical_json.rb', line 38

def string(value)
  JSON.generate(value).gsub(/[&<>]/, HTML_UNSAFE)
end