JsonArray is now a simple wrapper on top of JsonValue

This commit is contained in:
Benoît Blanchon
2014-07-17 13:12:12 +02:00
parent f2579397d6
commit 5e1697f47b
9 changed files with 93 additions and 132 deletions

View File

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

View File

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

View File

@ -5,7 +5,6 @@
#pragma once
#include "JsonObjectBase.h"
#include "JsonValue.h"
namespace ArduinoJson

View File

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

View File

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

View File

@ -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
@ -98,4 +103,42 @@ 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;
}

View File

@ -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,15 +22,24 @@ 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();
@ -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);
};
}
}

View File

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

View File

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