Files
ArduinoJson/src/Internals/JsonParser.cpp

224 lines
3.9 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 == '[';
}
2014-10-15 23:27:38 +02:00
bool JsonParser::isBoolean()
{
return *_ptr == 't' || *_ptr == 'f';
}
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-21 23:37:17 +02:00
bool JsonParser::isObjectStart()
{
return *_ptr == '{';
}
2014-10-22 12:25:41 +02:00
void JsonParser::skipSpaces()
2014-10-15 23:10:52 +02:00
{
2014-10-22 12:25:41 +02:00
while(isspace(*_ptr)) _ptr++;
2014-10-15 23:10:52 +02:00
}
2014-10-22 12:25:41 +02:00
bool JsonParser::skip(char charToSkip)
2014-10-14 21:24:26 +02:00
{
2014-10-22 12:25:41 +02:00
skipSpaces();
if (*_ptr != charToSkip) return false;
2014-10-14 21:24:26 +02:00
_ptr++;
2014-10-22 12:25:41 +02:00
skipSpaces();
return true;
2014-10-14 21:24:26 +02:00
}
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-22 12:25:41 +02:00
skip('[');
2014-10-14 21:48:22 +02:00
if (isEnd())
return 0;
2014-10-14 21:24:26 +02:00
2014-10-22 12:25:41 +02:00
if (skip(']'))
return node; // empty array
2014-10-14 21:24:26 +02:00
2014-10-14 21:48:22 +02:00
for(;;)
{
2014-10-22 12:25:41 +02:00
JsonNode* child = parseAnything();
2014-10-14 21:48:22 +02:00
2014-10-22 12:25:41 +02:00
if (!child)
return 0; // child parsing failed
2014-10-14 21:48:22 +02:00
2014-10-22 12:25:41 +02:00
node->addChild(child);
2014-10-14 21:48:22 +02:00
2014-10-22 12:25:41 +02:00
if (skip(']'))
return node; // end of the array
2014-10-14 21:48:22 +02:00
2014-10-22 12:25:41 +02:00
if (!skip(','))
return 0; // comma is missing
2014-10-14 21:48:22 +02:00
}
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()
{
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
2014-10-21 23:37:17 +02:00
JsonNode* JsonParser::parseObject()
{
JsonNode* node = _buffer->createObjectNode();
2014-10-22 12:25:41 +02:00
skip('{');
2014-10-21 23:37:17 +02:00
if (isEnd())
2014-10-22 12:25:41 +02:00
return 0; // premature ending
2014-10-21 23:37:17 +02:00
2014-10-22 12:25:41 +02:00
if (skip('}'))
return node; // empty object
2014-10-21 23:37:17 +02:00
for(;;)
{
2014-10-22 12:25:41 +02:00
JsonNode* child = parseObjectKeyValue();
2014-10-22 10:55:36 +02:00
2014-10-22 12:25:41 +02:00
if (!child)
return 0; // child parsing failed
2014-10-21 23:37:17 +02:00
2014-10-22 12:25:41 +02:00
node->addChild(child);
2014-10-21 23:37:17 +02:00
2014-10-22 12:25:41 +02:00
if (skip('}'))
return node; // end of the object
2014-10-21 23:37:17 +02:00
2014-10-22 12:25:41 +02:00
if (!skip(','))
return 0; // comma is missing
2014-10-21 23:37:17 +02:00
}
}
JsonNode* JsonParser::parseObjectKeyValue()
{
const char* key = QuotedString::extractFrom(_ptr, &_ptr);
2014-10-22 10:55:36 +02:00
if (!key)
2014-10-22 12:25:41 +02:00
return 0; // failed to extract key
2014-10-21 23:37:17 +02:00
2014-10-22 12:25:41 +02:00
if (!skip(':'))
return 0; // colon is missing
2014-10-21 23:37:17 +02:00
JsonNode* value = parseAnything();
2014-10-22 12:25:41 +02:00
if (!value)
return 0; // value parsing failed
2014-10-21 23:37:17 +02:00
return _buffer->createObjectKeyValueNode(key, value);
}
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);
}