Parse an array with two longs

This commit is contained in:
Benoit Blanchon
2014-10-14 21:48:22 +02:00
parent ee205971e9
commit beb49a9446
4 changed files with 42 additions and 9 deletions

View File

@ -42,6 +42,11 @@ bool JsonParser::isSpace()
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
}
bool JsonParser::isComma()
{
return *_ptr == ',';
}
void JsonParser::skipOneChar()
{
_ptr++;
@ -52,7 +57,7 @@ void JsonParser::skipSpaces()
while(isSpace()) skipOneChar();
}
JsonNode* JsonParser::parseNode()
JsonNode* JsonParser::parseAnything()
{
skipSpaces();
@ -67,19 +72,32 @@ JsonNode* JsonParser::parseNode()
JsonNode* JsonParser::parseArray()
{
skipOneChar(); // skip the '['
skipSpaces();
JsonNode* node = _buffer->createNode();
node->setAsArray(_buffer);
skipOneChar(); // skip the '['
skipSpaces();
if (isEnd())
return 0;
if (isArrayStop())
return node;
node->addChild(parseNode());
for(;;)
{
node->addChild(parseAnything());
skipSpaces();
if (isArrayStop())
return node;
if (!isComma())
return 0;
skipOneChar(); // skip the ','
}
return node;
}

View File

@ -12,9 +12,8 @@ public:
}
JsonNode* parseNode();
JsonNode* parseArray();
JsonNode* parseLong();
JsonNode* parseAnything();
private:
JsonBuffer* _buffer;
char* _ptr;
@ -24,6 +23,11 @@ private:
inline bool isArrayStop();
inline bool isLong();
inline bool isSpace();
inline bool isComma();
inline void skipOneChar();
inline void skipSpaces();
inline JsonNode* parseArray();
inline JsonNode* parseLong();
};

View File

@ -22,5 +22,5 @@ JsonNode* JsonBuffer::createNode()
JsonArray JsonBuffer::parseArray(char* json)
{
JsonParser parser(this, json);
return JsonArray(parser.parseNode());
return JsonArray(parser.parseAnything());
}

View File

@ -48,3 +48,14 @@ TEST_F(JsonArray_Parser_Tests, OneInteger)
EXPECT_EQ(1, array.size());
EXPECT_EQ(42, static_cast<int>(array[0]));
}
TEST_F(JsonArray_Parser_Tests, TwoIntegers)
{
JsonArray array = json.parseArray("[42,84]");
EXPECT_TRUE(array.success());
EXPECT_EQ(2, array.size());
EXPECT_EQ(42, static_cast<int>(array[0]));
EXPECT_EQ(84, static_cast<int>(array[1]));
}