Added class JsonValue.

Added subscript operator on JsonArray and JsonHashTable
This commit is contained in:
Benoît Blanchon
2014-07-16 13:26:11 +02:00
parent 9f07cdcabf
commit d189bd7140
10 changed files with 205 additions and 132 deletions

View File

@ -19,11 +19,11 @@ JsonArray::JsonArray(char* json, jsmntok_t* tokens)
/* /*
* Returns the token for the value at the specified index * Returns the token for the value at the specified index
*/ */
jsmntok_t* JsonArray::getToken(int index) JsonValue JsonArray::operator[](int index)
{ {
// sanity check // sanity check
if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size) if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
return 0; return JsonValue();
// skip first token, it's the whole object // skip first token, it's the whole object
jsmntok_t* currentToken = tokens + 1; jsmntok_t* currentToken = tokens + 1;
@ -35,35 +35,11 @@ jsmntok_t* JsonArray::getToken(int index)
currentToken += 1 + getNestedTokenCount(currentToken); currentToken += 1 + getNestedTokenCount(currentToken);
} }
return currentToken; return JsonValue(json, currentToken);
} }
JsonArray JsonArray::getArray(int index)
{
return JsonArray(json, getToken(index));
}
bool JsonArray::getBool(int index) JsonHashTable JsonArray::getHashTable(int index) DEPRECATED
{ {
return getBoolFromToken(getToken(index)); return (JsonHashTable) (*this)[index];
}
double JsonArray::getDouble(int index)
{
return getDoubleFromToken(getToken(index));
}
JsonHashTable JsonArray::getHashTable(int index)
{
return JsonHashTable(json, getToken(index));
}
long JsonArray::getLong(int index)
{
return getLongFromToken(getToken(index));
}
char* JsonArray::getString(int index)
{
return getStringFromToken(getToken(index));
} }

View File

@ -6,6 +6,9 @@
#pragma once #pragma once
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
#include "JsonValue.h"
#define DEPRECATED
namespace ArduinoJson namespace ArduinoJson
{ {
@ -16,7 +19,7 @@ namespace ArduinoJson
class JsonArray : public JsonObjectBase class JsonArray : public JsonObjectBase
{ {
friend class JsonParserBase; friend class JsonParserBase;
friend class JsonHashTable; friend class JsonValue;
public: public:
@ -27,17 +30,38 @@ namespace ArduinoJson
return tokens != 0 ? tokens[0].size : 0; return tokens != 0 ? tokens[0].size : 0;
} }
JsonArray getArray(int index); JsonValue operator[](int index);
bool getBool(int index);
double getDouble(int index); JsonArray getArray(int index) DEPRECATED
JsonHashTable getHashTable(int index); {
long getLong(int index); return (JsonArray) (*this)[index];
char* getString(int index); }
bool getBool(int index) DEPRECATED
{
return (bool) (*this)[index];
}
double getDouble(int index) DEPRECATED
{
return (double) (*this)[index];
}
JsonHashTable getHashTable(int index) DEPRECATED;
long getLong(int index) DEPRECATED
{
return (long) (*this)[index];
}
char* getString(int index) DEPRECATED
{
return (char*) (*this)[index];
}
private: private:
JsonArray(char* json, jsmntok_t* tokens); JsonArray(char* json, jsmntok_t* tokens);
jsmntok_t* getToken(int index);
}; };
} }
} }

View File

