serialize.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Modifications made by Cloudera are:
  4. # Copyright (c) 2016 Cloudera, Inc. All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"). You
  7. # may not use this file except in compliance with the License. A copy of
  8. # the License is located at
  9. #
  10. # http://aws.amazon.com/apache2.0/
  11. #
  12. # or in the "license" file accompanying this file. This file is
  13. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  14. # ANY KIND, either express or implied. See the License for the specific
  15. # language governing permissions and limitations under the License.
  16. try:
  17. from collections import OrderedDict
  18. except ImportError:
  19. from ordereddict import OrderedDict # Python 2.6
  20. import json
  21. class Serializer(object):
  22. DEFAULT_ENCODING = 'utf-8'
  23. def serialize_to_request(self, parameters, operation_model):
  24. # Don't serialize any parameter with a None value.
  25. filtered_parameters = OrderedDict(
  26. (k, v) for k, v in parameters.items() if v is not None)
  27. serialized = {}
  28. # serialized['method'] = operation_model.http['method']
  29. # serialized['headers'] = {'Content-Type': 'application/json'}
  30. # serialized['url_path'] = operation_model.http['requestUri']
  31. serialized_body = OrderedDict()
  32. if len(filtered_parameters) != 0:
  33. self._serialize(serialized_body, filtered_parameters, None)
  34. serialized['body'] = json.dumps(serialized_body).encode(self.DEFAULT_ENCODING)
  35. return serialized
  36. def _serialize(self, serialized, value, shape, key=None):
  37. # serialize_method_name = '_serialize_type_%s' % shape.type_name
  38. # method = getattr(self, serialize_method_name, self._default_serialize)
  39. self._default_serialize(serialized, value, shape, key)
  40. def _serialize_type_object(self, serialized, value, shape, key):
  41. if key is not None:
  42. # If a key is provided, this is a result of a recursive call, so we
  43. # need to add a new child dict as the value of the passed in dict.
  44. # Below we will add all the structure members to the new serialized
  45. # dictionary we just created.
  46. serialized[key] = OrderedDict()
  47. serialized = serialized[key]
  48. for member_key, member_value in value.items():
  49. member_shape = shape.members[member_key]
  50. self._serialize(serialized, member_value, member_shape, member_key)
  51. def _serialize_type_array(self, serialized, value, shape, key):
  52. array_obj = []
  53. serialized[key] = array_obj
  54. for array_item in value:
  55. wrapper = {}
  56. # JSON list serialization is the only case where we aren't setting
  57. # a key on a dict. We handle this by using a __current__ key on a
  58. # wrapper dict to serialize each list item before appending it to
  59. # the serialized list.
  60. self._serialize(wrapper, array_item, shape.member, "__current__")
  61. array_obj.append(wrapper["__current__"])
  62. def _default_serialize(self, serialized, value, shape, key):
  63. if key:
  64. serialized[key] = value
  65. else:
  66. for member_key, member_value in value.items():
  67. serialized[member_key] = member_value