JsonObjectBase.cpp 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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, strtod
  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. double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
  18. {
  19. if (token->type != JSMN_PRIMITIVE) return 0;
  20. return strtod(json + token->start, 0);
  21. }
  22. long JsonObjectBase::getLongFromToken(jsmntok_t* token)
  23. {
  24. if (token->type != JSMN_PRIMITIVE) return 0;
  25. return strtol(json + token->start, 0, 0);
  26. }
  27. char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
  28. {
  29. if (token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
  30. return 0;
  31. // add null terminator to the string
  32. json[token->end] = 0;
  33. return json + token->start;
  34. }