@ -4,8 +4,9 @@
*/ */
#include <string.h> // for strcmp() #include <string.h> // for strcmp()
#include "JsonArray.h"
#include "JsonHashTable.h" #include "JsonHashTable.h"
#include "JsonArray.h"
#include "JsonValue.h"
using namespace ArduinoJson::Parser; using namespace ArduinoJson::Parser;
@ -19,11 +20,11 @@ JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens)
/* /*
* Returns the token for the value associated with the specified key * Returns the token for the value associated with the specified key
*/ */
jsmntok_t* JsonHashTable::getToken(const char* desiredKey) JsonValue JsonHashTable::operator [](const char* desiredKey)
{ {
// sanity check // sanity check
if (json == 0 || tokens == 0 || desiredKey == 0) if (json == 0 || tokens == 0 || desiredKey == 0)
return 0; return JsonValue();
// skip first token, it's the whole object // skip first token, it's the whole object
jsmntok_t* currentToken = tokens + 1; jsmntok_t* currentToken = tokens + 1;
@ -32,13 +33,13 @@ jsmntok_t* JsonHashTable::getToken(const char* desiredKey)
for (int i = 0; i < tokens[0].size / 2 ; i++) for (int i = 0; i < tokens[0].size / 2 ; i++)
{ {
// get key token string // get key token string
char* key = getStringFromToken(currentToken); char* key = JsonValue(json, currentToken);
// compare with desired name // compare with desired name
if (strcmp(desiredKey, key) == 0) if (strcmp(desiredKey, key) == 0)
{ {
// return the value token that follows the key token // return the value token that follows the key token
return currentToken + 1; return JsonValue(json, currentToken + 1);
} }
// move forward: key + value + nested tokens // move forward: key + value + nested tokens
@ -46,40 +47,10 @@ jsmntok_t* JsonHashTable::getToken(const char* desiredKey)
} }
// nothing found, return NULL // nothing found, return NULL
return 0; return JsonValue();
} }
bool JsonHashTable::containsKey(const char* key) JsonArray JsonHashTable::getArray(const char* key) DEPRECATED
{ {
return getToken(key) != 0; return (JsonArray) (*this)[key];
}
JsonArray JsonHashTable::getArray(const char* key)
{
return JsonArray(json, getToken(key));
}
bool JsonHashTable::getBool(const char* key)
{
return getBoolFromToken(getToken(key));
}
double JsonHashTable::getDouble(const char* key)
{
return getDoubleFromToken(getToken(key));
}
JsonHashTable JsonHashTable::getHashTable(const char* key)
{
return JsonHashTable(json, getToken(key));
}
long JsonHashTable::getLong(const char* key)
{
return getLongFromToken(getToken(key));
}
char* JsonHashTable::getString(const char* key)
{
return getStringFromToken(getToken(key));
} }

View File

@ -6,6 +6,9 @@
#pragma once #pragma once
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
#include "JsonValue.h"
#define DEPRECATED
namespace ArduinoJson namespace ArduinoJson
{ {
@ -16,25 +19,49 @@ namespace ArduinoJson
class JsonHashTable : public JsonObjectBase class JsonHashTable : public JsonObjectBase
{ {
friend class JsonParserBase; friend class JsonParserBase;
friend class JsonArray; friend class JsonValue;
public: public:
JsonHashTable() {} JsonHashTable() {}
bool containsKey(const char* key); JsonValue operator[](const char* key);
JsonArray getArray(const char* key); bool containsKey(const char* key)
bool getBool(const char* key); {
double getDouble(const char* key); return (*this)[key].success();
JsonHashTable getHashTable(const char* key); }
long getLong(const char* key);
char* getString(const char* key); JsonArray getArray(const char* key) DEPRECATED;
bool getBool(const char* key) DEPRECATED
{
return (bool) (*this)[key];
}
double getDouble(const char* key) DEPRECATED
{
return (double) (*this)[key];
}
JsonHashTable getHashTable(const char* key) DEPRECATED
{
return (JsonHashTable) (*this)[key];
}
long getLong(const char* key) DEPRECATED
{
return (long) (*this)[key];
}
char* getString(const char* key) DEPRECATED
{
return (char*) (*this)[key];
}
private: private:
JsonHashTable(char* json, jsmntok_t* tokens); JsonHashTable(char* json, jsmntok_t* tokens);
jsmntok_t* getToken(const char* key);
}; };
} }
} }

View File

