JsonToken.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #pragma once
  6. #include "jsmn.h"
  7. namespace ArduinoJson
  8. {
  9. namespace Parser
  10. {
  11. // A pointer to a JSON token
  12. class JsonToken
  13. {
  14. public:
  15. // Create a "null" pointer
  16. JsonToken()
  17. : token(0)
  18. {
  19. }
  20. // Create a pointer to the specified JSON token
  21. JsonToken(char* json, jsmntok_t* token)
  22. : json(json), token(token)
  23. {
  24. }
  25. // Get content of the JSON token
  26. char* getText();
  27. // Get the number of children tokens
  28. int childrenCount()
  29. {
  30. return token->size;
  31. }
  32. // Get a pointer to the first child of the current token
  33. JsonToken firstChild() const
  34. {
  35. return JsonToken(json, token + 1);
  36. }
  37. // Get a pointer to the next sibling token (ie skiping the children tokens)
  38. JsonToken nextSibling() const;
  39. // Test equality
  40. bool operator!=(const JsonToken& other) const
  41. {
  42. return token != other.token;
  43. }
  44. // Tell if the pointer is "null"
  45. bool isValid()
  46. {
  47. return token != 0;
  48. }
  49. // Tell if the JSON token is a JSON object
  50. bool isObject()
  51. {
  52. return token != 0 && token->type == JSMN_OBJECT;
  53. }
  54. // Tell if the JSON token is a JSON array
  55. bool isArray()
  56. {
  57. return token != 0 && token->type == JSMN_ARRAY;
  58. }
  59. // Tell if the JSON token is a primitive
  60. bool isPrimitive()
  61. {
  62. return token != 0 && token->type == JSMN_PRIMITIVE;
  63. }
  64. // Tell if the JSON token is a string
  65. bool isString()
  66. {
  67. return token != 0 && token->type == JSMN_STRING;
  68. }
  69. // Explicit wait to create a "null" JsonToken
  70. static JsonToken null()
  71. {
  72. return JsonToken();
  73. }
  74. private:
  75. char* json;
  76. jsmntok_t* token;
  77. static char unescapeChar(char c);
  78. static void unescapeString(char* s);
  79. };
  80. }
  81. }