rapid_json_serializer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "rapid_json_serializer.h"
  15. #include "null_json_serializer.h"
  16. #include <rapidjson/document.h>
  17. #include <rapidjson/prettywriter.h>
  18. namespace dap {
  19. namespace json {
  20. RapidDeserializer::RapidDeserializer(const std::string& str)
  21. : doc(new rapidjson::Document()) {
  22. doc->Parse(str.c_str());
  23. }
  24. RapidDeserializer::RapidDeserializer(rapidjson::Value* json) : val(json) {}
  25. RapidDeserializer::~RapidDeserializer() {
  26. delete doc;
  27. }
  28. bool RapidDeserializer::deserialize(dap::boolean* v) const {
  29. if (!json()->IsBool()) {
  30. return false;
  31. }
  32. *v = json()->GetBool();
  33. return true;
  34. }
  35. bool RapidDeserializer::deserialize(dap::integer* v) const {
  36. if (json()->IsInt()) {
  37. *v = json()->GetInt();
  38. return true;
  39. } else if (json()->IsUint()) {
  40. *v = static_cast<int64_t>(json()->GetUint());
  41. return true;
  42. } else if (json()->IsInt64()) {
  43. *v = json()->GetInt64();
  44. return true;
  45. } else if (json()->IsUint64()) {
  46. *v = static_cast<int64_t>(json()->GetUint64());
  47. return true;
  48. }
  49. return false;
  50. }
  51. bool RapidDeserializer::deserialize(dap::number* v) const {
  52. if (!json()->IsNumber()) {
  53. return false;
  54. }
  55. *v = json()->GetDouble();
  56. return true;
  57. }
  58. bool RapidDeserializer::deserialize(dap::string* v) const {
  59. if (!json()->IsString()) {
  60. return false;
  61. }
  62. *v = json()->GetString();
  63. return true;
  64. }
  65. bool RapidDeserializer::deserialize(dap::object* v) const {
  66. v->reserve(json()->MemberCount());
  67. for (auto el = json()->MemberBegin(); el != json()->MemberEnd(); el++) {
  68. dap::any el_val;
  69. RapidDeserializer d(&(el->value));
  70. if (!d.deserialize(&el_val)) {
  71. return false;
  72. }
  73. (*v)[el->name.GetString()] = el_val;
  74. }
  75. return true;
  76. }
  77. bool RapidDeserializer::deserialize(dap::any* v) const {
  78. if (json()->IsBool()) {
  79. *v = dap::boolean(json()->GetBool());
  80. } else if (json()->IsDouble()) {
  81. *v = dap::number(json()->GetDouble());
  82. } else if (json()->IsInt()) {
  83. *v = dap::integer(json()->GetInt());
  84. } else if (json()->IsString()) {
  85. *v = dap::string(json()->GetString());
  86. } else if (json()->IsNull()) {
  87. *v = null();
  88. } else if (json()->IsObject()) {
  89. dap::object obj;
  90. if (!deserialize(&obj)) {
  91. return false;
  92. }
  93. *v = obj;
  94. } else if (json()->IsArray()){
  95. dap::array<any> arr;
  96. if (!deserialize(&arr)){
  97. return false;
  98. }
  99. *v = arr;
  100. } else {
  101. return false;
  102. }
  103. return true;
  104. }
  105. size_t RapidDeserializer::count() const {
  106. return json()->Size();
  107. }
  108. bool RapidDeserializer::array(
  109. const std::function<bool(dap::Deserializer*)>& cb) const {
  110. if (!json()->IsArray()) {
  111. return false;
  112. }
  113. for (uint32_t i = 0; i < json()->Size(); i++) {
  114. RapidDeserializer d(&(*json())[i]);
  115. if (!cb(&d)) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. bool RapidDeserializer::field(
  122. const std::string& name,
  123. const std::function<bool(dap::Deserializer*)>& cb) const {
  124. if (!json()->IsObject()) {
  125. return false;
  126. }
  127. auto it = json()->FindMember(name.c_str());
  128. if (it == json()->MemberEnd()) {
  129. return cb(&NullDeserializer::instance);
  130. }
  131. RapidDeserializer d(&(it->value));
  132. return cb(&d);
  133. }
  134. RapidSerializer::RapidSerializer()
  135. : doc(new rapidjson::Document(rapidjson::kObjectType)),
  136. allocator(doc->GetAllocator()) {}
  137. RapidSerializer::RapidSerializer(rapidjson::Value* json,
  138. rapidjson::Document::AllocatorType& allocator)
  139. : val(json), allocator(allocator) {}
  140. RapidSerializer::~RapidSerializer() {
  141. delete doc;
  142. }
  143. std::string RapidSerializer::dump() const {
  144. rapidjson::StringBuffer sb;
  145. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  146. json()->Accept(writer);
  147. return sb.GetString();
  148. }
  149. bool RapidSerializer::serialize(dap::boolean v) {
  150. json()->SetBool(v);
  151. return true;
  152. }
  153. bool RapidSerializer::serialize(dap::integer v) {
  154. json()->SetInt64(v);
  155. return true;
  156. }
  157. bool RapidSerializer::serialize(dap::number v) {
  158. json()->SetDouble(v);
  159. return true;
  160. }
  161. bool RapidSerializer::serialize(const dap::string& v) {
  162. json()->SetString(v.data(), static_cast<uint32_t>(v.length()), allocator);
  163. return true;
  164. }
  165. bool RapidSerializer::serialize(const dap::object& v) {
  166. if (!json()->IsObject()) {
  167. json()->SetObject();
  168. }
  169. for (auto& it : v) {
  170. if (!json()->HasMember(it.first.c_str())) {
  171. rapidjson::Value name_value{it.first.c_str(), allocator};
  172. json()->AddMember(name_value, rapidjson::Value(), allocator);
  173. }
  174. rapidjson::Value& member = (*json())[it.first.c_str()];
  175. RapidSerializer s(&member, allocator);
  176. if (!s.serialize(it.second)) {
  177. return false;
  178. }
  179. }
  180. return true;
  181. }
  182. bool RapidSerializer::serialize(const dap::any& v) {
  183. if (v.is<dap::boolean>()) {
  184. json()->SetBool((bool)v.get<dap::boolean>());
  185. } else if (v.is<dap::integer>()) {
  186. json()->SetInt64(v.get<dap::integer>());
  187. } else if (v.is<dap::number>()) {
  188. json()->SetDouble((double)v.get<dap::number>());
  189. } else if (v.is<dap::string>()) {
  190. auto s = v.get<dap::string>();
  191. json()->SetString(s.data(), static_cast<uint32_t>(s.length()), allocator);
  192. } else if (v.is<dap::object>()) {
  193. // reachable if dap::object nested is inside other dap::object
  194. return serialize(v.get<dap::object>());
  195. } else if (v.is<dap::null>()) {
  196. } else {
  197. // reachable if array or custom serialized type is nested inside other dap::object
  198. auto type = get_any_type(v);
  199. auto value = get_any_val(v);
  200. if (type && value) {
  201. return type->serialize(this, value);
  202. }
  203. return false;
  204. }
  205. return true;
  206. }
  207. bool RapidSerializer::array(size_t count,
  208. const std::function<bool(dap::Serializer*)>& cb) {
  209. if (!json()->IsArray()) {
  210. json()->SetArray();
  211. }
  212. while (count > json()->Size()) {
  213. json()->PushBack(rapidjson::Value(), allocator);
  214. }
  215. for (uint32_t i = 0; i < count; i++) {
  216. RapidSerializer s(&(*json())[i], allocator);
  217. if (!cb(&s)) {
  218. return false;
  219. }
  220. }
  221. return true;
  222. }
  223. bool RapidSerializer::object(
  224. const std::function<bool(dap::FieldSerializer*)>& cb) {
  225. struct FS : public FieldSerializer {
  226. rapidjson::Value* const json;
  227. rapidjson::Document::AllocatorType& allocator;
  228. FS(rapidjson::Value* json, rapidjson::Document::AllocatorType& allocator)
  229. : json(json), allocator(allocator) {}
  230. bool field(const std::string& name, const SerializeFunc& cb) override {
  231. if (!json->HasMember(name.c_str())) {
  232. rapidjson::Value name_value{name.c_str(), allocator};
  233. json->AddMember(name_value, rapidjson::Value(), allocator);
  234. }
  235. rapidjson::Value& member = (*json)[name.c_str()];
  236. RapidSerializer s(&member, allocator);
  237. auto res = cb(&s);
  238. if (s.removed) {
  239. json->RemoveMember(name.c_str());
  240. }
  241. return res;
  242. }
  243. };
  244. if (!json()->IsObject()) {
  245. json()->SetObject();
  246. }
  247. FS fs{json(), allocator};
  248. return cb(&fs);
  249. }
  250. void RapidSerializer::remove() {
  251. removed = true;
  252. }
  253. } // namespace json
  254. } // namespace dap