forked from bblanchon/ArduinoJson
JsonArray is now a simple wrapper on top of JsonValue
This commit is contained in:
@ -8,29 +8,6 @@
|
|||||||
|
|
||||||
using namespace ArduinoJson::Parser;
|
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)
|
DEPRECATED JsonHashTable JsonArray::getHashTable(int index)
|
||||||
{
|
{
|
||||||
return (JsonHashTable) (*this)[index];
|
return (JsonHashTable) (*this)[index];
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonObjectBase.h"
|
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
@ -14,26 +13,32 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
class JsonHashTable;
|
class JsonHashTable;
|
||||||
|
|
||||||
class JsonArray : public JsonObjectBase
|
class JsonArray
|
||||||
{
|
{
|
||||||
friend class JsonParserBase;
|
|
||||||
friend class JsonValue;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
JsonArray() {}
|
JsonArray() {}
|
||||||
|
|
||||||
|
JsonArray(JsonValue& value)
|
||||||
|
: value(value)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
bool success()
|
bool success()
|
||||||
{
|
{
|
||||||
return JsonObjectBase::success() && tokens->type == JSMN_ARRAY;
|
return value.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
int size()
|
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()
|
DEPRECATED int getLength()
|
||||||
{
|
{
|
||||||
@ -69,11 +74,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
JsonArray(char* json, jsmntok_t* tokens)
|
JsonValue value;
|
||||||
: JsonObjectBase(json, tokens)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonObjectBase.h"
|
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
|
|
||||||
namespace ArduinoJson
|
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()
|
JsonValue::operator JsonArray()
|
||||||
{
|
{
|
||||||
return tokens->type != JSMN_ARRAY
|
return tokens != 0 && tokens->type == JSMN_ARRAY
|
||||||
? JsonArray(*this)
|
? JsonArray(*this)
|
||||||
: JsonArray();
|
: JsonArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::operator JsonHashTable()
|
JsonValue::operator JsonHashTable()
|
||||||
{
|
{
|
||||||
return tokens->type != JSMN_OBJECT
|
return tokens != 0 && tokens->type == JSMN_OBJECT
|
||||||
? JsonHashTable(*this)
|
? JsonHashTable(*this)
|
||||||
: JsonHashTable();
|
: 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
|
* Returns the token for the value associated with the specified key
|
||||||
*/
|
*/
|
||||||
JsonValue JsonValue::operator [](const char* desiredKey)
|
JsonValue JsonValue::operator [](const char* desiredKey)
|
||||||
{
|
{
|
||||||
// sanity check
|
// sanity check
|
||||||
if (!json || !desiredKey || tokens->type != JSMN_OBJECT)
|
if (json == 0 || desiredKey == 0 || tokens->type != JSMN_OBJECT)
|
||||||
return JsonValue();
|
return JsonValue();
|
||||||
|
|
||||||
// skip first token, it's the whole object
|
// skip first token, it's the whole object
|
||||||
@ -99,3 +104,41 @@ JsonValue JsonValue::operator [](const char* desiredKey)
|
|||||||
// nothing found, return NULL
|
// nothing found, return NULL
|
||||||
return JsonValue();
|
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
|
#pragma once
|
||||||
|
|
||||||
#include "jsmn.h"
|
#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
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
@ -15,17 +22,26 @@ namespace ArduinoJson
|
|||||||
class JsonArray;
|
class JsonArray;
|
||||||
class JsonHashTable;
|
class JsonHashTable;
|
||||||
|
|
||||||
class JsonValue : public JsonObjectBase
|
class JsonValue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
JsonValue() {}
|
JsonValue()
|
||||||
|
: json(0), tokens(0)
|
||||||
JsonValue(char* json, jsmntok_t* tokens)
|
|
||||||
: JsonObjectBase(json, tokens)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue(char* json, jsmntok_t* tokens)
|
||||||
|
: json(json), tokens(tokens)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool success()
|
||||||
|
{
|
||||||
|
return json != 0 && tokens != 0;
|
||||||
|
}
|
||||||
|
|
||||||
operator bool();
|
operator bool();
|
||||||
operator double();
|
operator double();
|
||||||
operator long();
|
operator long();
|
||||||
@ -33,7 +49,17 @@ namespace ArduinoJson
|
|||||||
operator JsonArray();
|
operator JsonArray();
|
||||||
operator JsonHashTable();
|
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\jsmn.cpp" />
|
||||||
<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\JsonParserBase.cpp" />
|
<ClCompile Include="..\JsonParser\JsonParserBase.cpp" />
|
||||||
<ClCompile Include="..\JsonParser\JsonValue.cpp" />
|
<ClCompile Include="..\JsonParser\JsonValue.cpp" />
|
||||||
<ClCompile Include="JsonArrayTests.cpp" />
|
<ClCompile Include="JsonArrayTests.cpp" />
|
||||||
@ -99,7 +98,6 @@
|
|||||||
<ClInclude Include="..\JsonParser\jsmn.h" />
|
<ClInclude Include="..\JsonParser\jsmn.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonArray.h" />
|
<ClInclude Include="..\JsonParser\JsonArray.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonObjectBase.h" />
|
|
||||||
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonParserBase.h" />
|
<ClInclude Include="..\JsonParser\JsonParserBase.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonValue.h" />
|
<ClInclude Include="..\JsonParser\JsonValue.h" />
|
||||||
|
@ -24,9 +24,6 @@
|
|||||||
<ClCompile Include="..\JsonParser\JsonHashTable.cpp">
|
<ClCompile Include="..\JsonParser\JsonHashTable.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="JsonArrayTests.cpp">
|
<ClCompile Include="JsonArrayTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -53,9 +50,6 @@
|
|||||||
<ClInclude Include="..\JsonParser\JsonHashTable.h">
|
<ClInclude Include="..\JsonParser\JsonHashTable.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\JsonParser\JsonObjectBase.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\JsonParser\JsonParser.h">
|
<ClInclude Include="..\JsonParser\JsonParser.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
Reference in New Issue
Block a user