Extracted class JsonParserBase

This commit is contained in:
Benoit Blanchon
2014-07-14 13:17:30 +02:00
parent a42b03ec26
commit dde5a2510b
7 changed files with 86 additions and 41 deletions

View File

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

View File

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

View File

@ -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];
};
}

View File

@ -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;
}

View File

@ -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);
};
}
}

View File

@ -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">

View File

@ -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>