From beb49a9446cad3d459b670228e498dda77bdb47d Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 14 Oct 2014 21:48:22 +0200 Subject: [PATCH] Parse an array with two longs --- srcs/Internals/JsonParser.cpp | 28 +++++++++++++++++++++++----- srcs/Internals/JsonParser.h | 10 +++++++--- srcs/JsonBuffer.cpp | 2 +- tests/JsonArray_Parser_Tests.cpp | 11 +++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/srcs/Internals/JsonParser.cpp b/srcs/Internals/JsonParser.cpp index ac8f102e..bf145371 100644 --- a/srcs/Internals/JsonParser.cpp +++ b/srcs/Internals/JsonParser.cpp @@ -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; } diff --git a/srcs/Internals/JsonParser.h b/srcs/Internals/JsonParser.h index fa03369f..395f9846 100644 --- a/srcs/Internals/JsonParser.h +++ b/srcs/Internals/JsonParser.h @@ -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(); }; \ No newline at end of file diff --git a/srcs/JsonBuffer.cpp b/srcs/JsonBuffer.cpp index 6eaec7a3..292c9d66 100644 --- a/srcs/JsonBuffer.cpp +++ b/srcs/JsonBuffer.cpp @@ -22,5 +22,5 @@ JsonNode* JsonBuffer::createNode() JsonArray JsonBuffer::parseArray(char* json) { JsonParser parser(this, json); - return JsonArray(parser.parseNode()); + return JsonArray(parser.parseAnything()); } \ No newline at end of file diff --git a/tests/JsonArray_Parser_Tests.cpp b/tests/JsonArray_Parser_Tests.cpp index 78822743..cf4d0086 100644 --- a/tests/JsonArray_Parser_Tests.cpp +++ b/tests/JsonArray_Parser_Tests.cpp @@ -47,4 +47,15 @@ TEST_F(JsonArray_Parser_Tests, OneInteger) EXPECT_TRUE(array.success()); EXPECT_EQ(1, array.size()); EXPECT_EQ(42, static_cast(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(array[0])); + EXPECT_EQ(84, static_cast(array[1])); } \ No newline at end of file