Class: AWS::Core::Options::JSONSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/options/json_serializer.rb

Overview

Given a hash of serialization rules, a JSONSerializer can convert a hash of request options into a JSON document. The request options are validated before returning JSON.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, payload_param) ⇒ JSONSerializer

Returns a new instance of JSONSerializer.

Parameters:

  • rules (Hash)

    A hash of option rules to validate against.

  • payload_param (String, nil)


28
29
30
31
# File 'lib/aws/core/options/json_serializer.rb', line 28

def initialize rules, payload_param
  @payload_param = payload_param
  @rules = @payload_param ? rules[@payload_param][:members] : rules
end

Instance Attribute Details

#namespaceString (readonly)

Returns:

  • (String)


37
38
39
# File 'lib/aws/core/options/json_serializer.rb', line 37

def namespace
  @namespace
end

#operation_nameString (readonly)

Returns the name of the API operation.

Returns:

  • (String)

    Returns the name of the API operation.



34
35
36
# File 'lib/aws/core/options/json_serializer.rb', line 34

def operation_name
  @operation_name
end

#rulesHash (readonly)

Returns:

  • (Hash)


40
41
42
# File 'lib/aws/core/options/json_serializer.rb', line 40

def rules
  @rules
end

Instance Method Details

#serialize!(request_options) ⇒ String

Returns an string of the request parameters serialized into XML.

Parameters:

  • request_options (Hash)

    A hash of already validated request options with normalized values.

Returns:

  • (String)

    Returns an string of the request parameters serialized into XML.



47
48
49
50
51
52
53
54
# File 'lib/aws/core/options/json_serializer.rb', line 47

def serialize request_options
  request_options = request_options[@payload_param] if @payload_param
  data = normalize_keys(request_options, rules)
  if rules.any?{|k,v| v[:location] == 'body' }
    data = data.values.first
  end
  JSON.pretty_generate(data)
end