Advertisement
Moraxno

Untitled

Jun 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #include <nlohmann/json.hpp>
  7.  
  8. namespace Neat
  9. {
  10. class FileHelper
  11. {
  12. public:
  13. template <typename T>
  14. static bool readFromFile(const std::string& path, T& bufferInstance)
  15. {
  16. bool success = true;
  17. std::ifstream file(path);
  18.  
  19. try
  20. {
  21. nlohmann::json jsonFromFile = nlohmann::json::parse(file);
  22. bufferInstance = meta::deserialize<T>(jsonFromFile);
  23. }
  24. catch(const std::exception& e)
  25. {
  26. std::cerr << e.what() << '\n';
  27. success = false;
  28. }
  29.  
  30. file.close();
  31. return success;
  32. }
  33.  
  34. template <typename T>
  35. static bool writeToFile(const std::string& path, const T& instance)
  36. {
  37. bool success = true;
  38. std::ofstream file(path);
  39.  
  40. try
  41. {
  42. nlohmann::json jsonFromInstance = meta::serialize<T>(instance);
  43. file << jsonFromInstance;
  44. }
  45. catch(const std::exception& e)
  46. {
  47. std::cerr << e.what() << '\n';
  48. success = false;
  49. }
  50.  
  51. file.close();
  52. return success;
  53. }
  54.  
  55. };
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement