JsonObjectBase.cpp 766 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * malloc-free JSON parser for Arduino
  3. * Benoit Blanchon 2014
  4. * MIT License
  5. */
  6. #include "JsonObjectBase.h"
  7. #include <stdlib.h> // for strtol
  8. int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
  9. {
  10. int count = 0;
  11. for (int i = 0; i < token->size; i++)
  12. {
  13. count += 1 + getNestedTokenCount(token + 1 + i);
  14. }
  15. return count;
  16. }
  17. long JsonObjectBase::getLongFromToken(jsmntok_t* token)
  18. {
  19. if (token->type != JSMN_PRIMITIVE)
  20. return 0;
  21. return strtol(json + token->start, 0, 0);
  22. }
  23. char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
  24. {
  25. if (token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
  26. return 0;
  27. // add null terminator to the string
  28. json[token->end] = 0;
  29. return json + token->start;
  30. }