jsonizer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // jsonizer.h
  3. // serializer
  4. //
  5. // Created by Sam Jaffe on 3/15/23.
  6. //
  7. #pragma once
  8. #include <iosfwd>
  9. #include <memory>
  10. #include <optional>
  11. #include <string>
  12. #include <json/forwards.h>
  13. #include <serializer/forwards.h>
  14. namespace serializer {
  15. class Jsonizer {
  16. public:
  17. template <typename T> Json::Value to_json(T const & value) const;
  18. template <typename T>
  19. void from_json(T & value, Json::Value const & json) const;
  20. template <typename T> T from_json(Json::Value const & json) const;
  21. /**
  22. * @brief Load an object from either JSON, or from p_cache, depending on
  23. * the structure of the inputs.
  24. * @param ptr A pointer to an uninstantiated value
  25. * @param json A JSON object or string. If json is a string, then we attempt
  26. * to load ptr from p_cache, according to {@see Json::Value::asString}. If
  27. * json is an object, then we instead construct it like normal and attempt
  28. * to store the new object in p_cache.
  29. * @throws If json.isString() and T is not a recognized type in shared_cache
  30. * then this function will throw according to {@see shared_cache::fetch}.
  31. */
  32. template <typename T>
  33. void from_json(std::shared_ptr<T const> & ptr,
  34. Json::Value const & json) const;
  35. template <typename T> Json::Value to_json(std::optional<T> const & opt) const;
  36. template <typename T>
  37. void from_json(std::optional<T> & opt, Json::Value const & json) const;
  38. /**
  39. * @brief Construct an object of type 'T' from a datastream
  40. * @tparam T the output type to be deserialized
  41. * @param in A stream of JSON data
  42. * @return A newly constructed object by value
  43. */
  44. template <typename T> T from_stream(std::istream & in) const;
  45. template <typename T> T from_string(std::string_view in) const;
  46. /**
  47. * @brief Write an object out to a datastream, such as std::cout or a file
  48. * @tparam T the input type to be serialized
  49. * @param value The instance of type T to serialize
  50. * @param out an output stream of any sort
  51. */
  52. template <typename T>
  53. void to_stream(T const & value, std::ostream & out) const;
  54. /**
  55. * @brief Construct an object of type 'T' from a file handle
  56. * @tparam T the output type to be deserialized
  57. * @param file the name of a JSON file on disk to be read
  58. * @return A newly constructed object by value
  59. */
  60. template <typename T> T from_file(std::string const & file) const;
  61. /**
  62. * @brief Construct a jsonizer with a personal instance of the data cache.
  63. * Because there is no getter for p_cache, all cached instances are locally
  64. * stored, meaning that each jsonizer default-constructed will have a
  65. * different state of the cache.
  66. */
  67. Jsonizer();
  68. /**
  69. * @brief Construct a jsonizer with an externally owned cache
  70. * @param cache A shared_ptr to a data cache. Cannot be null
  71. * @throws std::logic_error if cache == nullptr
  72. */
  73. Jsonizer(std::shared_ptr<SharedCache> cache);
  74. /**
  75. * Do not permit the construction of a jsonizer from a nullptr literal.
  76. * This moves the run-time exception std::logic_error to compiler time.
  77. */
  78. Jsonizer(std::nullptr_t) = delete;
  79. private:
  80. template <typename T, size_t... Is>
  81. Json::Value to_json(T & value, std::index_sequence<Is...>) const;
  82. template <typename T, size_t... Is>
  83. void from_json(T & value, Json::Value const & json,
  84. std::index_sequence<Is...>) const;
  85. template <typename T> Json::Value to_json_impl(T const & value) const;
  86. template <typename T>
  87. void from_json_impl(T & value, Json::Value const & json) const;
  88. template <typename T, typename F>
  89. T from_cached_json(std::string const & key, Json::Value const & json,
  90. F && fetch) const;
  91. private:
  92. std::shared_ptr<SharedCache> p_cache;
  93. };
  94. }