From 241ca79114f56d6d78069a2555378ad9a6925f39 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 15 Oct 2014 23:39:25 +0200 Subject: [PATCH] Parse simple strings --- srcs/Internals/JsonParser.cpp | 20 ++++++++++++++++++++ srcs/Internals/JsonParser.h | 2 ++ tests/JsonArray_Parser_Tests.cpp | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/srcs/Internals/JsonParser.cpp b/srcs/Internals/JsonParser.cpp index 039807c0..07c2a698 100644 --- a/srcs/Internals/JsonParser.cpp +++ b/srcs/Internals/JsonParser.cpp @@ -70,6 +70,11 @@ bool JsonParser::isSpace() return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r'; } +bool JsonParser::isString() +{ + return *_ptr == '\"'; +} + void JsonParser::skipOneChar() { _ptr++; @@ -99,6 +104,9 @@ JsonNode* JsonParser::parseAnything() if (isNull()) return parseNull(); + if (isString()) + return parseString(); + return 0; } @@ -168,3 +176,15 @@ JsonNode* JsonParser::parseNull() return _buffer->createStringNode(0); } + +JsonNode* JsonParser::parseString() +{ + const char* s = ++_ptr; + + while (*_ptr != '\"') + _ptr++; + *_ptr = 0; + _ptr++; + + return _buffer->createStringNode(s); +} diff --git a/srcs/Internals/JsonParser.h b/srcs/Internals/JsonParser.h index bf40d861..5bbca4e6 100644 --- a/srcs/Internals/JsonParser.h +++ b/srcs/Internals/JsonParser.h @@ -29,6 +29,7 @@ private: inline bool isLong(); inline bool isNull(); inline bool isSpace(); + inline bool isString(); inline void skipOneChar(); inline void skipSpaces(); @@ -37,6 +38,7 @@ private: inline JsonNode* parseBoolean(); inline JsonNode* parseLong(); inline JsonNode* parseNull(); + inline JsonNode* parseString(); JsonNode *parseDouble(); }; \ No newline at end of file diff --git a/tests/JsonArray_Parser_Tests.cpp b/tests/JsonArray_Parser_Tests.cpp index 698fc07a..e7929fbd 100644 --- a/tests/JsonArray_Parser_Tests.cpp +++ b/tests/JsonArray_Parser_Tests.cpp @@ -45,6 +45,11 @@ protected: EXPECT_EQ(expected, static_cast(_array[index])); } + void elementAtIndexMustBe(int index, const char* expected) + { + EXPECT_STREQ(expected, static_cast(_array[index])); + } + StaticJsonBuffer<42> _jsonBuffer; JsonArray _array; char _jsonString[256]; @@ -147,4 +152,14 @@ TEST_F(JsonArray_Parser_Tests, TwoNulls) sizeMustBe(2); firstElementMustBe(nullCharPtr); secondElementMustBe(nullCharPtr); +} + +TEST_F(JsonArray_Parser_Tests, TwoStrings) +{ + whenInputIs("[\"hello\",\"world\"]"); + + parseMustSucceed(); + sizeMustBe(2); + firstElementMustBe("hello"); + secondElementMustBe("world"); } \ No newline at end of file