mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 04:52:22 +02:00
JsonArray is now a simple wrapper on top of JsonValue
This commit is contained in:
@ -8,29 +8,6 @@
|
||||
|
||||
using namespace ArduinoJson::Parser;
|
||||
|
||||
/*
|
||||
* Returns the token for the value at the specified index
|
||||
*/
|
||||
JsonValue JsonArray::operator[](int index)
|
||||
{
|
||||
// sanity check
|
||||
if (!success() || index < 0 || index >= tokens[0].size)
|
||||
return JsonValue();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
jsmntok_t* currentToken = tokens + 1;
|
||||
|
||||
// skip all tokens before the specified index
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
// move forward: current + nested tokens
|
||||
currentToken += 1 + getNestedTokenCount(currentToken);
|
||||
}
|
||||
|
||||
return JsonValue(json, currentToken);
|
||||
}
|
||||
|
||||
|
||||
DEPRECATED JsonHashTable JsonArray::getHashTable(int index)
|
||||
{
|
||||
return (JsonHashTable) (*this)[index];
|
||||
|
@ -5,7 +5,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonObjectBase.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
@ -14,26 +13,32 @@ namespace ArduinoJson
|
||||
{
|
||||
class JsonHashTable;
|
||||
|
||||
class JsonArray : public JsonObjectBase
|
||||
class JsonArray
|
||||
{
|
||||
friend class JsonParserBase;
|
||||
friend class JsonValue;
|
||||
|
||||
public:
|
||||
|
||||
JsonArray() {}
|
||||
|
||||
JsonArray(JsonValue& value)
|
||||
: value(value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool success()
|
||||
{
|
||||
return JsonObjectBase::success() && tokens->type == JSMN_ARRAY;
|
||||
return value.success();
|
||||
}
|
||||
|
||||
int size()
|
||||
{
|
||||
return success() ? tokens[0].size : 0;
|
||||
return value.size();
|
||||
}
|
||||
|
||||
JsonValue operator[](int index);
|
||||
JsonValue operator[](int index)
|
||||
{
|
||||
return value[index];
|
||||
}
|
||||
|
||||
DEPRECATED int getLength()
|
||||
{
|
||||
@ -69,11 +74,7 @@ namespace ArduinoJson
|
||||
|
||||
private:
|
||||
|
||||
JsonArray(char* json, jsmntok_t* tokens)
|
||||
: JsonObjectBase(json, tokens)
|
||||
{
|
||||
|
||||
}
|
||||
JsonValue value;
|
||||
};
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonObjectBase.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "JsonObjectBase.h"
|
||||
|
||||
using namespace ArduinoJson::Parser;
|
||||
|
||||
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
|
||||
{
|
||||
int tokensToVisit = token->size;
|
||||
int count = 0;
|
||||
|
||||
while (tokensToVisit)
|
||||
{
|
||||
count++;
|
||||
token++;
|
||||
tokensToVisit--;
|
||||
tokensToVisit += token->size;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "jsmn.h"
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define DEPRECATED __attribute__((deprecated))
|
||||
#elif defined(_MSC_VER)
|
||||
#define DEPRECATED __declspec(deprecated)
|
||||
#else
|
||||
#define DEPRECATED
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Parser
|
||||
{
|
||||
class JsonObjectBase
|
||||
{
|
||||
friend class JsonHashTable;
|
||||
|
||||
public:
|
||||
|
||||
JsonObjectBase()
|
||||
: json(0), tokens(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool success()
|
||||
{
|
||||
return json != 0 && tokens != 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
JsonObjectBase(char* json, jsmntok_t* tokens)
|
||||
: json(json), tokens(tokens)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static int getNestedTokenCount(jsmntok_t* token);
|
||||
|
||||
char* json;
|
||||
jsmntok_t* tokens;
|
||||
};
|
||||
}
|
||||
}
|
@ -55,25 +55,30 @@ JsonValue::operator char*()
|
||||
|
||||
JsonValue::operator JsonArray()
|
||||
{
|
||||
return tokens->type != JSMN_ARRAY
|
||||
return tokens != 0 && tokens->type == JSMN_ARRAY
|
||||
? JsonArray(*this)
|
||||
: JsonArray();
|
||||
}
|
||||
|
||||
JsonValue::operator JsonHashTable()
|
||||
{
|
||||
return tokens->type != JSMN_OBJECT
|
||||
return tokens != 0 && tokens->type == JSMN_OBJECT
|
||||
? JsonHashTable(*this)
|
||||
: JsonHashTable();
|
||||
}
|
||||
|
||||
int JsonValue::size()
|
||||
{
|
||||
return tokens != 0 && tokens->type == JSMN_ARRAY ? tokens->size : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the token for the value associated with the specified key
|
||||
*/
|
||||
JsonValue JsonValue::operator [](const char* desiredKey)
|
||||
{
|
||||
// sanity check
|
||||
if (!json || !desiredKey || tokens->type != JSMN_OBJECT)
|
||||
if (json == 0 || desiredKey == 0 || tokens->type != JSMN_OBJECT)
|
||||
return JsonValue();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
@ -99,3 +104,41 @@ JsonValue JsonValue::operator [](const char* desiredKey)
|
||||
// nothing found, return NULL
|
||||
return JsonValue();
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the token for the value at the specified index
|
||||
*/
|
||||
JsonValue JsonValue::operator[](int index)
|
||||
{
|
||||
// sanity check
|
||||
if (index < 0 || index >= size())
|
||||
return JsonValue();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
jsmntok_t* currentToken = tokens + 1;
|
||||
|
||||
// skip all tokens before the specified index
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
// move forward: current + nested tokens
|
||||
currentToken += 1 + getNestedTokenCount(currentToken);
|
||||
}
|
||||
|
||||
return JsonValue(json, currentToken);
|
||||
}
|
||||
|
||||
int JsonValue::getNestedTokenCount(jsmntok_t* token)
|
||||
{
|
||||
int tokensToVisit = token->size;
|
||||
int count = 0;
|
||||
|
||||
while (tokensToVisit)
|
||||
{
|
||||
count++;
|
||||
token++;
|
||||
tokensToVisit--;
|
||||
tokensToVisit += token->size;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
@ -6,7 +6,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "jsmn.h"
|
||||
#include "JsonObjectBase.h"
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define DEPRECATED __attribute__((deprecated))
|
||||
#elif defined(_MSC_VER)
|
||||
#define DEPRECATED __declspec(deprecated)
|
||||
#else
|
||||
#define DEPRECATED
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
@ -15,17 +22,26 @@ namespace ArduinoJson
|
||||
class JsonArray;
|
||||
class JsonHashTable;
|
||||
|
||||
class JsonValue : public JsonObjectBase
|
||||
class JsonValue
|
||||
{
|
||||
public:
|
||||
JsonValue() {}
|
||||
|
||||
JsonValue(char* json, jsmntok_t* tokens)
|
||||
: JsonObjectBase(json, tokens)
|
||||
JsonValue()
|
||||
: json(0), tokens(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
JsonValue(char* json, jsmntok_t* tokens)
|
||||
: json(json), tokens(tokens)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool success()
|
||||
{
|
||||
return json != 0 && tokens != 0;
|
||||
}
|
||||
|
||||
operator bool();
|
||||
operator double();
|
||||
operator long();
|
||||
@ -33,7 +49,17 @@ namespace ArduinoJson
|
||||
operator JsonArray();
|
||||
operator JsonHashTable();
|
||||
|
||||
JsonValue operator[](const char* key);
|
||||
JsonValue operator[](const char*);
|
||||
JsonValue operator[](int);
|
||||
|
||||
int size();
|
||||
|
||||
private:
|
||||
|
||||
char* json;
|
||||
jsmntok_t* tokens;
|
||||
|
||||
static int getNestedTokenCount(jsmntok_t* token);
|
||||
};
|
||||
}
|
||||
}
|
@ -88,7 +88,6 @@
|
||||
<ClCompile Include="..\JsonParser\jsmn.cpp" />
|
||||
<ClCompile Include="..\JsonParser\JsonArray.cpp" />
|
||||
<ClCompile Include="..\JsonParser\JsonHashTable.cpp" />
|
||||
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp" />
|
||||
<ClCompile Include="..\JsonParser\JsonParserBase.cpp" />
|
||||
<ClCompile Include="..\JsonParser\JsonValue.cpp" />
|
||||
<ClCompile Include="JsonArrayTests.cpp" />
|
||||
@ -99,7 +98,6 @@
|
||||
<ClInclude Include="..\JsonParser\jsmn.h" />
|
||||
<ClInclude Include="..\JsonParser\JsonArray.h" />
|
||||
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
||||
<ClInclude Include="..\JsonParser\JsonObjectBase.h" />
|
||||
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
||||
<ClInclude Include="..\JsonParser\JsonParserBase.h" />
|
||||
<ClInclude Include="..\JsonParser\JsonValue.h" />
|
||||
|
@ -24,9 +24,6 @@
|
||||
<ClCompile Include="..\JsonParser\JsonHashTable.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonArrayTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
@ -53,9 +50,6 @@
|
||||
<ClInclude Include="..\JsonParser\JsonHashTable.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\JsonParser\JsonObjectBase.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\JsonParser\JsonParser.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
Reference in New Issue
Block a user