Ver Fonte

Extracted class JsonParserBase

Benoit Blanchon há 11 anos atrás
pai
commit
dde5a2510b

+ 1 - 3
JsonParser/JsonArray.h

@@ -15,9 +15,7 @@ namespace ArduinoJson
 
         class JsonArray : public JsonObjectBase
         {
-            template <int N>
-            friend class JsonParser;
-
+            friend class JsonParserBase;
             friend class JsonHashTable;
 
         public:

+ 1 - 3
JsonParser/JsonHashTable.h

@@ -15,9 +15,7 @@ namespace ArduinoJson
 
         class JsonHashTable : public JsonObjectBase
         {
-            template <int N>
-            friend class JsonParser;
-
+            friend class JsonParserBase;
             friend class JsonArray;
 
         public:

+ 4 - 35
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 <int MAX_TOKENS>
-        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];
         };
     }

+ 19 - 0
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;
+}

+ 53 - 0
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);
+        };
+    }
+}

+ 2 - 0
JsonParserTests/JsonParserTests.vcxproj

@@ -89,6 +89,7 @@
     <ClCompile Include="..\JsonParser\JsonArray.cpp" />
     <ClCompile Include="..\JsonParser\JsonHashTable.cpp" />
     <ClCompile Include="..\JsonParser\JsonObjectBase.cpp" />
+    <ClCompile Include="..\JsonParser\JsonParserBase.cpp" />
     <ClCompile Include="JsonArrayTests.cpp" />
     <ClCompile Include="JsonHashTableTests.cpp" />
     <ClCompile Include="GbathreeBug.cpp" />
@@ -99,6 +100,7 @@
     <ClInclude Include="..\JsonParser\JsonHashTable.h" />
     <ClInclude Include="..\JsonParser\JsonObjectBase.h" />
     <ClInclude Include="..\JsonParser\JsonParser.h" />
+    <ClInclude Include="..\JsonParser\JsonParserBase.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

+ 6 - 0
JsonParserTests/JsonParserTests.vcxproj.filters

@@ -36,6 +36,9 @@
     <ClCompile Include="JsonHashTableTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\JsonParser\JsonParserBase.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\JsonParser\jsmn.h">
@@ -53,5 +56,8 @@
     <ClInclude Include="..\JsonParser\JsonParser.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\JsonParser\JsonParserBase.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>