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 Attribute Summary

Attributes included from Hanuman::StageInstanceMethods

#graph

Instance Method Summary collapse

Methods inherited from Serializer

#handle_error

Methods inherited from Wukong::Processor

configure, description, #finalize, #perform_action, #receive_action, #setup, #stop

Methods included from Logging

included

Methods inherited from Hanuman::Stage

#clone

Methods included from Hanuman::StageClassMethods

#builder, #label, #register, #set_builder

Methods included from Hanuman::StageInstanceMethods

#add_link, #linkable_name, #root

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:



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

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