std_istream.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(std::istream&)") {
  8. DynamicJsonDocument doc;
  9. SECTION("array") {
  10. std::istringstream json(" [ 42 /* comment */ ] ");
  11. JsonError err = deserializeJson(doc, json);
  12. JsonArray& arr = doc.as<JsonArray>();
  13. REQUIRE(err == JsonError::Ok);
  14. REQUIRE(1 == arr.size());
  15. REQUIRE(42 == arr[0]);
  16. }
  17. SECTION("object") {
  18. std::istringstream json(" { hello : world // comment\n }");
  19. JsonError err = deserializeJson(doc, json);
  20. JsonObject& obj = doc.as<JsonObject>();
  21. REQUIRE(err == JsonError::Ok);
  22. REQUIRE(1 == obj.size());
  23. REQUIRE(std::string("world") == obj["hello"]);
  24. }
  25. SECTION("Should not read after the closing brace of an empty object") {
  26. std::istringstream json("{}123");
  27. deserializeJson(doc, json);
  28. REQUIRE('1' == char(json.get()));
  29. }
  30. SECTION("Should not read after the closing brace") {
  31. std::istringstream json("{\"hello\":\"world\"}123");
  32. deserializeJson(doc, json);
  33. REQUIRE('1' == char(json.get()));
  34. }
  35. SECTION("Should not read after the closing bracket of an empty array") {
  36. std::istringstream json("[]123");
  37. deserializeJson(doc, json);
  38. REQUIRE('1' == char(json.get()));
  39. }
  40. SECTION("Should not read after the closing bracket") {
  41. std::istringstream json("[\"hello\",\"world\"]123");
  42. deserializeJson(doc, json);
  43. REQUIRE('1' == char(json.get()));
  44. }
  45. SECTION("Should not read after the closing quote") {
  46. std::istringstream json("\"hello\"123");
  47. deserializeJson(doc, json);
  48. REQUIRE('1' == char(json.get()));
  49. }
  50. }