forked from bblanchon/ArduinoJson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonHashTable.cpp
84 lines (68 loc) · 1.79 KB
/
JsonHashTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 - MIT License
*/
#include "JsonArray.h"
#include "JsonHashTable.h"
#include <string.h> // for strcmp()
JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
if (tokens == 0 || tokens[0].type != JSMN_OBJECT)
makeInvalid();
}
/*
* Returns the token for the value associated with the specified key
*/
jsmntok_t* JsonHashTable::getToken(const char* desiredKey)
{
// sanity check
if (json == 0 || tokens == 0 || desiredKey == 0)
return 0;
// skip first token, it's the whole object
jsmntok_t* currentToken = tokens + 1;
// scan each keys
for (int i = 0; i < tokens[0].size / 2 ; i++)
{
// get key token string
char* key = getStringFromToken(currentToken);
// compare with desired name
if (strcmp(desiredKey, key) == 0)
{
// return the value token that follows the key token
return currentToken + 1;
}
// move forward: key + value + nested tokens
currentToken += 2 + getNestedTokenCount(currentToken + 1);
}
// nothing found, return NULL
return 0;
}
bool JsonHashTable::containsKey(const char* key)
{
return getToken(key) != 0;
}
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));
}