Simplified the parser

This commit is contained in:
Benoit Blanchon
2014-10-22 12:25:41 +02:00
parent 743381de6d
commit 5aefc7d652
2 changed files with 42 additions and 81 deletions

View File

@ -26,23 +26,18 @@ namespace ArduinoJson
char* _ptr; char* _ptr;
inline bool isArrayStart(); inline bool isArrayStart();
inline bool isArrayStop();
inline bool isBoolean(); inline bool isBoolean();
inline bool isComma();
inline bool isColon();
inline bool isDouble(); inline bool isDouble();
inline bool isEnd(); inline bool isEnd();
inline bool isLong(); inline bool isLong();
inline bool isNull(); inline bool isNull();
inline bool isObjectStop();
inline bool isObjectStart(); inline bool isObjectStart();
inline bool isSpace();
inline void skipOneChar(); bool skip(char charToSkip);
inline void skipSpaces(); void skipSpaces();
inline JsonNode* parseArray(); inline JsonNode* parseArray();
inline JsonNode* parseBoolean(); inline JsonNode* parseBoolean();
inline JsonNode *parseDouble(); inline JsonNode *parseDouble();
inline JsonNode* parseObjectKeyValue(); inline JsonNode* parseObjectKeyValue();
inline JsonNode* parseLong(); inline JsonNode* parseLong();

View File

@ -13,26 +13,11 @@ bool JsonParser::isArrayStart()
return *_ptr == '['; return *_ptr == '[';
} }
bool JsonParser::isArrayStop()
{
return *_ptr == ']';
}
bool JsonParser::isBoolean() bool JsonParser::isBoolean()
{ {
return *_ptr == 't' || *_ptr == 'f'; return *_ptr == 't' || *_ptr == 'f';
} }
bool JsonParser::isComma()
{
return *_ptr == ',';
}
bool JsonParser::isColon()
{
return *_ptr == ':';
}
bool JsonParser::isDouble() bool JsonParser::isDouble()
{ {
char* ptr = _ptr; char* ptr = _ptr;
@ -80,24 +65,18 @@ bool JsonParser::isObjectStart()
return *_ptr == '{'; return *_ptr == '{';
} }
bool JsonParser::isObjectStop()
{
return *_ptr == '}';
}
bool JsonParser::isSpace()
{
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
}
void JsonParser::skipOneChar()
{
_ptr++;
}
void JsonParser::skipSpaces() void JsonParser::skipSpaces()
{ {
while(isSpace()) skipOneChar(); while(isspace(*_ptr)) _ptr++;
}
bool JsonParser::skip(char charToSkip)
{
skipSpaces();
if (*_ptr != charToSkip) return false;
_ptr++;
skipSpaces();
return true;
} }
JsonNode* JsonParser::parseAnything() JsonNode* JsonParser::parseAnything()
@ -129,31 +108,28 @@ JsonNode* JsonParser::parseArray()
{ {
JsonNode* node = _buffer->createArrayNode(); JsonNode* node = _buffer->createArrayNode();
skipOneChar(); // skip the '[' skip('[');
skipSpaces();
if (isEnd()) if (isEnd())
return 0; return 0;
if (isArrayStop()) if (skip(']'))
{ return node; // empty array
skipOneChar(); // skip the ']'
return node;
}
for(;;) for(;;)
{ {
node->addChild(parseAnything()); JsonNode* child = parseAnything();
skipSpaces(); if (!child)
return 0; // child parsing failed
if (isArrayStop()) node->addChild(child);
return node;
if (!isComma()) if (skip(']'))
return 0; return node; // end of the array
skipOneChar(); // skip the ',' if (!skip(','))
return 0; // comma is missing
} }
} }
@ -197,36 +173,28 @@ JsonNode* JsonParser::parseObject()
{ {
JsonNode* node = _buffer->createObjectNode(); JsonNode* node = _buffer->createObjectNode();
skipOneChar(); // skip the '{' skip('{');
skipSpaces();
if (isEnd()) if (isEnd())
return 0; return 0; // premature ending
if (isObjectStop()) if (skip('}'))
{ return node; // empty object
skipOneChar(); // skip the '}'
return node;
}
for(;;) for(;;)
{ {
JsonNode* keyValueNode = parseObjectKeyValue(); JsonNode* child = parseObjectKeyValue();
if (!keyValueNode) if (!child)
return 0; return 0; // child parsing failed
node->addChild(keyValueNode); node->addChild(child);
skipSpaces(); if (skip('}'))
return node; // end of the object
if (isObjectStop()) if (!skip(','))
return node; return 0; // comma is missing
if (!isComma())
return 0;
skipOneChar(); // skip the ','
} }
} }
@ -235,18 +203,16 @@ JsonNode* JsonParser::parseObjectKeyValue()
const char* key = QuotedString::extractFrom(_ptr, &_ptr); const char* key = QuotedString::extractFrom(_ptr, &_ptr);
if (!key) if (!key)
return 0; return 0; // failed to extract key
skipSpaces(); if (!skip(':'))
return 0; // colon is missing
if (!isColon())
return 0;
skipOneChar(); // skip the :
skipSpaces();
JsonNode* value = parseAnything(); JsonNode* value = parseAnything();
if (!value)
return 0; // value parsing failed
return _buffer->createObjectKeyValueNode(key, value); return _buffer->createObjectKeyValueNode(key, value);
} }