JsonObjectBase.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include <stdlib.h> // for strtol, strtod
  6. #include "JsonObjectBase.h"
  7. using namespace ArduinoJson::Parser;
  8. int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
  9. {
  10. int end = token->end;
  11. int count = 0;
  12. token++;
  13. while (token->start < end)
  14. {
  15. token++;
  16. count++;
  17. }
  18. return count;
  19. }
  20. bool JsonObjectBase::getBoolFromToken(jsmntok_t* token)
  21. {
  22. if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
  23. // "true"
  24. if (json[token->start] == 't') return true;
  25. // "false"
  26. if (json[token->start] == 'f') return false;
  27. // "null"
  28. if (json[token->start] == 'n') return false;
  29. // number
  30. return strtol(json + token->start, 0, 0) != 0;
  31. }
  32. double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
  33. {
  34. if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
  35. return strtod(json + token->start, 0);
  36. }
  37. long JsonObjectBase::getLongFromToken(jsmntok_t* token)
  38. {
  39. if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
  40. return strtol(json + token->start, 0, 0);
  41. }
  42. char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
  43. {
  44. if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
  45. return 0;
  46. // add null terminator to the string
  47. json[token->end] = 0;
  48. return json + token->start;
  49. }