Class: Temporalio::Converters::PayloadConverter::JSONPlain

Inherits:
Encoding
  • Object
show all
Defined in:
lib/temporalio/converters/payload_converter/json_plain.rb

Overview

Encoding for all values for json/plain encoding.

Constant Summary collapse

ENCODING =
'json/plain'

Instance Method Summary collapse

Constructor Details

#initialize(parse_options: { create_additions: true }, generate_options: {}) ⇒ JSONPlain

Create JSONPlain converter.

Parameters:

  • parse_options (Hash) (defaults to: { create_additions: true })

    Options for JSON.parse.

  • generate_options (Hash) (defaults to: {})

    Options for JSON.generate.



18
19
20
21
22
# File 'lib/temporalio/converters/payload_converter/json_plain.rb', line 18

def initialize(parse_options: { create_additions: true }, generate_options: {})
  super()
  @parse_options = parse_options
  @generate_options = generate_options
end

Instance Method Details

#encodingString

Returns Encoding that will be put on the payload metadata if this encoding converter can handle the value.

Returns:

  • (String)

    Encoding that will be put on the payload metadata if this encoding converter can handle the value.



25
26
27
# File 'lib/temporalio/converters/payload_converter/json_plain.rb', line 25

def encoding
  ENCODING
end

#from_payload(payload) ⇒ Object

Convert the payload to a Ruby value. The caller confirms the encoding metadata matches #encoding, so this will error if it cannot convert.

Parameters:

Returns:

  • (Object)

    Converted Ruby value.



38
39
40
# File 'lib/temporalio/converters/payload_converter/json_plain.rb', line 38

def from_payload(payload)
  JSON.parse(payload.data, @parse_options)
end

#to_payload(value) ⇒ Api::Common::V1::Payload?

Convert value to payload if this encoding converter can handle it, or return nil. If the converter can handle it, the resulting payload must have encoding metadata on the payload set to the value of #encoding.

Parameters:

  • value (Object)

    Ruby value to possibly convert.

Returns:



30
31
32
33
34
35
# File 'lib/temporalio/converters/payload_converter/json_plain.rb', line 30

def to_payload(value)
  Api::Common::V1::Payload.new(
    metadata: { 'encoding' => ENCODING },
    data: JSON.generate(value, @generate_options).b
  )
end