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

Merge branch 'json-io'

parents 54a59b6b 2c75e89e
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
......@@ -573,8 +574,10 @@ SET(INCLUDES
Transformation/DHParameter.h
Util/json.h
Util/json/eigen_conversion.h
Util/json/eigen_conversion.hpp
Util/json/io.h
Util/json/json.hpp
Util/xml/tinyxml2.h
......
#pragma once
#include "json/json.hpp"
#include "json/eigen_conversion.h"
#include "json/io.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 = ' ');
}
......@@ -34,6 +34,8 @@ ADD_VR_TEST( VirtualRobotTimeOptimalTrajectoryTest )
ADD_VR_TEST( VirtualRobotRuntimeEnvironmentTest )
ADD_VR_TEST( VirtualRobotJsonEigenConversionTest )
ADD_VR_TEST( VirtualRobotJsonIOTest )
ADD_VR_TEST( VirtualRobotMjcfTest )
......
/**
* @package VirtualRobot
* @author Rainer Kartmann
* @copyright 2018 Rainer Kartmann
*/
#define BOOST_TEST_MODULE VirtualRobot_VirtualRobotJsonIOTest
#include <VirtualRobot/VirtualRobotTest.h>
#include <filesystem>
#include <VirtualRobot/Util/json/io.h>
namespace fs = std::filesystem;
struct Fixture
{
const std::string FILENAME = "JsonIOTest.json";
nlohmann::json testj;
Fixture()
{
testj["s"] = "answer";
testj["i"] = 42;
if (fs::is_regular_file(FILENAME))
{
fs::remove(FILENAME);
}
}
~Fixture()
{
if (fs::is_regular_file(FILENAME))
{
fs::remove(FILENAME);
}
}
};
BOOST_FIXTURE_TEST_SUITE(VirtualRobotIOConversionTest, Fixture)
BOOST_AUTO_TEST_CASE(test_read_json_existent)
{
// Write the JSON file.
{
std::ofstream ofs(FILENAME);
ofs << testj.dump();
// Ensure file exists.
}
BOOST_CHECK(fs::exists(FILENAME));
// Test reading.
const nlohmann::json j = nlohmann::read_json(FILENAME);
BOOST_CHECK_EQUAL(j, testj);
BOOST_CHECK_EQUAL(j.at("s").get<std::string>(), testj.at("s").get<std::string>());
BOOST_CHECK_EQUAL(j.at("i").get<int>(), testj.at("i").get<int>());
}
BOOST_AUTO_TEST_CASE(test_read_json_nonexistent)
{
// Ensure file does not exist.
BOOST_CHECK(!fs::exists(FILENAME));
// Test reading.
nlohmann::json j;
BOOST_CHECK_THROW(j = nlohmann::read_json(FILENAME), std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE(test_write_json_valid)
{
// Ensure file does not exist.
BOOST_CHECK(!fs::exists(FILENAME));
// Test writing.
nlohmann::write_json(FILENAME, testj);
// Check that something has been written.
BOOST_CHECK(fs::exists(FILENAME));
}
BOOST_AUTO_TEST_CASE(test_write_json_invalid)
{
BOOST_CHECK(!fs::exists(FILENAME));
// Make it a directory, i.e. not writable.
fs::create_directory(FILENAME);
BOOST_CHECK(fs::exists(FILENAME));
// Test writing.
BOOST_CHECK_THROW(nlohmann::write_json(FILENAME, testj);, std::ios_base::failure);
// Clean up.
fs::remove(FILENAME);
BOOST_CHECK(!fs::exists(FILENAME));
}
BOOST_AUTO_TEST_CASE(test_read_after_write_json)
{
// Ensure file does not exist.
BOOST_CHECK(!fs::exists(FILENAME));
// Test writing.
nlohmann::write_json(FILENAME, testj);
// Test reading.
nlohmann::json j;
BOOST_CHECK_NO_THROW(j = nlohmann::read_json(FILENAME));
BOOST_CHECK_EQUAL(j, testj);
BOOST_CHECK_EQUAL(j.at("s").get<std::string>(), testj.at("s").get<std::string>());
BOOST_CHECK_EQUAL(j.at("i").get<int>(), testj.at("i").get<int>());
}
BOOST_AUTO_TEST_SUITE_END()
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