jsonizer.tpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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>) {
  54. return static_cast<int>(value);
  55. } else if constexpr (detail::is_associative_container_v<T>) {
  56. Json::Value rval;
  57. using K = std::decay_t<typename T::key_type>;
  58. if constexpr (std::is_same_v<std::string, K> || std::is_arithmetic_v<K>) {
  59. for (auto const & [k, v] : value) {
  60. rval[to_string(k)] = to_json(v);
  61. }
  62. } else {
  63. for (auto const & pair : value) {
  64. rval.append(to_json(pair));
  65. }
  66. }
  67. return rval;
  68. } else if constexpr (detail::is_container_v<T>) {
  69. Json::Value rval;
  70. for (auto const & elem : value) {
  71. rval.append(to_json(elem));
  72. }
  73. return rval;
  74. } else {
  75. return to_json_impl(value);
  76. }
  77. }
  78. template <typename T, size_t... Is>
  79. void Jsonizer::from_json(T & value, Json::Value const & json,
  80. std::index_sequence<Is...>) const {
  81. [[maybe_unused]] auto l = {
  82. ((std::get<Is>(value) =
  83. from_json<std::tuple_element_t<Is, T>>(json[int(Is)])),
  84. 0)...};
  85. }
  86. template <typename T>
  87. void Jsonizer::from_json(std::optional<T> &opt, Json::Value const & json) const {
  88. opt = json.isNull() ? std::nullopt : std::optional{from_json<T>(json)};
  89. }
  90. template <typename T>
  91. void Jsonizer::from_json(T & value, Json::Value const & json) const {
  92. if (json.isNull()) return;
  93. if constexpr (detail::has_serial_type_v<T>) {
  94. value = T(from_json<typename T::serial_type>(json));
  95. } else if constexpr (std::is_enum_v<T>) {
  96. if (!string_utils::cast(value, json.asString())) {
  97. throw std::invalid_argument("Cannot cast to enum: " + json.asString());
  98. }
  99. } else if constexpr (std::is_same_v<std::string, T>) {
  100. value = json.asString();
  101. } else if constexpr (detail::is_tuple_v<T>) {
  102. from_json(value, json, std::make_index_sequence<std::tuple_size_v<T>>());
  103. } else if constexpr (detail::is_associative_container_v<T>) {
  104. using K = std::decay_t<typename T::key_type>;
  105. using V = std::decay_t<typename T::mapped_type>;
  106. if (json.isArray()) {
  107. std::vector<std::pair<K, V>> tmp;
  108. from_json(tmp, json);
  109. value.insert(tmp.begin(), tmp.end());
  110. } else if (json.isObject()) {
  111. if constexpr (std::is_constructible_v<K, std::string>) {
  112. for (auto it = json.begin(), end = json.end(); it != end; ++it) {
  113. value.emplace(it.key().asString(), from_json<V>(*it));
  114. }
  115. } else {
  116. for (auto it = json.begin(), end = json.end(); it != end; ++it) {
  117. value.emplace(from_string<K>(it.key().asString()), from_json<V>(*it));
  118. }
  119. }
  120. } else {
  121. throw std::invalid_argument(
  122. "cannot construct container from non-container");
  123. }
  124. } else if constexpr (detail::is_container_v<T>) {
  125. using V = std::decay_t<typename T::value_type>;
  126. if (!json.isArray()) {
  127. throw std::invalid_argument("cannot construct container from non-array");
  128. }
  129. for (auto const & elem : json) {
  130. value.insert(value.end(), from_json<V>(elem));
  131. }
  132. } else if constexpr (std::is_floating_point_v<T>) {
  133. value = static_cast<T>(json.asDouble());
  134. } else if constexpr (std::is_same_v<bool, T>) {
  135. value = json.asBool();
  136. } else if constexpr (std::is_arithmetic_v<T>) {
  137. value = static_cast<T>(json.asInt());
  138. } else {
  139. from_json_impl(value, json);
  140. }
  141. }
  142. template <typename T, typename F>
  143. T Jsonizer::from_cached_json(std::string const & key, Json::Value const & json,
  144. F && fetch) const {
  145. std::shared_ptr<T const> ptr = p_cache->fetch<T>(json[key].asString());
  146. if (!ptr) {
  147. T value{};
  148. std::forward<F>(fetch)(value, json);
  149. ptr = p_cache->store(value.name, std::move(value));
  150. }
  151. return *ptr;
  152. }
  153. template <typename T>
  154. void Jsonizer::from_json(std::shared_ptr<T const> & ptr,
  155. Json::Value const & json) const {
  156. if (json.isString()) {
  157. ptr = p_cache->fetch<T>(json.asString());
  158. } else {
  159. T local{};
  160. from_json(local, json);
  161. ptr = p_cache->store(local.name, local);
  162. }
  163. }
  164. }