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.
@return [String]
@return [String] Returns the name of the API operation.
@return [Hash]
@param [Hash] rules A hash of option rules to validate against. @param [String,nil] payload_param
# 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
@overload serialize!(request_options)
@param [Hash] request_options A hash of already validated request options with normalized values. @return [String] Returns an string of the request parameters serialized into XML.
# 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
# File lib/aws/core/options/json_serializer.rb, line 58 def normalize_keys values, rules values.inject({}) do |h,(k,v)| child_rules = rules[k] child_name = child_rules[:name] || Inflection.class_name(k.to_s) h.merge(child_name => normalize_value(v, child_rules)) end end
# File lib/aws/core/options/json_serializer.rb, line 66 def normalize_value value, rules case rules[:type] when :hash then normalize_keys(value, rules[:members]) when :array then value.map{|v| normalize_value(v, rules[:members]) } when :map value.inject({}) do |h,(k,v)| h.merge(k => normalize_value(v, rules[:members])) end when :blob then Base64.encode64(value.read).strip else value end end