jsonizer.tpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <json/json.h>
  12. #include <magic_enum.hpp>
  13. #include <serializer/jsonizer.h>
  14. #include <serializer/traits.h>
  15. namespace serializer {
  16. template <typename T, size_t... Is>
  17. Json::Value Jsonizer::to_json(T & value, std::index_sequence<Is...>) const {
  18. Json::Value json;
  19. [[maybe_unused]] auto l = {
  20. ((json[int(Is)] = to_json(std::get<Is>(value))), 0)...};
  21. return json;
  22. }
  23. template <typename T> Json::Value Jsonizer::to_json(T const & value) const {
  24. if constexpr (detail::has_serial_type_v<T>) {
  25. return to_json(static_cast<typename T::serial_type>(value));
  26. } else if constexpr (std::is_enum_v<T>) {
  27. return magic_enum::enum_name(value);
  28. } else if constexpr (detail::is_tuple_v<T>) {
  29. return to_json(value, std::make_index_sequence<std::tuple_size_v<T>>());
  30. } else if constexpr (std::is_same_v<T, std::string>) {
  31. return value;
  32. } else if constexpr (std::is_floating_point_v<T>) {
  33. return static_cast<double>(value);
  34. } else if constexpr (std::is_arithmetic_v<T>) {
  35. return static_cast<int>(value);
  36. } else if constexpr (detail::is_associative_container_v<T>) {
  37. Json::Value rval;
  38. using K = std::decay_t<typename T::key_type>;
  39. if constexpr (std::is_same_v<std::string, K> || std::is_arithmetic_v<K>) {
  40. for (auto const & [k, v] : value) {
  41. rval[to_string(k)] = to_json(v);
  42. }
  43. } else {
  44. for (auto const & pair : value) {
  45. rval.append(to_json(pair));
  46. }
  47. }
  48. return rval;
  49. } else if constexpr (detail::is_container_v<T>) {
  50. Json::Value rval;
  51. for (auto const & elem : value) {
  52. rval.append(to_json(elem));
  53. }
  54. return rval;
  55. } else {
  56. return to_json_impl(value);
  57. }
  58. }
  59. template <typename T, size_t... Is>
  60. void Jsonizer::from_json(T & value, Json::Value const & json,
  61. std::index_sequence<Is...>) const {
  62. [[maybe_unused]] auto l = {
  63. ((std::get<Is>(value) =
  64. from_json<std::tuple_element_t<Is, T>>(json[int(Is)])),
  65. 0)...};
  66. }
  67. template <typename T>
  68. void Jsonizer::from_json(T & value, Json::Value const & json) const {
  69. if (json.isNull()) return;
  70. if constexpr (detail::has_serial_type_v<T>) {
  71. value = T(from_json<typename T::serial_type>(json));
  72. } else if constexpr (std::is_enum_v<T>) {
  73. if (!from_string(value, json.asString())) {
  74. throw std::invalid_argument("Cannot cast to enum: " + json.asString());
  75. }
  76. } else if constexpr (std::is_same_v<std::string, T>) {
  77. value = json.asString();
  78. } else if constexpr (detail::is_tuple_v<T>) {
  79. from_json(value, json, std::make_index_sequence<std::tuple_size_v<T>>());
  80. } else if constexpr (detail::is_associative_container_v<T>) {
  81. using K = std::decay_t<typename T::key_type>;
  82. using V = std::decay_t<typename T::mapped_type>;
  83. if (json.isArray()) {
  84. std::vector<std::pair<K, V>> tmp;
  85. from_json(tmp, json);
  86. value.insert(tmp.begin(), tmp.end());
  87. } else if (json.isObject()) {
  88. if constexpr (std::is_constructible_v<K, std::string>) {
  89. for (auto it = json.begin(), end = json.end(); it != end; ++it) {
  90. value.emplace(it.key().asString(), from_json<V>(*it));
  91. }
  92. } else {
  93. for (auto it = json.begin(), end = json.end(); it != end; ++it) {
  94. value.emplace(from_string<K>(it.key().asString()), from_json<V>(*it));
  95. }
  96. }
  97. } else {
  98. throw std::invalid_argument(
  99. "cannot construct container from non-container");
  100. }
  101. } else if constexpr (detail::is_container_v<T>) {
  102. using V = std::decay_t<typename T::value_type>;
  103. if (!json.isArray()) {
  104. throw std::invalid_argument("cannot construct container from non-array");
  105. }
  106. for (auto const & elem : json) {
  107. value.insert(value.end(), from_json<V>(elem));
  108. }
  109. } else if constexpr (std::is_floating_point_v<T>) {
  110. value = static_cast<T>(json.asDouble());
  111. } else if constexpr (std::is_arithmetic_v<T>) {
  112. value = static_cast<T>(json.asInt());
  113. } else {
  114. from_json_impl(value, json);
  115. }
  116. }
  117. template <typename T> T Jsonizer::from_json(Json::Value const & json) const {
  118. std::decay_t<T> tmp;
  119. from_json(tmp, json);
  120. return tmp;
  121. }
  122. template <typename T> T Jsonizer::from_stream(std::istream & in) const {
  123. Json::Value root;
  124. in >> root;
  125. return from_json<T>(root);
  126. }
  127. template <typename T> T Jsonizer::from_string(std::string const & in) const {
  128. std::stringstream ss;
  129. ss << in;
  130. return from_stream<T>(ss);
  131. }
  132. template <typename T>
  133. void Jsonizer::to_stream(T const & value, std::ostream & out) const {
  134. Json::Value root = to_json(value);
  135. out << root;
  136. }
  137. template <typename T> T Jsonizer::from_file(std::string const & file) const {
  138. std::ifstream in(file);
  139. return from_stream<T>(in);
  140. }
  141. }