rapidjson.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * rapid_json.h - JSON (de)serialize via RapidJSON
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __CORE__SERIALIZE__RAPIDJSON_H__38642728
  19. #define __CORE__SERIALIZE__RAPIDJSON_H__38642728
  20. #include <iostream>
  21. #include <boost/uuid/string_generator.hpp>
  22. #include <fmt/format.h>
  23. #include <rapidjson/reader.h>
  24. #include "serialize.h"
  25. using namespace fmt::literals;
  26. namespace rainynite::core {
  27. namespace serialize {
  28. boost::uuids::string_generator s_to_id;
  29. template <class W>
  30. class RJSONWrapper {
  31. private:
  32. enum class Status {
  33. Empty,
  34. Type,
  35. UID,
  36. Value,
  37. ObjectStart,
  38. };
  39. public:
  40. bool Null() {
  41. throw DeserializationError("Unexpected null");
  42. }
  43. bool Bool(bool) {
  44. throw DeserializationError("Unexpected bool");
  45. }
  46. bool Int(int) {
  47. throw DeserializationError("Unexpected int");
  48. }
  49. bool Uint(unsigned) {
  50. throw DeserializationError("Unexpected uint");
  51. }
  52. bool Int64(int64_t) {
  53. throw DeserializationError("Unexpected int64");
  54. }
  55. bool Uint64(uint64_t) {
  56. throw DeserializationError("Unexpected uint64");
  57. }
  58. bool Double(double) {
  59. throw DeserializationError("Unexpected double");
  60. }
  61. bool RawNumber(char const*, size_t, bool) {
  62. throw DeserializationError("Unexpected number");
  63. }
  64. bool String(char const* str, size_t length, bool) {
  65. auto s = string(str, length);
  66. try {
  67. switch (status) {
  68. case Status::Type: {
  69. writer.type(s);
  70. status = Status::Empty;
  71. } break;
  72. case Status::UID: {
  73. auto id = s_to_id(s);
  74. writer.object_start(id);
  75. } break;
  76. case Status::Value: {
  77. writer.value_string(s);
  78. } break;
  79. case Status::Empty: {
  80. auto id = s_to_id(s);
  81. writer.reference(id);
  82. } break;
  83. default:
  84. std::cerr << s << std::endl;
  85. std::cerr << (int)status << std::endl;
  86. throw DeserializationError("Unexpected status when received \"{}\""_format(s));
  87. }
  88. } catch (DeserializationError const& ex) {
  89. throw ex;
  90. } catch (std::exception const& ex) {
  91. throw DeserializationError("Uncaught exception ({}) when received string \"{}\""_format(ex.what(), s));
  92. }
  93. return true;
  94. }
  95. bool StartObject() {
  96. status = Status::ObjectStart;
  97. return true;
  98. }
  99. bool Key(char const* str, size_t length, bool) {
  100. auto key = string(str, length);
  101. if (key == "TYPE") {
  102. status = Status::Type;
  103. } else if (key == "VALUE") {
  104. writer.object_value_start();
  105. status = Status::Value;
  106. } else if (key == "UID") {
  107. // relying on UID being first element in JSON object
  108. // this should NOT be relied on as JSON does not preserve order
  109. if (status != Status::ObjectStart)
  110. throw DeserializationError("Unexpected UID");
  111. status = Status::UID;
  112. } else {
  113. writer.key(key);
  114. }
  115. return true;
  116. }
  117. bool EndObject(size_t) {
  118. status = Status::Empty;
  119. writer.object_end();
  120. return true;
  121. }
  122. bool StartArray() {
  123. status = Status::Empty;
  124. writer.list_start();
  125. return true;
  126. }
  127. bool EndArray(size_t) {
  128. status = Status::Empty;
  129. writer.list_end();
  130. return true;
  131. }
  132. public:
  133. W const& get_node_reader() const {
  134. return writer;
  135. }
  136. private:
  137. W writer;
  138. Status status;
  139. };
  140. } // namespace serialize
  141. } // namespace rainynite::core
  142. #endif