2014-07-16 13:26:11 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h> // for strtol, strtod
|
2014-07-21 10:52:35 +02:00
|
|
|
#include <string.h> // for strcmp()
|
2014-07-16 13:26:11 +02:00
|
|
|
#include "JsonArray.h"
|
2014-07-18 16:46:01 +02:00
|
|
|
#include "JsonObject.h"
|
2014-07-16 13:26:11 +02:00
|
|
|
#include "JsonValue.h"
|
|
|
|
|
|
|
|
using namespace ArduinoJson::Parser;
|
|
|
|
|
2014-07-22 20:28:59 +02:00
|
|
|
// Convert the JsonValue to a bool.
|
|
|
|
// Returns false if the JsonValue is not a primitve.
|
2014-07-16 13:26:11 +02:00
|
|
|
JsonValue::operator bool()
|
|
|
|
{
|
2014-07-19 12:44:27 +02:00
|
|
|
if (!isPrimitive()) return 0;
|
2014-07-18 15:43:20 +02:00
|
|
|
|
2014-07-19 12:44:27 +02:00
|
|
|
char *text = getText();
|
2014-07-16 13:26:11 +02:00
|
|
|
|
|
|
|
// "true"
|
2014-07-18 15:43:20 +02:00
|
|
|
if (text[0] == 't') return true;
|
2014-07-16 13:26:11 +02:00
|
|
|
|
|
|
|
// "false"
|
2014-07-18 15:43:20 +02:00
|
|
|
if (text[0] == 'f') return false;
|
2014-07-16 13:26:11 +02:00
|
|
|
|
|
|
|
// "null"
|
2014-07-18 15:43:20 +02:00
|
|
|
if (text[0] == 'n') return false;
|
2014-07-16 13:26:11 +02:00
|
|
|
|
|
|
|
// number
|
2014-07-18 15:43:20 +02:00
|
|
|
return strtol(text, 0, 0) != 0;
|
2014-07-16 13:26:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 20:28:59 +02:00
|
|
|
// Convert the JsonValue to a floating point value.
|
|
|
|
// Returns false if the JsonValue is not a number.
|
2014-07-16 13:26:11 +02:00
|
|
|
JsonValue::operator double()
|
|
|
|
{
|
2014-07-19 12:44:27 +02:00
|
|
|
return isPrimitive() ? strtod(getText(), 0) : 0;
|
2014-07-16 13:26:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 20:28:59 +02:00
|
|
|
// Convert the JsonValue to a floating point value.
|
|
|
|
// Returns false if the JsonValue is not a number.
|
2014-07-16 13:26:11 +02:00
|
|
|
JsonValue::operator long()
|
|
|
|
{
|
2014-07-19 12:44:27 +02:00
|
|
|
return isPrimitive() ? strtol(getText(), 0, 0) : 0;
|
2014-07-16 13:26:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 20:28:59 +02:00
|
|
|
// Convert the JsonValue to a string.
|
|
|
|
// Returns 0 if the JsonValue is not a string.
|
2014-07-16 13:26:11 +02:00
|
|
|
JsonValue::operator char*()
|
|
|
|
{
|
2014-07-19 12:44:27 +02:00
|
|
|
return isString() || isPrimitive() ? getText() : 0;
|
2014-07-21 10:52:35 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 20:28:59 +02:00
|
|
|
// Get the nested value at the specified index.
|
|
|
|
// Returns an invalid JsonValue if the current value is not an array.
|
|
|
|
JsonValue JsonValue::operator[](int index)
|
|
|
|
{
|
|
|
|
// sanity check
|
|
|
|
if (index < 0 || !isArray() || index >= childrenCount())
|
|
|
|
return null();
|
|
|
|
|
|
|
|
// skip first token, it's the whole object
|
|
|
|
JsonToken runningToken = firstChild();
|
|
|
|
|
|
|
|
// skip all tokens before the specified index
|
|
|
|
for (int i = 0; i < index; i++)
|
|
|
|
{
|
|
|
|
// move forward: current + nested tokens
|
|
|
|
runningToken = runningToken.nextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return runningToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the nested value matching the specified index.
|
|
|
|
// Returns an invalid JsonValue if the current value is not an object.
|
2014-07-21 10:52:35 +02:00
|
|
|
JsonValue JsonValue::operator[](const char* desiredKey)
|
|
|
|
{
|
|
|
|
// sanity check
|
|
|
|
if (desiredKey == 0 || !isObject())
|
|
|
|
return null();
|
|
|
|
|
|
|
|
// skip first token, it's the whole object
|
|
|
|
JsonToken runningToken = firstChild();
|
|
|
|
|
|
|
|
// scan each keys
|
|
|
|
for (int i = 0; i < childrenCount() / 2; i++)
|
|
|
|
{
|
|
|
|
// get 'key' token string
|
|
|
|
char* key = runningToken.getText();
|
|
|
|
|
|
|
|
// move to the 'value' token
|
|
|
|
runningToken = runningToken.nextSibling();
|
|
|
|
|
|
|
|
// compare with desired name
|
|
|
|
if (strcmp(desiredKey, key) == 0)
|
|
|
|
{
|
|
|
|
// return the value token that follows the key token
|
|
|
|
return runningToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip nested tokens
|
|
|
|
runningToken = runningToken.nextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing found, return NULL
|
|
|
|
return null();
|
|
|
|
}
|