subscript.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonObject::operator[]") {
  7. DynamicJsonDocument doc;
  8. JsonObject obj = doc.to<JsonObject>();
  9. SECTION("int") {
  10. obj["hello"] = 123;
  11. REQUIRE(123 == obj["hello"].as<int>());
  12. REQUIRE(true == obj["hello"].is<int>());
  13. REQUIRE(false == obj["hello"].is<bool>());
  14. }
  15. SECTION("volatile int") { // issue #415
  16. volatile int i = 123;
  17. obj["hello"] = i;
  18. REQUIRE(123 == obj["hello"].as<int>());
  19. REQUIRE(true == obj["hello"].is<int>());
  20. REQUIRE(false == obj["hello"].is<bool>());
  21. }
  22. SECTION("double") {
  23. obj["hello"] = 123.45;
  24. REQUIRE(true == obj["hello"].is<double>());
  25. REQUIRE(false == obj["hello"].is<long>());
  26. REQUIRE(123.45 == obj["hello"].as<double>());
  27. }
  28. SECTION("bool") {
  29. obj["hello"] = true;
  30. REQUIRE(true == obj["hello"].is<bool>());
  31. REQUIRE(false == obj["hello"].is<long>());
  32. REQUIRE(true == obj["hello"].as<bool>());
  33. }
  34. SECTION("const char*") {
  35. obj["hello"] = "h3110";
  36. REQUIRE(true == obj["hello"].is<const char*>());
  37. REQUIRE(false == obj["hello"].is<long>());
  38. REQUIRE(std::string("h3110") == obj["hello"].as<const char*>());
  39. REQUIRE(std::string("h3110") == obj["hello"].as<char*>()); // <- short hand
  40. }
  41. SECTION("array") {
  42. DynamicJsonDocument doc2;
  43. JsonArray arr = doc2.to<JsonArray>();
  44. obj["hello"] = arr;
  45. REQUIRE(arr == obj["hello"].as<JsonArray>());
  46. REQUIRE(true == obj["hello"].is<JsonArray>());
  47. REQUIRE(false == obj["hello"].is<JsonObject>());
  48. }
  49. SECTION("object") {
  50. DynamicJsonDocument doc2;
  51. JsonObject obj2 = doc2.to<JsonObject>();
  52. obj["hello"] = obj2;
  53. REQUIRE(obj2 == obj["hello"].as<JsonObject>());
  54. REQUIRE(true == obj["hello"].is<JsonObject>());
  55. REQUIRE(false == obj["hello"].is<JsonArray>());
  56. }
  57. SECTION("array subscript") {
  58. DynamicJsonDocument doc2;
  59. JsonArray arr = doc2.to<JsonArray>();
  60. arr.add(42);
  61. obj["a"] = arr[0];
  62. REQUIRE(42 == obj["a"]);
  63. }
  64. SECTION("object subscript") {
  65. DynamicJsonDocument doc2;
  66. JsonObject obj2 = doc2.to<JsonObject>();
  67. obj2["x"] = 42;
  68. obj["a"] = obj2["x"];
  69. REQUIRE(42 == obj["a"]);
  70. }
  71. SECTION("char key[]") { // issue #423
  72. char key[] = "hello";
  73. obj[key] = 42;
  74. REQUIRE(42 == obj[key]);
  75. }
  76. SECTION("should not duplicate const char*") {
  77. obj["hello"] = "world";
  78. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  79. REQUIRE(expectedSize == doc.memoryUsage());
  80. }
  81. SECTION("should duplicate char* value") {
  82. obj["hello"] = const_cast<char*>("world");
  83. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  84. REQUIRE(expectedSize == doc.memoryUsage());
  85. }
  86. SECTION("should duplicate char* key") {
  87. obj[const_cast<char*>("hello")] = "world";
  88. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  89. REQUIRE(expectedSize == doc.memoryUsage());
  90. }
  91. SECTION("should duplicate char* key&value") {
  92. obj[const_cast<char*>("hello")] = const_cast<char*>("world");
  93. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
  94. REQUIRE(expectedSize <= doc.memoryUsage());
  95. }
  96. SECTION("should duplicate std::string value") {
  97. obj["hello"] = std::string("world");
  98. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  99. REQUIRE(expectedSize == doc.memoryUsage());
  100. }
  101. SECTION("should duplicate std::string key") {
  102. obj[std::string("hello")] = "world";
  103. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  104. REQUIRE(expectedSize == doc.memoryUsage());
  105. }
  106. SECTION("should duplicate std::string key&value") {
  107. obj[std::string("hello")] = std::string("world");
  108. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
  109. REQUIRE(expectedSize <= doc.memoryUsage());
  110. }
  111. SECTION("should ignore null key") {
  112. // object must have a value to make a call to strcmp()
  113. obj["dummy"] = 42;
  114. const char* null = 0;
  115. obj[null] = 666;
  116. REQUIRE(obj.size() == 1);
  117. REQUIRE(obj[null] == 0);
  118. }
  119. SECTION("obj[key].to<JsonArray>()") {
  120. JsonArray arr = obj["hello"].to<JsonArray>();
  121. REQUIRE(arr.isNull() == false);
  122. }
  123. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  124. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  125. SECTION("obj[VLA] = str") {
  126. int i = 16;
  127. char vla[i];
  128. strcpy(vla, "hello");
  129. obj[vla] = "world";
  130. REQUIRE(std::string("world") == obj["hello"]);
  131. }
  132. SECTION("obj[str] = VLA") { // issue #416
  133. int i = 32;
  134. char vla[i];
  135. strcpy(vla, "world");
  136. obj["hello"] = vla;
  137. REQUIRE(std::string("world") == obj["hello"].as<char*>());
  138. }
  139. SECTION("obj.set(VLA, str)") {
  140. int i = 16;
  141. char vla[i];
  142. strcpy(vla, "hello");
  143. obj[vla] = "world";
  144. REQUIRE(std::string("world") == obj["hello"]);
  145. }
  146. SECTION("obj.set(str, VLA)") {
  147. int i = 32;
  148. char vla[i];
  149. strcpy(vla, "world");
  150. obj["hello"].set(vla);
  151. REQUIRE(std::string("world") == obj["hello"].as<char*>());
  152. }
  153. SECTION("obj[VLA]") {
  154. int i = 16;
  155. char vla[i];
  156. strcpy(vla, "hello");
  157. deserializeJson(doc, "{\"hello\":\"world\"}");
  158. obj = doc.as<JsonObject>();
  159. REQUIRE(std::string("world") == obj[vla]);
  160. }
  161. #endif
  162. SECTION("chain") {
  163. obj.createNestedObject("hello")["world"] = 123;
  164. REQUIRE(123 == obj["hello"]["world"].as<int>());
  165. REQUIRE(true == obj["hello"]["world"].is<int>());
  166. REQUIRE(false == obj["hello"]["world"].is<bool>());
  167. }
  168. }