jsonizer_ios.tpp 914 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // jsonizer_ios.tpp
  3. // serializer
  4. //
  5. // Created by Sam Jaffe on 3/18/23.
  6. //
  7. #include <fstream>
  8. #include <sstream>
  9. #include <json/json.h>
  10. #include <serializer/jsonizer.h>
  11. namespace serializer {
  12. template <typename T> T Jsonizer::from_json(Json::Value const & json) const {
  13. std::decay_t<T> tmp;
  14. from_json(tmp, json);
  15. return tmp;
  16. }
  17. template <typename T> T Jsonizer::from_stream(std::istream & in) const {
  18. Json::Value root;
  19. in >> root;
  20. return from_json<T>(root);
  21. }
  22. template <typename T> T Jsonizer::from_string(std::string const & in) const {
  23. std::stringstream ss;
  24. ss << in;
  25. return from_stream<T>(ss);
  26. }
  27. template <typename T>
  28. void Jsonizer::to_stream(T const & value, std::ostream & out) const {
  29. Json::Value root = to_json(value);
  30. out << root;
  31. }
  32. template <typename T> T Jsonizer::from_file(std::string const & file) const {
  33. std::ifstream in(file);
  34. return from_stream<T>(in);
  35. }
  36. }