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

Add json/io.h/.cpp with write_json() and read_json()

parent 54a59b6b
No related branches found
No related tags found
No related merge requests found
......@@ -353,6 +353,7 @@ SET(SOURCES
Tools/Gravity.cpp
Util/json/eigen_conversion.cpp
Util/json/io.cpp
Util/xml/tinyxml2.cpp
Visualization/ColorMap.cpp
......@@ -575,6 +576,7 @@ SET(INCLUDES
Util/json/eigen_conversion.h
Util/json/eigen_conversion.hpp
Util/json/io.h
Util/json/json.hpp
Util/xml/tinyxml2.h
......
#include "io.h"
#include <iostream>
#include <fstream>
namespace nlohmann
{
json read_json(const std::string& filename)
{
std::ifstream ifs;
// Allow throwing std::ios_base::failure.
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs.open(filename);
json j;
ifs >> j;
return j;
}
void write_json(const std::string& filename, const json& j,
const int indent, const char indent_char)
{
std::ofstream ofs;
// Allow throwing std::ios_base::failure.
ofs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ofs.open(filename);
ofs << j.dump(indent, indent_char);
}
}
#pragma once
#include "json.hpp"
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.
* @param filename The name of the file to write to.
* @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 = ' ');
}
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