Skip to Content Skip to Search

class ActiveSupport::EventReporter::Encoders::JSON

JSON encoder for serializing events to JSON format.

event = { name: "user_created", payload: { id: 123 }, tags: { api: true } }
ActiveSupport::EventReporter::Encoders::JSON.encode(event)
# => {
#      "name": "user_created",
#      "payload": {
#        "id": 123
#      },
#      "tags": {
#        "api": true
#      },
#      "context": {}
#    }

Schematized events and tags MUST respond to to_h to be serialized.

event = { name: "UserCreatedEvent", payload: #<UserCreatedEvent:0x111>, tags: { "GraphqlTag": #<GraphqlTag:0x111> } }
ActiveSupport::EventReporter::Encoders::JSON.encode(event)
# => {
#      "name": "UserCreatedEvent",
#      "payload": {
#        "id": 123
#      },
#      "tags": {
#        "GraphqlTag": {
#          "operation_name": "user_created",
#          "operation_type": "mutation"
#        }
#      },
#      "context": {}
#    }

Inherits From

Public class methods

Source code GitHub
# File activesupport/lib/active_support/event_reporter/encoders.rb, line 66
def self.encode(event)
  event[:payload] = event[:payload].to_h
  event[:tags] = event[:tags].transform_values do |value|
    value.respond_to?(:to_h) ? value.to_h : value
  end
  ::JSON.dump(event)
end

Definition files