Advertisement
Guest User

Untitled

a guest
Feb 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <Arduino.h>
  2. // This is a sample file from the book "Mastering ArduinoJson"
  3. // https://arduinojson.org/book/
  4. // Copyright 2017-2018 Benoit Blanchon
  5. //
  6. // This example shows how to recursively analyze the content of an unknown
  7. // JSON document.
  8. //
  9. // Paste a JSON document in the Serial Monitor, and the program will display
  10. // everything it knows about this document
  11.  
  12. #include <ArduinoJson.h>
  13.  
  14. void indent(int nesting) {
  15. for (int i = 0; i < nesting; i++)
  16. Serial.print(" ");
  17. }
  18.  
  19. void dump(const JsonVariant &variant, int nesting) {
  20. if (variant.is<bool>())
  21. dump(variant.as<bool>(), nesting);
  22. else if (variant.is<long>())
  23. dump(variant.as<long>(), nesting);
  24. else if (variant.is<double>())
  25. dump(variant.as<double>(), nesting);
  26. else if (variant.is<char *>())
  27. dump(variant.as<char *>(), nesting);
  28. else if (variant.is<JsonObject>())
  29. dump(variant.as<JsonObject>(), nesting);
  30. else if (variant.is<JsonArray>())
  31. dump(variant.as<JsonArray>(), nesting);
  32. else {
  33. indent(nesting);
  34. Serial.println(F("Undefined"));
  35. }
  36. }
  37.  
  38. void dump(bool value, int nesting) {
  39. indent(nesting);
  40. Serial.print(F("Bool: "));
  41. Serial.print(value);
  42. Serial.println('"');
  43. }
  44.  
  45. void dump(long value, int nesting) {
  46. indent(nesting);
  47. Serial.print(F("Integer: "));
  48. Serial.println(value);
  49. }
  50.  
  51. void dump(double value, int nesting) {
  52. indent(nesting);
  53. Serial.print(F("Float: "));
  54. Serial.println(value);
  55. }
  56.  
  57. void dump(const char *str, int nesting) {
  58. indent(nesting);
  59. Serial.print(F("String: "));
  60. Serial.print(str);
  61. Serial.println('"');
  62. }
  63.  
  64. void dump(const JsonObject &obj, int nesting) {
  65. indent(nesting);
  66. Serial.print(F("Object (size = "));
  67. Serial.print(obj.size());
  68. Serial.println("):");
  69.  
  70. // Iterate though all key-value pairs
  71. for (JsonPair kvp : obj) {
  72. // Print the key
  73. indent(nesting + 1);
  74. Serial.print("[\"");
  75. Serial.print(kvp.key);
  76. Serial.println("\"]");
  77.  
  78. // Print the value
  79. dump(kvp.value, nesting + 2); // <- RECURSION
  80. }
  81. }
  82.  
  83. void dump(const JsonArray &arr, int nesting) {
  84. indent(nesting);
  85. Serial.print(F("Array (size = "));
  86. Serial.print(arr.size());
  87. Serial.println(")");
  88.  
  89. int index = 0;
  90. // Iterate though all elements
  91. for (JsonVariant value : arr) {
  92. // Print the index
  93. indent(nesting + 1);
  94. Serial.print("[");
  95. Serial.print(index);
  96. Serial.println("]");
  97.  
  98. // Print the value
  99. dump(value, nesting + 2); // <- RECURSION
  100.  
  101. index++;
  102. }
  103. }
  104.  
  105. void setup() {
  106. Serial.begin(9600);
  107. while (!Serial)
  108. continue;
  109.  
  110. Serial.setTimeout(5000);
  111. }
  112.  
  113. void loop() {
  114. Serial.println(F("Flushing Serial input buffer..."));
  115. while (Serial.available())
  116. Serial.read();
  117.  
  118. Serial.println(F("Waiting for a JSON document in Serial..."));
  119. while (!Serial.available())
  120. delay(50);
  121.  
  122. Serial.println(F("Parsing..."));
  123. DynamicJsonBuffer jb;
  124. JsonVariant root = jb.parse(Serial);
  125.  
  126. if (root.success()) {
  127. Serial.println(F("Parsing... done!"));
  128. dump(root, 0);
  129. } else {
  130. Serial.println(F("Parsing... failed!"));
  131. }
  132.  
  133. Serial.println();
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement