test_json.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <string>
  2. #include <nlohmann_json/json.hpp>
  3. #include <unity.h>
  4. using std::string;
  5. TEST_CASE("nlohmann JSON", "[lib][json]") {
  6. nlohmann::json json;
  7. json["foo"] = 123;
  8. json["nested"]["magic"] = 1337;
  9. json["nested"]["amazing"] = true;
  10. TEST_ASSERT_EQUAL_STRING("{\"foo\":123,\"nested\":{\"amazing\":true,\"magic\":1337}}",
  11. json.dump().c_str());
  12. nlohmann::json noway = {
  13. {"pi", 3.141},
  14. {"happy", true},
  15. {"name", "Niels"},
  16. {"nothing", nullptr},
  17. {"answer", {
  18. {"everything", 42}
  19. }},
  20. {"list", {1, 0, 2}},
  21. {"object", {
  22. {"currency", "USD"},
  23. {"value", 42.99}
  24. }}
  25. };
  26. TEST_ASSERT_EQUAL_STRING(
  27. "{\"answer\":{\"everything\":42},\"happy\":true,\"list\":[1,0,2],\"name\":\"Niels\",\"nothing\":null,\"object\":{\"currency\":\"USD\",\"value\":42.99},\"pi\":3.141}",
  28. noway.dump().c_str()
  29. );
  30. string str = R"({"key":"value"})";
  31. auto parsed = nlohmann::json::parse(str);
  32. TEST_ASSERT_EQUAL_STRING("value", parsed.find("key")->get<string>().c_str());
  33. }