2014-10-14 21:24:26 +02:00
|
|
|
#include "JsonParser.h"
|
|
|
|
#include "../JsonBuffer.h"
|
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>
|
|
|
|
|
|
|
|
bool JsonParser::isArrayStart()
|
|
|
|
{
|
|
|
|
return *_ptr == '[';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::isArrayStop()
|
|
|
|
{
|
|
|
|
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-14 21:24:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
_ptr += 4;
|
|
|
|
// 4 = strlen("null")
|
|
|
|
|
|
|
|
return _buffer->createStringNode(0);
|
2014-10-15 23:10:52 +02:00
|
|
|
}
|