jsonizer.tpp 5.5 KB

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