Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <string>
- #include <nlohmann/json.hpp>
- namespace Neat
- {
- class FileHelper
- {
- public:
- template <typename T>
- static bool readFromFile(const std::string& path, T& bufferInstance)
- {
- bool success = true;
- std::ifstream file(path);
- try
- {
- nlohmann::json jsonFromFile = nlohmann::json::parse(file);
- bufferInstance = meta::deserialize<T>(jsonFromFile);
- }
- catch(const std::exception& e)
- {
- std::cerr << e.what() << '\n';
- success = false;
- }
- file.close();
- return success;
- }
- template <typename T>
- static bool writeToFile(const std::string& path, const T& instance)
- {
- bool success = true;
- std::ofstream file(path);
- try
- {
- nlohmann::json jsonFromInstance = meta::serialize<T>(instance);
- file << jsonFromInstance;
- }
- catch(const std::exception& e)
- {
- std::cerr << e.what() << '\n';
- success = false;
- }
- file.close();
- return success;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement