JsonWriter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/string/string.h>
  10. #include <AzCore/std/containers/vector.h>
  11. #include <AzCore/JSON/writer.h>
  12. #include <aws/core/utils/memory/stl/AWSStreamFwd.h>
  13. namespace AWSCore
  14. {
  15. class JsonOutputStream
  16. {
  17. public:
  18. typedef char Ch;
  19. JsonOutputStream(std::ostream& os) : m_os(os)
  20. {
  21. }
  22. Ch Peek() const
  23. {
  24. AZ_Assert(false, "Not Implemented");
  25. return '\0';
  26. }
  27. Ch Take()
  28. {
  29. AZ_Assert(false, "Not Implemented");
  30. return '\0';
  31. }
  32. size_t Tell() const
  33. {
  34. AZ_Assert(false, "Not Implemented");
  35. return 0;
  36. }
  37. Ch* PutBegin()
  38. {
  39. AZ_Assert(false, "Not Implemented");
  40. return nullptr;
  41. }
  42. void Put(Ch c)
  43. {
  44. m_os.put(c);
  45. }
  46. void Flush()
  47. {
  48. m_os.flush();
  49. }
  50. size_t PutEnd(Ch*)
  51. {
  52. AZ_Assert(false, "Not Implemented");
  53. return 0;
  54. }
  55. private:
  56. JsonOutputStream(const JsonOutputStream&) = delete;
  57. JsonOutputStream& operator=(const JsonOutputStream&) = delete;
  58. std::ostream& m_os;
  59. };
  60. class JsonWriter;
  61. /// The default GlobalWriteJson implementation. Provide
  62. /// specializations of this function in the ServiceApi
  63. /// namespace to implement serialization differently
  64. /// for specific types.
  65. template<class ObjectType>
  66. bool GlobalWriteJson(JsonWriter& writer, const ObjectType& source)
  67. {
  68. return source.WriteJson(writer);
  69. }
  70. /// Used to produce request json format content.
  71. class JsonWriter
  72. : public rapidjson::Writer<JsonOutputStream>
  73. {
  74. using BaseType = rapidjson::Writer<JsonOutputStream>;
  75. public:
  76. template<typename ObjectType>
  77. static bool WriteObject(JsonOutputStream& os, const ObjectType& object)
  78. {
  79. JsonWriter writer{ os };
  80. return GlobalWriteJson(writer, object);
  81. }
  82. JsonWriter(JsonOutputStream& os)
  83. : BaseType{ os }
  84. {
  85. }
  86. bool Write(const char* s)
  87. {
  88. return String(s);
  89. }
  90. bool Write(const AZStd::string& s)
  91. {
  92. return String(s);
  93. }
  94. bool Write(int i)
  95. {
  96. return Int(i);
  97. }
  98. bool Write(unsigned i)
  99. {
  100. return Uint(i);
  101. }
  102. bool Write(int64_t i)
  103. {
  104. return Int64(i);
  105. }
  106. bool Write(uint64_t i)
  107. {
  108. return Uint64(i);
  109. }
  110. bool Write(bool b)
  111. {
  112. return Bool(b);
  113. }
  114. template<typename ElementType>
  115. bool Write(const AZStd::vector<ElementType, AZStd::allocator>& v)
  116. {
  117. return Array(v);
  118. }
  119. template<class ObjectType>
  120. bool Write(const ObjectType& o)
  121. {
  122. return Object(o);
  123. }
  124. template<class ValueType>
  125. bool Write(const char* key, const ValueType& value)
  126. {
  127. bool ok = Key(key);
  128. if (ok)
  129. {
  130. ok = Write(value);
  131. }
  132. return ok;
  133. }
  134. /// Write JSON format content directly to the writer's output stream.
  135. /// This can be used to efficiently output static content.
  136. bool WriteJson(const Ch* json)
  137. {
  138. if (json)
  139. {
  140. while (*json != '\0')
  141. {
  142. os_->Put(*json);
  143. ++json;
  144. }
  145. }
  146. return true;
  147. }
  148. /// Overload of String that calls c_str for you.
  149. bool String(const AZStd::string& str)
  150. {
  151. return BaseType::String(str.c_str());
  152. }
  153. /// Write an object. The object can implement a WriteJson function
  154. /// or you can provide an GlobalWriteJson template function
  155. /// specialization.
  156. template<class ObjectType>
  157. bool Object(const ObjectType& obj)
  158. {
  159. return GlobalWriteJson(*this, obj);
  160. }
  161. template<class ElementType>
  162. bool Array(const AZStd::vector<ElementType, AZStd::allocator>& v)
  163. {
  164. bool ok = StartArray();
  165. if (ok)
  166. {
  167. for (const ElementType& e : v)
  168. {
  169. ok = Write(e);
  170. if (!ok) break;
  171. }
  172. if (ok)
  173. {
  174. ok = EndArray();
  175. }
  176. }
  177. return ok;
  178. }
  179. private:
  180. // Prohibit copy constructor & assignment operator.
  181. JsonWriter(const JsonWriter&) = delete;
  182. JsonWriter& operator=(const JsonWriter&) = delete;
  183. };
  184. } // namespace AWSCore