Class: Wukong::Processor::ToJson

Inherits:
Serializer show all
Defined in:
lib/wukong/widget/serializers.rb

Overview

A widget for serializing inputs to JSON.

Examples:

Serializing to JSON at the end of a data flow


Wukong.dataflow(:emits_json) do
  ... | to_json
end

See Also:

Constant Summary

Constants inherited from Wukong::Processor

SerializerError

Instance Method Summary collapse

Methods inherited from Serializer

#handle_error

Methods inherited from Wukong::Processor

configure, consumes, description, #expected_record_type, #expected_serialization, #finalize, #perform_action, produces, #receive_action, #setup, #stop, valid_serializer?, validate_and_set_serialization

Methods included from Logging

included

Methods included from Hanuman::StageClassMethods

#builder, #label, #register, #set_builder

Instance Method Details

#process(record) {|json| ... } ⇒ Object

Yields the input record serialized as JSON.

Parameters:

  • record (Object)

Yields:

  • (json)

    the serialized json output

Yield Parameters:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wukong/widget/serializers.rb', line 42

def process(record)
  raise SerializerError.new("Cannot serialize: <nil>") if record.nil?
  if record.respond_to?(:to_json) && !record.is_a?(Hash) # We only want to invoke to_json if it has been explicitly defined
    json = record.to_json(pretty: pretty)
  else
    json = MultiJson.dump(record.try(:to_wire) || record, pretty: pretty)
  end
  yield json
rescue => e
  handle_error(record, e)
end