input_types.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <sstream>
  7. #include "CustomReader.hpp"
  8. TEST_CASE("deserializeJson(const std::string&)") {
  9. DynamicJsonDocument doc(4096);
  10. SECTION("should accept const string") {
  11. const std::string input("[42]");
  12. DeserializationError err = deserializeJson(doc, input);
  13. REQUIRE(err == DeserializationError::Ok);
  14. }
  15. SECTION("should accept temporary string") {
  16. DeserializationError err = deserializeJson(doc, std::string("[42]"));
  17. REQUIRE(err == DeserializationError::Ok);
  18. }
  19. SECTION("should duplicate content") {
  20. std::string input("[\"hello\"]");
  21. DeserializationError err = deserializeJson(doc, input);
  22. input[2] = 'X'; // alter the string tomake sure we made a copy
  23. JsonArray array = doc.as<JsonArray>();
  24. REQUIRE(err == DeserializationError::Ok);
  25. REQUIRE(std::string("hello") == array[0]);
  26. }
  27. }
  28. TEST_CASE("deserializeJson(std::istream&)") {
  29. DynamicJsonDocument doc(4096);
  30. SECTION("array") {
  31. std::istringstream json(" [ 42 /* comment */ ] ");
  32. DeserializationError err = deserializeJson(doc, json);
  33. JsonArray arr = doc.as<JsonArray>();
  34. REQUIRE(err == DeserializationError::Ok);
  35. REQUIRE(1 == arr.size());
  36. REQUIRE(42 == arr[0]);
  37. }
  38. SECTION("object") {
  39. std::istringstream json(" { hello : 'world' // comment\n }");
  40. DeserializationError err = deserializeJson(doc, json);
  41. JsonObject obj = doc.as<JsonObject>();
  42. REQUIRE(err == DeserializationError::Ok);
  43. REQUIRE(1 == obj.size());
  44. REQUIRE(std::string("world") == obj["hello"]);
  45. }
  46. SECTION("Should not read after the closing brace of an empty object") {
  47. std::istringstream json("{}123");
  48. deserializeJson(doc, json);
  49. REQUIRE('1' == char(json.get()));
  50. }
  51. SECTION("Should not read after the closing brace") {
  52. std::istringstream json("{\"hello\":\"world\"}123");
  53. deserializeJson(doc, json);
  54. REQUIRE('1' == char(json.get()));
  55. }
  56. SECTION("Should not read after the closing bracket of an empty array") {
  57. std::istringstream json("[]123");
  58. deserializeJson(doc, json);
  59. REQUIRE('1' == char(json.get()));
  60. }
  61. SECTION("Should not read after the closing bracket") {
  62. std::istringstream json("[\"hello\",\"world\"]123");
  63. deserializeJson(doc, json);
  64. REQUIRE('1' == char(json.get()));
  65. }
  66. SECTION("Should not read after the closing quote") {
  67. std::istringstream json("\"hello\"123");
  68. deserializeJson(doc, json);
  69. REQUIRE('1' == char(json.get()));
  70. }
  71. }
  72. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  73. TEST_CASE("deserializeJson(VLA)") {
  74. int i = 9;
  75. char vla[i];
  76. strcpy(vla, "{\"a\":42}");
  77. StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
  78. DeserializationError err = deserializeJson(doc, vla);
  79. REQUIRE(err == DeserializationError::Ok);
  80. }
  81. #endif
  82. TEST_CASE("deserializeJson(CustomReader)") {
  83. DynamicJsonDocument doc(4096);
  84. CustomReader reader("[4,2]");
  85. DeserializationError err = deserializeJson(doc, reader);
  86. REQUIRE(err == DeserializationError::Ok);
  87. REQUIRE(doc.size() == 2);
  88. REQUIRE(doc[0] == 4);
  89. REQUIRE(doc[1] == 2);
  90. }