Files
ArduinoJson/JsonObjectBase.cpp

65 lines
1.3 KiB
C++
Raw Normal View History

2014-01-11 15:05:35 +01:00
/*
* malloc-free JSON parser for Arduino
* Benoit Blanchon 2014
* MIT License
*/
#include "JsonObjectBase.h"
#include <stdlib.h> // for strtol, strtod
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
2014-01-11 15:05:35 +01:00
{
int count = 0;
2014-02-27 13:30:03 +01:00
jsmntok_t* nextSibling = token + 1;
2014-01-11 15:05:35 +01:00
2014-02-27 13:30:03 +01:00
while (nextSibling->start < token->end)
2014-01-11 15:05:35 +01:00
{
2014-02-27 13:30:03 +01:00
nextSibling++;
count++;
2014-01-11 15:05:35 +01:00
}
return count;
}
bool JsonObjectBase::getBoolFromToken(jsmntok_t* token)
{
if (token->type != JSMN_PRIMITIVE) return 0;
// "true"
if (json[token->start] == 't') return true;
// "false"
if (json[token->start] == 'f') return false;
// "null"
if (json[token->start] == 'n') return false;
// number
return strtol(json + token->start, 0, 0) != 0;
}
double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
{
2014-01-15 13:47:06 +01:00
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
return strtod(json + token->start, 0);
}
long JsonObjectBase::getLongFromToken(jsmntok_t* token)
{
2014-01-15 13:47:06 +01:00
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
return strtol(json + token->start, 0, 0);
}
char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
{
2014-01-15 13:47:06 +01:00
if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
return 0;
// add null terminator to the string
json[token->end] = 0;
return json + token->start;
2014-01-11 15:05:35 +01:00
}