mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 13:02:25 +02:00
Extracted class JsonParserBase
This commit is contained in:
@ -15,9 +15,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
class JsonArray : public JsonObjectBase
|
class JsonArray : public JsonObjectBase
|
||||||
{
|
{
|
||||||
template <int N>
|
friend class JsonParserBase;
|
||||||
friend class JsonParser;
|
|
||||||
|
|
||||||
friend class JsonHashTable;
|
friend class JsonHashTable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -15,9 +15,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
class JsonHashTable : public JsonObjectBase
|
class JsonHashTable : public JsonObjectBase
|
||||||
{
|
{
|
||||||
template <int N>
|
friend class JsonParserBase;
|
||||||
friend class JsonParser;
|
|
||||||
|
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonHashTable.h"
|
#include "JsonParserBase.h"
|
||||||
#include "JsonArray.h"
|
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
@ -26,45 +25,15 @@ namespace ArduinoJson
|
|||||||
* longer life that the JsonParser.
|
* longer life that the JsonParser.
|
||||||
*/
|
*/
|
||||||
template <int MAX_TOKENS>
|
template <int MAX_TOKENS>
|
||||||
class JsonParser
|
class JsonParser : public JsonParserBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
JsonParser()
|
||||||
/*
|
: JsonParserBase(tokens, MAX_TOKENS)
|
||||||
* 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:
|
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];
|
jsmntok_t tokens[MAX_TOKENS];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
19
JsonParser/JsonParserBase.cpp
Normal file
19
JsonParser/JsonParserBase.cpp
Normal 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;
|
||||||
|
}
|
53
JsonParser/JsonParserBase.h
Normal file
53
JsonParser/JsonParserBase.h
Normal 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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -89,6 +89,7 @@
|
|||||||
<ClCompile Include="..\JsonParser\JsonArray.cpp" />
|
<ClCompile Include="..\JsonParser\JsonArray.cpp" />
|
||||||
<ClCompile Include="..\JsonParser\JsonHashTable.cpp" />
|
<ClCompile Include="..\JsonParser\JsonHashTable.cpp" />
|
||||||
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp" />
|
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp" />
|
||||||
|
<ClCompile Include="..\JsonParser\JsonParserBase.cpp" />
|
||||||
<ClCompile Include="JsonArrayTests.cpp" />
|
<ClCompile Include="JsonArrayTests.cpp" />
|
||||||
<ClCompile Include="JsonHashTableTests.cpp" />
|
<ClCompile Include="JsonHashTableTests.cpp" />
|
||||||
<ClCompile Include="GbathreeBug.cpp" />
|
<ClCompile Include="GbathreeBug.cpp" />
|
||||||
@ -99,6 +100,7 @@
|
|||||||
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonObjectBase.h" />
|
<ClInclude Include="..\JsonParser\JsonObjectBase.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
||||||
|
<ClInclude Include="..\JsonParser\JsonParserBase.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -36,6 +36,9 @@
|
|||||||
<ClCompile Include="JsonHashTableTests.cpp">
|
<ClCompile Include="JsonHashTableTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\JsonParser\JsonParserBase.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\JsonParser\jsmn.h">
|
<ClInclude Include="..\JsonParser\jsmn.h">
|
||||||
@ -53,5 +56,8 @@
|
|||||||
<ClInclude Include="..\JsonParser\JsonParser.h">
|
<ClInclude Include="..\JsonParser\JsonParser.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\JsonParser\JsonParserBase.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Reference in New Issue
Block a user