From dde5a2510b69954351efb548813076641eef3af2 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 14 Jul 2014 13:17:30 +0200 Subject: [PATCH] Extracted class JsonParserBase --- JsonParser/JsonArray.h | 4 +- JsonParser/JsonHashTable.h | 4 +- JsonParser/JsonParser.h | 39 ++------------ JsonParser/JsonParserBase.cpp | 19 +++++++ JsonParser/JsonParserBase.h | 53 +++++++++++++++++++ JsonParserTests/JsonParserTests.vcxproj | 2 + .../JsonParserTests.vcxproj.filters | 6 +++ 7 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 JsonParser/JsonParserBase.cpp create mode 100644 JsonParser/JsonParserBase.h diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index 0633f91c..42219b58 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -15,9 +15,7 @@ namespace ArduinoJson class JsonArray : public JsonObjectBase { - template - friend class JsonParser; - + friend class JsonParserBase; friend class JsonHashTable; public: diff --git a/JsonParser/JsonHashTable.h b/JsonParser/JsonHashTable.h index a0139945..b33492ad 100644 --- a/JsonParser/JsonHashTable.h +++ b/JsonParser/JsonHashTable.h @@ -15,9 +15,7 @@ namespace ArduinoJson class JsonHashTable : public JsonObjectBase { - template - friend class JsonParser; - + friend class JsonParserBase; friend class JsonArray; public: diff --git a/JsonParser/JsonParser.h b/JsonParser/JsonParser.h index ac41c00c..a6f118bb 100644 --- a/JsonParser/JsonParser.h +++ b/JsonParser/JsonParser.h @@ -5,8 +5,7 @@ #pragma once -#include "JsonHashTable.h" -#include "JsonArray.h" +#include "JsonParserBase.h" namespace ArduinoJson { @@ -26,45 +25,15 @@ namespace ArduinoJson * longer life that the JsonParser. */ template - class JsonParser + class JsonParser : public JsonParserBase { public: - - /* - * Parse the JSON string and return a array. - * - * The content of the string may be altered to add '\0' at the - * end of string tokens - */ - JsonArray parseArray(char* json) + JsonParser() + : JsonParserBase(tokens, MAX_TOKENS) { - return JsonArray(json, parse(json)); - } - - /* - * Parse the JSON string and return a array. - * - * The content of the string may be altered to add '\0' at the - * end of string tokens - */ - JsonHashTable parseHashTable(char* json) - { - return JsonHashTable(json, parse(json)); } private: - - jsmntok_t* parse(char* json) - { - jsmn_parser parser; - jsmn_init(&parser); - - if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) - return 0; - - return tokens; - } - jsmntok_t tokens[MAX_TOKENS]; }; } diff --git a/JsonParser/JsonParserBase.cpp b/JsonParser/JsonParserBase.cpp new file mode 100644 index 00000000..ada66dde --- /dev/null +++ b/JsonParser/JsonParserBase.cpp @@ -0,0 +1,19 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#include "JsonParserBase.h" + +using namespace ArduinoJson::Parser; + +jsmntok_t* JsonParserBase::parse(char* json) +{ + jsmn_parser parser; + jsmn_init(&parser); + + if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, maxTokens)) + return 0; + + return tokens; +} diff --git a/JsonParser/JsonParserBase.h b/JsonParser/JsonParserBase.h new file mode 100644 index 00000000..e08bf2dd --- /dev/null +++ b/JsonParser/JsonParserBase.h @@ -0,0 +1,53 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonHashTable.h" +#include "JsonArray.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonParserBase + { + public: + + JsonParserBase(jsmntok_t* tokens, int maxTokens) + : tokens(tokens), maxTokens(maxTokens) + { + } + + /* + * Parse the JSON string and return a array. + * + * The content of the string may be altered to add '\0' at the + * end of string tokens + */ + JsonArray parseArray(char* json) + { + return JsonArray(json, parse(json)); + } + + /* + * Parse the JSON string and return a array. + * + * The content of the string may be altered to add '\0' at the + * end of string tokens + */ + JsonHashTable parseHashTable(char* json) + { + return JsonHashTable(json, parse(json)); + } + + private: + jsmntok_t* tokens; + int maxTokens; + + jsmntok_t* parse(char* json); + }; + } +} \ No newline at end of file diff --git a/JsonParserTests/JsonParserTests.vcxproj b/JsonParserTests/JsonParserTests.vcxproj index d30f231e..f9df10eb 100644 --- a/JsonParserTests/JsonParserTests.vcxproj +++ b/JsonParserTests/JsonParserTests.vcxproj @@ -89,6 +89,7 @@ + @@ -99,6 +100,7 @@ + diff --git a/JsonParserTests/JsonParserTests.vcxproj.filters b/JsonParserTests/JsonParserTests.vcxproj.filters index 4e3dedd3..a11a87e0 100644 --- a/JsonParserTests/JsonParserTests.vcxproj.filters +++ b/JsonParserTests/JsonParserTests.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files + @@ -53,5 +56,8 @@ Header Files + + Header Files + \ No newline at end of file