2014-10-14 21:24:26 +02:00
|
|
|
#include "JsonParser.h"
|
|
|
|
#include "../JsonBuffer.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
bool JsonParser::isEnd()
|
|
|
|
{
|
|
|
|
return *_ptr == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::isArrayStart()
|
|
|
|
{
|
|
|
|
return *_ptr == '[';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::isArrayStop()
|
|
|
|
{
|
|
|
|
return *_ptr == ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::isLong()
|
|
|
|
{
|
|
|
|
char* ptr = _ptr;
|
|
|
|
|
|
|
|
// skip all digits
|
|
|
|
while (isdigit(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
// same position => 0 digits => not a long
|
|
|
|
if (ptr == _ptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// stopped on a decimal separator => not a long but a double
|
|
|
|
if (*ptr == '.')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::isSpace()
|
|
|
|
{
|
|
|
|
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
|
|
|
|
}
|
|
|
|
|
2014-10-14 21:48:22 +02:00
|
|
|
bool JsonParser::isComma()
|
|
|
|
{
|
|
|
|
return *_ptr == ',';
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (isLong())
|
|
|
|
return parseLong();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode* JsonParser::parseArray()
|
|
|
|
{
|
|
|
|
JsonNode* node = _buffer->createNode();
|
|
|
|
node->setAsArray(_buffer);
|
|
|
|
|
2014-10-14 21:48:22 +02:00
|
|
|
skipOneChar(); // skip the '['
|
|
|
|
skipSpaces();
|
|
|
|
|
2014-10-14 21:35:47 +02:00
|
|
|
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
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode* JsonParser::parseLong()
|
|
|
|
{
|
|
|
|
long value = strtol(_ptr, &_ptr, 10);
|
|
|
|
|
|
|
|
JsonNode* node = _buffer->createNode();
|
|
|
|
node->setAsLong(value);
|
|
|
|
return node;
|
|
|
|
}
|