@ -3,7 +3,6 @@
* Benoit Blanchon 2014 - MIT License * Benoit Blanchon 2014 - MIT License
*/ */
#include <stdlib.h> // for strtol, strtod
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
using namespace ArduinoJson::Parser; using namespace ArduinoJson::Parser;
@ -23,45 +22,3 @@ int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
return count; return count;
} }
bool JsonObjectBase::getBoolFromToken(jsmntok_t* token)
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
// "true"
if (json[token->start] == 't') return true;
// "false"
if (json[token->start] == 'f') return false;
// "null"
if (json[token->start] == 'n') return false;
// number
return strtol(json + token->start, 0, 0) != 0;
}
double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
return strtod(json + token->start, 0);
}
long JsonObjectBase::getLongFromToken(jsmntok_t* token)
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
return strtol(json + token->start, 0, 0);
}
char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
{
if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
return 0;
// add null terminator to the string
json[token->end] = 0;
return json + token->start;
}

View File

@ -41,11 +41,6 @@ namespace ArduinoJson
static int getNestedTokenCount(jsmntok_t* token); static int getNestedTokenCount(jsmntok_t* token);
bool getBoolFromToken(jsmntok_t* token);
double getDoubleFromToken(jsmntok_t* token);
long getLongFromToken(jsmntok_t* token);
char* getStringFromToken(jsmntok_t* token);
char* json; char* json;
jsmntok_t* tokens; jsmntok_t* tokens;
}; };

63
JsonParser/JsonValue.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include <stdlib.h> // for strtol, strtod
#include "JsonArray.h"
#include "JsonHashTable.h"
#include "JsonValue.h"
using namespace ArduinoJson::Parser;
JsonValue::operator bool()
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
// "true"
if (json[token->start] == 't') return true;
// "false"
if (json[token->start] == 'f') return false;
// "null"
if (json[token->start] == 'n') return false;
// number
return strtol(json + token->start, 0, 0) != 0;
}
JsonValue::operator double()
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
return strtod(json + token->start, 0);
}
JsonValue::operator long()
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
return strtol(json + token->start, 0, 0);
}
JsonValue::operator char*()
{
if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
return 0;
// add null terminator to the string
json[token->end] = 0;
return json + token->start;
}
JsonValue::operator JsonArray()
{
return JsonArray(json, token);
}
JsonValue::operator JsonHashTable()
{
return JsonHashTable(json, token);
}

52
JsonParser/JsonValue.h Normal file
View File

@ -0,0 +1,52 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "jsmn.h"
namespace ArduinoJson
{
namespace Parser
{
class JsonArray;
class JsonHashTable;
class JsonValue
{
friend JsonArray;
friend JsonHashTable;
public:
bool success()
{
return token != 0;
}
operator bool();
operator double();
operator long();
operator char*();
operator JsonArray();
operator JsonHashTable();
private:
JsonValue()
: json(0), token(0)
{
}
JsonValue(char* json, jsmntok_t* token)
: json(json), token(token)
{
}
char* json;
jsmntok_t* token;
};
}
}

View File

@ -90,6 +90,7 @@
<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="..\JsonParser\JsonParserBase.cpp" />
<ClCompile Include="..\JsonParser\JsonValue.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" />
@ -101,6 +102,7 @@
<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" /> <ClInclude Include="..\JsonParser\JsonParserBase.h" />
<ClInclude Include="..\JsonParser\JsonValue.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -39,6 +39,9 @@
<ClCompile Include="..\JsonParser\JsonParserBase.cpp"> <ClCompile Include="..\JsonParser\JsonParserBase.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\JsonParser\JsonValue.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\JsonParser\jsmn.h"> <ClInclude Include="..\JsonParser\jsmn.h">
@ -59,5 +62,8 @@
<ClInclude Include="..\JsonParser\JsonParserBase.h"> <ClInclude Include="..\JsonParser\JsonParserBase.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\JsonParser\JsonValue.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>