Files
ArduinoJson/JsonParser/JsonValue.cpp

70 lines
1.4 KiB
C++
Raw Normal View History

/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include <stdlib.h> // for strtol, strtod
#include "JsonArray.h"
2014-07-18 16:46:01 +02:00
#include "JsonObject.h"
#include "JsonValue.h"
using namespace ArduinoJson::Parser;
2014-07-18 15:43:20 +02:00
using namespace ArduinoJson::Internal;
JsonValue JsonValue::operator[](int index)
{
return JsonArray(json, token)[index];
}
JsonValue JsonValue::operator[](const char* key)
{
2014-07-18 16:46:01 +02:00
return JsonObject(json, token)[key];
}
JsonValue::operator bool()
{
2014-07-18 15:43:20 +02:00
if (!token.isPrimitive()) return 0;
char *text = token.getText(json);
// "true"
2014-07-18 15:43:20 +02:00
if (text[0] == 't') return true;
// "false"
2014-07-18 15:43:20 +02:00
if (text[0] == 'f') return false;
// "null"
2014-07-18 15:43:20 +02:00
if (text[0] == 'n') return false;
// number
2014-07-18 15:43:20 +02:00
return strtol(text, 0, 0) != 0;
}
JsonValue::operator double()
{
2014-07-18 15:43:20 +02:00
return token.isPrimitive() ? strtod(token.getText(json), 0) : 0;
}
JsonValue::operator long()
{
2014-07-18 15:43:20 +02:00
return token.isPrimitive() ? strtol(token.getText(json), 0, 0) : 0;
}
JsonValue::operator char*()
{
2014-07-18 15:43:20 +02:00
return token.isString() || token.isPrimitive() ? token.getText(json) : 0;
}
JsonValue::operator JsonArray()
{
2014-07-18 15:43:20 +02:00
return token.isArray()
? JsonArray(json, token)
: JsonArray::null();
}
2014-07-18 16:46:01 +02:00
JsonValue::operator JsonObject()
{
2014-07-18 15:43:20 +02:00
return token.isObject()
2014-07-18 16:46:01 +02:00
? JsonObject(json, token)
: JsonObject::null();
}