jsonizer.tpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // jsonizer.tpp
  3. // serializer
  4. //
  5. // Created by Sam Jaffe on 3/15/23.
  6. //
  7. #pragma once
  8. #include <stdexcept>
  9. #include <expect/expect.hpp>
  10. #include <json/json.h>
  11. #include <magic_enum.hpp>
  12. #include <string_utils/cast.h>
  13. #include <serializer/jsonizer.h>
  14. #include <serializer/jsonizer_ios.tpp>
  15. #include <serializer/shared_cache.h>
  16. #include <serializer/strconv.h>
  17. #include <serializer/traits.h>
  18. namespace serializer {
  19. inline Jsonizer::Jsonizer() : p_cache(std::make_shared<SharedCache>()) {}
  20. /**
  21. * @brief Construct a jsonizer with an externally owned cache
  22. * @param cache A shared_ptr to a data cache. Cannot be null
  23. * @throws std::logic_error if cache == nullptr
  24. */
  25. inline Jsonizer::Jsonizer(std::shared_ptr<SharedCache> cache) : p_cache(cache) {
  26. expects(p_cache != nullptr);
  27. }
  28. template <typename T, size_t... Is>
  29. Json::Value Jsonizer::to_json(T & value, std::index_sequence<Is...>) const {
  30. Json::Value json;
  31. [[maybe_unused]] auto l = {
  32. ((json[int(Is)] = to_json(std::get<Is>(value))), 0)...};
  33. return json;
  34. }
  35. template <typename T> Json::Value Jsonizer::to_json(std::optional<T> const &opt) const {
  36. return opt.has_value() ? to_json(*opt) : Json::Value();
  37. }
  38. template <typename T> Json::Value Jsonizer::to_json(T const & value) const {
  39. if constexpr (detail::has_serial_type_v<T>) {
  40. return to_json(static_cast<typename T::serial_type>(value));
  41. } else if constexpr (std::is_enum_v<T>) {
  42. constexpr auto type =
  43. magic_enum::as_flags<magic_enum::detail::is_flags_v<T>>;
  44. return std::string(magic_enum::enum_name<type>(value));
  45. } else if constexpr (detail::is_tuple_v<T>) {
  46. return to_json(value, std::make_index_sequence<std::tuple_size_v<T>>());
  47. } else if constexpr (std::is_constructible_v<std::string, T>) {
  48. return std::string(value);
  49. } else if constexpr (std::is_floating_point_v<T>) {
  50. return static_cast<double>(value);
  51. } else if constexpr (std::is_same_v<bool, T>) {
  52. return value;
  53. } else if constexpr (std::is_arithmetic_v<T> && std::is_unsigned_v<T>) {
  54. return static_cast<uint64_t>(value);
  55. } else if constexpr (std::is_arithmetic_v<T>) {
  56. return static_cast<int64_t>(value);
  57. } else if constexpr (detail::is_associative_container_v<T>) {
  58. Json::Value rval;
  59. using K = std::decay_t<typename T::key_type>;
  60. if constexpr (std::is_same_v<std::string, K> || std::is_arithmetic_v<K>) {
  61. for (auto const & [k, v] : value) {
  62. rval[to_string(k)] = to_json(v);
  63. }
  64. } else {
  65. for (auto const & pair : value) {
  66. rval.append(to_json(pair));
  67. }
  68. }
  69. return rval;
  70. } else if constexpr (detail::is_container_v<T>) {
  71. Json::Value rval;
  72. for (auto const & elem : value) {
  73. rval.append(to_json(elem));
  74. }
  75. return rval;
  76. } else {
  77. return to_json_impl(value);
  78. }
  79. }
  80. template <typename T, size_t... Is>
  81. void Jsonizer::from_json(T & value, Json::Value const & json,
  82. std::index_sequence<Is...>) const {
  83. [[maybe_unused]] auto l = {
  84. ((std::get<Is>(value) =
  85. from_json<std::tuple_element_t<Is, T>>(json[int(Is)])),
  86. 0)...};
  87. }
  88. template <typename T>
  89. void Jsonizer::from_json(std::optional<T> &opt, Json::Value const & json) const {
  90. opt = json.isNull() ? std::nullopt : std::optional{from_json<T>(json)};
  91. }
  92. template <typename T>
  93. void Jsonizer::from_json(T & value, Json::Value const & json) const {
  94. if (json.isNull()) return;
  95. if constexpr (detail::has_serial_type_v<T>) {
  96. value = T(from_json<typename T::serial_type>(json));
  97. } else if constexpr (std::is_enum_v<T>) {
  98. if (json.isInt()) {
  99. value = *magic_enum::enum_cast<T>(json.asInt());
  100. } else if (!string_utils::cast(value, json.asString())) {
  101. throw std::invalid_argument("Cannot cast to enum: " + json.asString());
  102. }
  103. } else if constexpr (std::is_same_v<std::string, T>) {
  104. value = json.asString();
  105. } else if constexpr (detail::is_tuple_v<T>) {
  106. from_json(value, json, std::make_index_sequence<std::tuple_size_v<T>>());
  107. } else if constexpr (detail::is_associative_container_v<T>) {
  108. using K = std::decay_t<typename T::key_type>;
  109. using V = std::decay_t<typename T::mapped_type>;
  110. if (json.isArray()) {
  111. std::vector<std::pair<K, V>> tmp;
  112. from_json(tmp, json);
  113. value.insert(tmp.begin(), tmp.end());
  114. } else if (json.isObject()) {
  115. if constexpr (std::is_constructible_v<K, std::string>) {
  116. for (auto it = json.begin(), end = json.end(); it != end; ++it) {
  117. value.emplace(it.key().asString(), from_json<V>(*it));
  118. }
  119. } else {
  120. for (auto it = json.begin(), end = json.end(); it != end; ++it) {
  121. value.emplace(from_string<K>(it.key().asString()), from_json<V>(*it));
  122. }
  123. }
  124. } else {
  125. throw std::invalid_argument(
  126. "cannot construct container from non-container");
  127. }
  128. } else if constexpr (detail::is_container_v<T>) {
  129. using V = std::decay_t<typename T::value_type>;
  130. if (!json.isArray()) {
  131. throw std::invalid_argument("cannot construct container from non-array");
  132. }
  133. for (auto const & elem : json) {
  134. value.insert(value.end(), from_json<V>(elem));
  135. }
  136. } else if constexpr (std::is_floating_point_v<T>) {
  137. value = static_cast<T>(json.asDouble());
  138. } else if constexpr (std::is_same_v<bool, T>) {
  139. value = json.asBool();
  140. } else if constexpr (std::is_arithmetic_v<T> && std::is_unsigned_v<T>) {
  141. value = static_cast<T>(json.asUInt64());
  142. } else if constexpr (std::is_arithmetic_v<T>) {
  143. value = static_cast<T>(json.asInt64());
  144. } else {
  145. from_json_impl(value, json);
  146. }
  147. }
  148. template <typename T, typename F>
  149. T Jsonizer::from_cached_json(std::string const & key, Json::Value const & json,
  150. F && fetch) const {
  151. std::shared_ptr<T const> ptr = p_cache->fetch<T>(json[key].asString());
  152. if (!ptr) {
  153. T value{};
  154. std::forward<F>(fetch)(value, json);
  155. ptr = p_cache->store(value.name, std::move(value));
  156. }
  157. return *ptr;
  158. }
  159. template <typename T>
  160. void Jsonizer::from_json(std::shared_ptr<T const> & ptr,
  161. Json::Value const & json) const {
  162. if (json.isString()) {
  163. ptr = p_cache->fetch<T>(json.asString());
  164. } else {
  165. T local{};
  166. from_json(local, json);
  167. ptr = p_cache->store(local.name, local);
  168. }
  169. }
  170. }