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.



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

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.



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

def encoding
  ENCODING
end

#from_payload(payload, hint: nil) ⇒ 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:

  • payload (Api::Common::V1::Payload)

    Payload to convert.

  • hint (Object, nil) (defaults to: nil)

    Hint, if any, to assist conversion.

Returns:

  • (Object)

    Converted Ruby value.



48
49
50
51
52
53
54
55
56
57
# File 'lib/temporalio/converters/payload_converter/json_plain.rb', line 48

def from_payload(payload, hint: nil) # rubocop:disable Lint/UnusedMethodArgument
  # See comment in to_payload about why we have to do something different in workflow
  if Temporalio::Workflow.in_workflow?
    Temporalio::Workflow::Unsafe.durable_scheduler_disabled do
      JSON.parse(payload.data, @parse_options)
    end
  else
    JSON.parse(payload.data, @parse_options)
  end
end

#to_payload(value, hint: nil) ⇒ 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.

  • hint (Object, nil) (defaults to: nil)

    Hint, if any, to assist conversion.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/temporalio/converters/payload_converter/json_plain.rb', line 31

def to_payload(value, hint: nil) # rubocop:disable Lint/UnusedMethodArgument
  # For generate and parse, if we are in a workflow, we need to do this outside of the durable scheduler since
  # some things like the recent https://github.com/ruby/json/pull/832 may make illegal File.expand_path calls.
  # And other future things may be slightly illegal in JSON generate/parse and we don't want to break everyone
  # when it happens.
  data = if Temporalio::Workflow.in_workflow?
           Temporalio::Workflow::Unsafe.durable_scheduler_disabled do
             JSON.generate(value, @generate_options).b
           end
         else
           JSON.generate(value, @generate_options).b
         end

  Api::Common::V1::Payload.new(metadata: { 'encoding' => ENCODING }, data:)
end