input_types.cpp 2.8 KB

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