123456789101112131415161718192021222324252627282930313233343536373839 |
- #include <string>
- #include <nlohmann_json/json.hpp>
- #include <unity.h>
- using std::string;
- TEST_CASE("nlohmann JSON", "[lib][json]") {
- nlohmann::json json;
- json["foo"] = 123;
- json["nested"]["magic"] = 1337;
- json["nested"]["amazing"] = true;
- TEST_ASSERT_EQUAL_STRING("{\"foo\":123,\"nested\":{\"amazing\":true,\"magic\":1337}}",
- json.dump().c_str());
- nlohmann::json noway = {
- {"pi", 3.141},
- {"happy", true},
- {"name", "Niels"},
- {"nothing", nullptr},
- {"answer", {
- {"everything", 42}
- }},
- {"list", {1, 0, 2}},
- {"object", {
- {"currency", "USD"},
- {"value", 42.99}
- }}
- };
- TEST_ASSERT_EQUAL_STRING(
- "{\"answer\":{\"everything\":42},\"happy\":true,\"list\":[1,0,2],\"name\":\"Niels\",\"nothing\":null,\"object\":{\"currency\":\"USD\",\"value\":42.99},\"pi\":3.141}",
- noway.dump().c_str()
- );
- string str = R"({"key":"value"})";
- auto parsed = nlohmann::json::parse(str);
- TEST_ASSERT_EQUAL_STRING("value", parsed.find("key")->get<string>().c_str());
- }
|