json: Added nlohmann's JSON C++ header

Adding JSON header for serializing/deserializing library descriptor JSON
files.

    * Added header
    * Added simple unit test to verify functionality

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2018-03-02 16:01:20 -08:00
parent 92b0919f56
commit 41e80d611e
4 changed files with 14968 additions and 1 deletions

View File

@ -489,4 +489,4 @@ endif()
# Install C headers
install(DIRECTORY include/ DESTINATION include/upm
COMPONENT ${CMAKE_PROJECT_NAME}-dev
FILES_MATCHING PATTERN "*.h")
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")

14874
include/external/json/json.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,13 @@ if(NOT GTEST_FOUND)
return ()
endif()
# Unit tests - utilities library
add_executable(utilities_tests utilities/utilities_tests.cxx)
target_link_libraries(utilities_tests utilities GTest::GTest GTest::Main)
gtest_add_tests(utilities_tests "" AUTO)
# Unit tests - Json header
add_executable(json_tests json/json_tests.cxx)
target_link_libraries(json_tests GTest::GTest GTest::Main)
target_include_directories(json_tests PRIVATE "${UPM_COMMON_HEADER_DIRS}/")
gtest_add_tests(json_tests "" AUTO)

View File

@ -0,0 +1,86 @@
#include "gtest/gtest.h"
#include "external/json/json.hpp"
namespace
{
class source
{
public:
std::string unit;
float max;
float min;
float accuracy;
};
void to_json(nlohmann::json& j, const source& p) {
j = nlohmann::json{
{"unit", p.unit},
{"min", p.min},
{"max", p.max},
{"accuracy", p.accuracy}};
}
void from_json(const nlohmann::json& j, source& p) {
p.unit = j.at("unit").get<std::string>();
p.min = j.at("min").get<float>();
p.max = j.at("max").get<float>();
p.accuracy = j.at("accuracy").get<float>();
}
bool operator==(const source& lhs, const source& rhs)
{
return lhs.unit == rhs.unit && lhs.min == rhs.min;
}
}
/* Json test fixture */
class json_unit : public ::testing::Test
{
protected:
/* One-time setup logic if needed */
json_unit() {}
/* One-time tear-down logic if needed */
virtual ~json_unit() {}
/* Per-test setup logic if needed */
virtual void SetUp() {}
/* Per-test tear-down logic if needed */
virtual void TearDown() {}
};
/* Test basic json serialization */
TEST_F(json_unit, test_basic_class_serialization)
{
source s_temp {"C", -40.0, 85.0, 1.0};
source s_humi {"%", 0.0, 100.0, 1.0};
/* Create the json objects */
nlohmann::json j_temp = s_temp;
nlohmann::json j_humi = s_humi;
/* Serialize/deserialize the originals */
source clone_temp = j_temp;
source clone_humi = j_humi;
ASSERT_EQ(s_temp, clone_temp);
/* Populate an stl container */
std::map<std::string, source> sources;
sources["temperature"] = s_temp;
sources["humidity-relative"] = s_humi;
/* Create the json object */
nlohmann::json j_sources = sources;
/* Serialize/deserialize the container */
std::map<std::string, source> clone_sources = j_sources;
ASSERT_EQ(sources, j_sources);
}
/* Currently no need for a custom main (use gtest's)
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
*/