Files
ArduinoJson/src/Internals/JsonParser.cpp

199 lines
3.2 KiB
C++
Raw Normal View History

#include "ArduinoJson/Internals/JsonParser.hpp"
2014-10-17 19:57:00 +02:00
2014-10-15 14:54:31 +02:00
#include <stdlib.h> // for strtol, strtod
2014-10-14 21:24:26 +02:00
#include <ctype.h>
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/Internals/QuotedString.hpp"
2014-10-17 19:57:00 +02:00
using namespace ArduinoJson::Internals;
2014-10-14 21:24:26 +02:00
bool JsonParser::isArrayStart()
{
return *_ptr == '[';
}
bool JsonParser::isArrayStop()
{
return *_ptr == ']';
}
2014-10-21 21:11:30 +02:00
bool JsonParser::isObjectStart()
{
return *_ptr == '{';
}
bool JsonParser::isObjectStop()
{
return *_ptr == '}';
}
2014-10-15 23:27:38 +02:00
bool JsonParser::isBoolean()
{
return *_ptr == 't' || *_ptr == 'f';
}
bool JsonParser::isComma()
{
return *_ptr == ',';
}
bool JsonParser::isDouble()
2014-10-14 21:24:26 +02:00
{
char* ptr = _ptr;
// skip all digits
while (isdigit(*ptr))
ptr++;
2014-10-15 14:54:31 +02:00
// same position => 0 digits => not a number
2014-10-15 23:27:38 +02:00
if (ptr == _ptr)
2014-10-14 21:24:26 +02:00
return false;
2014-10-15 23:27:38 +02:00
// stopped on a decimal separator => ok it's a double
return *ptr == '.';
2014-10-15 14:54:31 +02:00
}
2014-10-15 23:27:38 +02:00
bool JsonParser::isEnd()
{
return *_ptr == 0;
}
bool JsonParser::isLong()
2014-10-15 14:54:31 +02:00
{
char* ptr = _ptr;
// skip all digits
while (isdigit(*ptr))
ptr++;
// same position => 0 digits => not a number
2014-10-15 23:27:38 +02:00
if (ptr == _ptr)
2014-10-14 21:24:26 +02:00
return false;
2014-10-15 23:27:38 +02:00
// stopped on a decimal separator => not a long but a double
return *ptr != '.';
2014-10-14 21:24:26 +02:00
}
2014-10-15 23:27:38 +02:00
bool JsonParser::isNull()
2014-10-14 21:48:22 +02:00
{
2014-10-15 23:27:38 +02:00
return *_ptr == 'n';
2014-10-14 21:48:22 +02:00
}
2014-10-15 23:27:38 +02:00
bool JsonParser::isSpace()
2014-10-15 23:10:52 +02:00
{
2014-10-15 23:27:38 +02:00
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
2014-10-15 23:10:52 +02:00
}
2014-10-14 21:24:26 +02:00
void JsonParser::skipOneChar()
{
_ptr++;
}
void JsonParser::skipSpaces()
{
while(isSpace()) skipOneChar();
}
2014-10-14 21:48:22 +02:00
JsonNode* JsonParser::parseAnything()
2014-10-14 21:24:26 +02:00
{
skipSpaces();
if (isArrayStart())
return parseArray();
2014-10-15 14:54:31 +02:00
2014-10-15 23:27:38 +02:00
if (isBoolean())
return parseBoolean();
2014-10-15 14:54:31 +02:00
if (isDouble())
return parseDouble();
2014-10-14 21:24:26 +02:00
2014-10-15 23:27:38 +02:00
if (isLong())
return parseLong();
if (isNull())
return parseNull();
2014-10-15 23:10:52 +02:00
2014-10-21 21:11:30 +02:00
if (isObjectStart())
return parseObject();
return parseString();
2014-10-14 21:24:26 +02:00
}
JsonNode* JsonParser::parseArray()
{
2014-10-21 21:11:30 +02:00
JsonNode* node = _buffer->createArrayNode();
2014-10-14 21:24:26 +02:00
2014-10-14 21:48:22 +02:00
skipOneChar(); // skip the '['
skipSpaces();
if (isEnd())
return 0;
2014-10-14 21:24:26 +02:00
if (isArrayStop())
return node;
2014-10-14 21:48:22 +02:00
for(;;)
{
node->addChild(parseAnything());
skipSpaces();
if (isArrayStop())
return node;
if (!isComma())
return 0;
skipOneChar(); // skip the ','
}
2014-10-14 21:24:26 +02:00
}
2014-10-21 21:11:30 +02:00
JsonNode* JsonParser::parseObject()
{
_ptr+=2;
return _buffer->createObjectNode();
}
2014-10-15 23:27:38 +02:00
JsonNode *JsonParser::parseBoolean()
2014-10-14 21:24:26 +02:00
{
2014-10-15 23:27:38 +02:00
bool value = *_ptr == 't';
2014-10-14 21:24:26 +02:00
2014-10-15 23:27:38 +02:00
_ptr += value ? 4 : 5;
// 4 = strlen("true")
// 5 = strlen("false");
return _buffer->createBoolNode(value);
2014-10-15 14:54:31 +02:00
}
JsonNode *JsonParser::parseDouble()
{
double value = strtod(_ptr, &_ptr);
int decimals = 0;
while(_ptr[1-decimals] != '.')
decimals++;
return _buffer->createDoubleNode(value, decimals);
}
2014-10-15 23:10:52 +02:00
2014-10-15 23:27:38 +02:00
JsonNode* JsonParser::parseLong()
2014-10-15 23:10:52 +02:00
{
2014-10-15 23:27:38 +02:00
long value = strtol(_ptr, &_ptr, 10);
2014-10-15 23:10:52 +02:00
2014-10-15 23:27:38 +02:00
return _buffer->createLongNode(value);
}
2014-10-15 23:10:52 +02:00
2014-10-15 23:27:38 +02:00
JsonNode* JsonParser::parseNull()
{
2014-10-21 21:11:30 +02:00
_ptr += 4; // strlen("null")
2014-10-15 23:27:38 +02:00
return _buffer->createStringNode(0);
2014-10-15 23:10:52 +02:00
}
2014-10-15 23:39:25 +02:00
JsonNode* JsonParser::parseString()
{
const char* s = QuotedString::extractFrom(_ptr, &_ptr);
2014-10-15 23:39:25 +02:00
return _buffer->createStringNode(s);
}