module Cucumber::Messages::Message::Serialization

def prepare_value(value, camelize:, reject_nil_values:)

def prepare_value(value, camelize:, reject_nil_values:)
  return value.to_h(camelize: camelize, reject_nil_values: reject_nil_values) if value.is_a?(Cucumber::Messages::Message)
  return value.map { |v| prepare_value(v, camelize: camelize, reject_nil_values: reject_nil_values) } if value.is_a?(Array)
  value
end

def to_h(camelize: false, reject_nil_values: false)

#
Cucumber::Messages::Comment.new(location: location, text: 'comment').to_h # => { location: { line: 2, :column: nil }, text: "comment" }
location = Cucumber::Messages::Location.new(line: 2)

It is recursive so embedded messages are also processed

Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: nil).to_h(reject_nil_values: true) # => { name: 'foo' }
Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: 'abc-def').to_h(camelize: true) # => { name: 'foo', astNodeId: 'abc-def' }
Cucumber::Messages::Duration.new(seconds: 1, nanos: 42).to_h # => { seconds: 1, nanos: 42 }

If +reject_nil_values:+ keyword parameter is set to true, resulting hash won't include nil values
If +camelize:+ keyword parameter is set to true, then keys will be camelized
Returns a new Hash formed from the message attributes
#
def to_h(camelize: false, reject_nil_values: false)
  resulting_hash = self.instance_variables.map do |variable_name|
    h_key = variable_name[1..-1]
    h_key = Cucumber::Messages::Message.camelize(h_key) if camelize
    h_value = prepare_value(
      self.instance_variable_get(variable_name),
      camelize: camelize,
      reject_nil_values: reject_nil_values
    )
    [ h_key.to_sym, h_value ]
  end.to_h
  resulting_hash.reject! { |_, value| value.nil? } if reject_nil_values
  resulting_hash
end

def to_json

#
Cucumber::Messages::Comment.new(location: location, text: 'comment').to_json # => '{"location":{"line":2,"column":null},"text":"comment"}'
location = Cucumber::Messages::Location.new(line: 2)

As #to_h, the method is recursive

Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: nil).to_json # => '{"name":"foo"}'
Cucumber::Messages::PickleTag.new(name: 'foo', ast_node_id: 'abc-def').to_json # => '{"name":"foo","astNodeId":"abc-def"}'
Cucumber::Messages::Duration.new(seconds: 1, nanos: 42).to_json # => '{"seconds":1,"nanos":42}'

Keys are camelized during the process. Null values are not part of the json document.
Generates a JSON document from the message.
#
def to_json
  to_h(camelize: true, reject_nil_values: true).to_json
end