jsonizer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  35. * @brief Construct an object of type 'T' from a datastream
  36. * @param T the output type to be deserialized
  37. * @param in A stream of JSON data
  38. * @return A newly constructed object by value
  39. */
  40. template <typename T> T from_stream(std::istream & in) const;
  41. template <typename T> T from_string(std::string const & in) const;
  42. /**
  43. * @brief Write an object out to a datastream, such as std::cout or a file
  44. * @param T the input type to be serialized
  45. * @param value The instance of type T to serialize
  46. * @param out an output stream of any sort
  47. */
  48. template <typename T>
  49. void to_stream(T const & value, std::ostream & out) const;
  50. /**
  51. * @brief Construct an object of type 'T' from a file handle
  52. * @param T the output type to be deserialized
  53. * @param file the name of a JSON file on disk to be read
  54. * @return A newly constructed object by value
  55. */
  56. template <typename T> T from_file(std::string const & file) const;
  57. /**
  58. * @brief Construct a jsonizer with a personal instance of the data cache.
  59. * Because there is no getter for p_cache, all cached instances are locally
  60. * stored, meaning that each jsonizer default-constructed will have a
  61. * different state of the cache.
  62. */
  63. Jsonizer();
  64. /**
  65. * @brief Construct a jsonizer with an externally owned cache
  66. * @param cache A shared_ptr to a data cache. Cannot be null
  67. * @throws std::logic_error if cache == nullptr
  68. */
  69. Jsonizer(std::shared_ptr<SharedCache> cache);
  70. /**
  71. * Do not permit the construction of a jsonizer from a nullptr literal.
  72. * This moves the run-time exception std::logic_error to compiler time.
  73. */
  74. Jsonizer(std::nullptr_t) = delete;
  75. private:
  76. template <typename T, size_t... Is>
  77. Json::Value to_json(T & value, std::index_sequence<Is...>) const;
  78. template <typename T, size_t... Is>
  79. void from_json(T & value, Json::Value const & json,
  80. std::index_sequence<Is...>) const;
  81. template <typename T> Json::Value to_json_impl(T const & value) const;
  82. template <typename T>
  83. void from_json_impl(T & value, Json::Value const & json) const;
  84. template <typename T, typename F>
  85. T from_cached_json(std::string const & key, Json::Value const & json,
  86. F && fetch) const;
  87. private:
  88. std::shared_ptr<SharedCache> p_cache;
  89. };
  90. }