Files
ArduinoJson/JsonParser/JsonValue.cpp

65 lines
1.1 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;
JsonValue JsonValue::operator[](int index)
{
2014-07-19 12:44:27 +02:00
return JsonArray(*this)[index];
}
JsonValue JsonValue::operator[](const char* key)
{
2014-07-19 12:44:27 +02:00
return JsonObject(*this)[key];
}
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();
// "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-19 12:44:27 +02:00
return isPrimitive() ? strtod(getText(), 0) : 0;
}
JsonValue::operator long()
{
2014-07-19 12:44:27 +02:00
return isPrimitive() ? strtol(getText(), 0, 0) : 0;
}
JsonValue::operator char*()
{
2014-07-19 12:44:27 +02:00
return isString() || isPrimitive() ? getText() : 0;
}
JsonValue::operator JsonArray()
{
2014-07-19 12:44:27 +02:00
return *this;
}
2014-07-18 16:46:01 +02:00
JsonValue::operator JsonObject()
{
2014-07-19 12:44:27 +02:00
return *this;
}