Class: CloudEvents::JsonFormat
- Inherits:
-
Object
- Object
- CloudEvents::JsonFormat
- 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
-
#decode_data(spec_version: nil, content: nil, content_type: nil, **_other_kwargs) ⇒ Hash?
Decode an event data object from a JSON formatted string.
-
#decode_event(content: nil, content_type: nil, **_other_kwargs) ⇒ Hash?
Decode an event or batch from the given input JSON string.
-
#encode_data(spec_version: nil, data: UNSPECIFIED, content_type: nil, sort: false, **_other_kwargs) ⇒ Hash?
Encode an event data object to a JSON formatted string.
-
#encode_event(event: nil, event_batch: nil, sort: false, **_other_kwargs) ⇒ Hash?
Encode an event or batch to a JSON string.
Instance Method Details
#decode_data(spec_version: nil, content: nil, content_type: nil, **_other_kwargs) ⇒ Hash?
Decode an event data object from a JSON formatted string. See Format#decode_data for a general description.
Expects :spec_version
, :content
and :content_type
arguments, and
will decline the request unless all three are provided.
If decoding succeeded, returns a hash with the following keys:
:data
(Object) The payload object to set as thedata
attribute.:content_type
(ContentType) The content type to be set as thedatacontenttype
attribute.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/cloud_events/json_format.rb', line 116 def decode_data spec_version: nil, content: nil, content_type: nil, **_other_kwargs return nil unless spec_version return nil unless content return nil unless content_type&.subtype_base == "json" || content_type&.subtype_format == "json" unless spec_version =~ /^0\.3|1(\.|$)/ raise SpecVersionError, "Unrecognized specversion: #{spec_version}" end data = ::JSON.parse content { data: data, content_type: content_type } rescue ::JSON::JSONError raise FormatSyntaxError, "JSON syntax error" end |
#decode_event(content: nil, content_type: nil, **_other_kwargs) ⇒ Hash?
Decode an event or batch from the given input JSON string. See Format#decode_event for a general description.
Expects :content
and :content_type
arguments, and will decline the
request unless both are provided.
If decoding succeeded, returns a hash with one of the following keys:
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cloud_events/json_format.rb', line 39 def decode_event content: nil, content_type: nil, **_other_kwargs return nil unless content && content_type&.media_type == "application" && content_type&.subtype_format == "json" case content_type.subtype_base when "cloudevents" event = decode_hash_structure ::JSON.parse(content), charset_of(content) { event: event } when "cloudevents-batch" charset = charset_of content batch = Array(::JSON.parse(content)).map do |structure| decode_hash_structure structure, charset end { event_batch: batch } end rescue ::JSON::JSONError raise FormatSyntaxError, "JSON syntax error" end |
#encode_data(spec_version: nil, data: UNSPECIFIED, content_type: nil, sort: false, **_other_kwargs) ⇒ Hash?
Encode an event data object to a JSON formatted string. See Format#encode_data for a general description.
Expects :spec_version
, :data
and :content_type
arguments, and will
decline the request unless all three are provided.
The :data
object can be any Ruby object that can be interpreted as
JSON. Most Ruby objects will work, but normally it will be a JSON value
type comprising hashes, arrays, strings, numbers, booleans, or nil.
If decoding succeeded, returns a hash with the following keys:
:content
(String) The serialized form of the data.:content_type
(ContentType) The content type for the output.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cloud_events/json_format.rb', line 151 def encode_data spec_version: nil, data: UNSPECIFIED, content_type: nil, sort: false, **_other_kwargs return nil unless spec_version return nil if data == UNSPECIFIED return nil unless content_type&.subtype_base == "json" || content_type&.subtype_format == "json" unless spec_version =~ /^0\.3|1(\.|$)/ raise SpecVersionError, "Unrecognized specversion: #{spec_version}" end data = sort_keys data if sort content = ::JSON.dump data { content: content, content_type: content_type } end |
#encode_event(event: nil, event_batch: nil, sort: false, **_other_kwargs) ⇒ Hash?
Encode an event or batch to a JSON string. This formatter should be able to handle any event. See Format#decode_event for a general description.
Expects either the :event
or the :event_batch
argument, but not
both, and will decline the request unless exactly one is provided.
If encoding succeeded, returns a hash with the following keys:
:content
(String) The serialized form of the event or batch.:content_type
(ContentType) The content type for the output.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cloud_events/json_format.rb', line 76 def encode_event event: nil, event_batch: nil, sort: false, **_other_kwargs if event && !event_batch structure = encode_hash_structure event structure = sort_keys structure if sort subtype = "cloudevents" elsif event_batch && !event structure = event_batch.map do |elem| structure_elem = encode_hash_structure elem sort ? sort_keys(structure_elem) : structure_elem end subtype = "cloudevents-batch" else return nil end content = ::JSON.dump structure content_type = ContentType.new "application/#{subtype}+json; charset=#{charset_of content}" { content: content, content_type: content_type } end |