Class: CloudEvents::JsonFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_events/json_format.rb

Overview

An implementation of JSON format and JSON batch format.

Supports the CloudEvents 0.3 and CloudEvents 1.0 variants of this format. See https://github.com/cloudevents/spec/blob/v0.3/json-format.md and https://github.com/cloudevents/spec/blob/v1.0/json-format.md.

Instance Method Summary collapse

Instance Method Details

#decode(json, **_other_kwargs) ⇒ CloudEvents::Event

Decode an event from the given input JSON string.

Parameters:

  • json (String)

    A JSON-formatted string

Returns:



21
22
23
24
# File 'lib/cloud_events/json_format.rb', line 21

def decode json, **_other_kwargs
  structure = ::JSON.parse json
  decode_hash_structure structure
end

#decode_batch(json, **_other_kwargs) ⇒ Array<CloudEvents::Event>

Decode a batch of events from the given input string.

Parameters:

  • json (String)

    A JSON-formatted string

Returns:



45
46
47
48
49
50
# File 'lib/cloud_events/json_format.rb', line 45

def decode_batch json, **_other_kwargs
  structure_array = Array(::JSON.parse(json))
  structure_array.map do |structure|
    decode_hash_structure structure
  end
end

#decode_hash_structure(structure) ⇒ CloudEvents::Event

Decode a single event from a hash data structure with keys and types conforming to the JSON envelope.

Parameters:

  • structure (Hash)

    An input hash.

Returns:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloud_events/json_format.rb', line 74

def decode_hash_structure structure
  spec_version = structure["specversion"].to_s
  case spec_version
  when "0.3"
    decode_hash_structure_v0 structure
  when /^1(\.|$)/
    decode_hash_structure_v1 structure
  else
    raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
  end
end

#encode(event, sort: false, **_other_kwargs) ⇒ String

Encode an event to a JSON string.

Parameters:

  • event (CloudEvents::Event)

    An input event.

  • sort (boolean) (defaults to: false)

    Whether to sort keys of the JSON output.

Returns:

  • (String)

    The JSON representation.



33
34
35
36
37
# File 'lib/cloud_events/json_format.rb', line 33

def encode event, sort: false, **_other_kwargs
  structure = encode_hash_structure event
  structure = sort_keys structure if sort
  ::JSON.dump structure
end

#encode_batch(events, sort: false, **_other_kwargs) ⇒ String

Encode a batch of event to a JSON string.

Parameters:

  • events (Array<CloudEvents::Event>)

    An array of input events.

  • sort (boolean) (defaults to: false)

    Whether to sort keys of the JSON output.

Returns:

  • (String)

    The JSON representation.



59
60
61
62
63
64
65
# File 'lib/cloud_events/json_format.rb', line 59

def encode_batch events, sort: false, **_other_kwargs
  structure_array = Array(events).map do |event|
    structure = encode_hash_structure event
    sort ? sort_keys(structure) : structure
  end
  ::JSON.dump structure_array
end

#encode_hash_structure(event) ⇒ String

Encode a single event to a hash data structure with keys and types conforming to the JSON envelope.

Parameters:

Returns:

  • (String)

    The hash structure.



93
94
95
96
97
98
99
100
101
102
# File 'lib/cloud_events/json_format.rb', line 93

def encode_hash_structure event
  case event
  when Event::V0
    encode_hash_structure_v0 event
  when Event::V1
    encode_hash_structure_v1 event
  else
    raise SpecVersionError, "Unrecognized specversion: #{event.spec_version}"
  end
end