MsgPackParserExample.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to generate a JSON document with ArduinoJson.
  6. #include <iostream>
  7. #include "ArduinoJson.h"
  8. int main() {
  9. // Allocate the JSON document
  10. JsonDocument doc;
  11. // The MessagePack input string
  12. uint8_t input[] = {131, 166, 115, 101, 110, 115, 111, 114, 163, 103, 112, 115,
  13. 164, 116, 105, 109, 101, 206, 80, 147, 50, 248, 164, 100,
  14. 97, 116, 97, 146, 203, 64, 72, 96, 199, 58, 188, 148,
  15. 112, 203, 64, 2, 106, 146, 230, 33, 49, 169};
  16. // This MessagePack document contains:
  17. // {
  18. // "sensor": "gps",
  19. // "time": 1351824120,
  20. // "data": [48.75608, 2.302038]
  21. // }
  22. // Parse the input
  23. DeserializationError error = deserializeMsgPack(doc, input);
  24. // Test if parsing succeeds
  25. if (error) {
  26. std::cerr << "deserializeMsgPack() failed: " << error.c_str() << std::endl;
  27. return 1;
  28. }
  29. // Fetch the values
  30. //
  31. // Most of the time, you can rely on the implicit casts.
  32. // In other case, you can do doc["time"].as<long>();
  33. const char* sensor = doc["sensor"];
  34. long time = doc["time"];
  35. double latitude = doc["data"][0];
  36. double longitude = doc["data"][1];
  37. // Print the values
  38. std::cout << sensor << std::endl;
  39. std::cout << time << std::endl;
  40. std::cout << latitude << std::endl;
  41. std::cout << longitude << std::endl;
  42. return 0;
  43. }