subscript.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. #include "Literals.hpp"
  8. TEST_CASE("JsonObjectConst::operator[]") {
  9. JsonDocument doc;
  10. doc["hello"] = "world";
  11. doc["a\0b"_s] = "ABC";
  12. JsonObjectConst obj = doc.as<JsonObjectConst>();
  13. SECTION("supports const char*") {
  14. REQUIRE(obj["hello"] == "world"); // issue #2019
  15. }
  16. SECTION("supports std::string") {
  17. REQUIRE(obj["hello"_s] == "world"); // issue #2019
  18. REQUIRE(obj["a\0b"_s] == "ABC");
  19. }
  20. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  21. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  22. SECTION("supports VLA") {
  23. size_t i = 16;
  24. char vla[i];
  25. strcpy(vla, "hello");
  26. REQUIRE(obj[vla] == "world"_s);
  27. }
  28. #endif
  29. SECTION("supports JsonVariant") {
  30. doc["key1"] = "hello";
  31. doc["key2"] = "a\0b"_s;
  32. doc["key3"] = "foo";
  33. REQUIRE(obj[obj["key1"]] == "world");
  34. REQUIRE(obj[obj["key2"]] == "ABC");
  35. REQUIRE(obj[obj["key3"]] == nullptr);
  36. REQUIRE(obj[obj["key4"]] == nullptr);
  37. }
  38. }