Skip to content
Snippets Groups Projects
Commit af7a2a9e authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add read/write_json for i/ostream.

parent 13b9086b
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
#include <filesystem>
#include <fstream>
namespace fs = std::filesystem;
......@@ -28,9 +29,7 @@ namespace nlohmann
try
{
ifs.open(filename);
json j;
ifs >> j;
return j;
return read_json(ifs);
}
catch (const std::ios_base::failure& e)
{
......@@ -42,6 +41,15 @@ namespace nlohmann
}
json read_json(std::istream& is)
{
json j;
is >> j;
return j;
}
void write_json(const std::string& filename, const json& j,
const int indent, const char indent_char)
{
......@@ -52,7 +60,7 @@ namespace nlohmann
try
{
ofs.open(filename);
ofs << j.dump(indent, indent_char);
write_json(ofs, j, indent, indent_char);
}
catch (const std::ios_base::failure& e)
{
......@@ -62,6 +70,12 @@ namespace nlohmann
}
}
void write_json(std::ostream& os, const json& j, const int indent, const char indent_char)
{
os << j.dump(indent, indent_char);
}
}
......@@ -8,27 +8,43 @@ namespace nlohmann
/**
* @brief Read a JSON document from the given file.
*
*
* @param filename The name of the file to read from.
* @return The JSON document.
*
*
* @throw std::ios_base::failure If IO access fails.
*/
json read_json(const std::string& filename);
/**
* @brief Write a JSON document to the given file.
*
* @param j The JSON document.
* @brief Read a JSON document from the given in-stream.
* @param is The in-stream.
* @return The JSON document.
*/
json read_json(std::istream& is);
/**
* @brief Write a JSON document to the given file.
*
* @param filename The name of the file to write to.
* @param j The JSON document.
* @param indent See nlohmann::json::dump().
* @param indent_char See nlohmann::json::dump().
*
*
* @throw std::ios_base::failure If IO access fails.
*/
void write_json(const std::string& filename, const json& j,
const int indent = -1, const char indent_char = ' ');
/**
* @brief Write a JSON document to the given out-stream.
* @param os The out-stream.
* @param j The JSON document.
* @param indent See nlohmann::json::dump().
* @param indent_char See nlohmann::json::dump().
*/
void write_json(std::ostream& os, const json& j,
const int indent = -1, const char indent_char = ' ');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment