jsonizer.h 3.6 KB

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