Module: Rapidomize::Encoding::Json

Included in:
Payload
Defined in:
lib/rapidomize/encoding/json.rb

Overview

Encode payload to JSON and build payload from JSON

Instance Method Summary collapse

Instance Method Details

#from_json(str) ⇒ Object

Add to Payload from a JSON string This can also be used to add data from JSON strings

Examples:

From a json array, build an enum-like payload

json_string = " '[{"data":1},{"data": 2}]'
payload = Rapidomize::Payload.new.from_json(json_string)

Parameters:

  • str (String)

    A valid JSON string

Returns:

  • self



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rapidomize/encoding/json.rb', line 21

def from_json(str)
  json = JSON.parse(str)
  if json.is_a? Array
    json.each do |j|
      self << Rapidomize::Payload.new.from_hash(j)
    end
  else
    from_hash(json)
  end
  self
end

#to_json(*args) ⇒ String

Encode the payload object in JSON

Parameters:

  • args

    same as Ruby JSON.generate

Returns:

  • (String)

    a JSON string



10
11
12
# File 'lib/rapidomize/encoding/json.rb', line 10

def to_json(*args)
  data.to_json(*args)
end