forked from bblanchon/ArduinoJson
-
Notifications
You must be signed in to change notification settings - Fork 2
/
JsonToken.h
99 lines (81 loc) · 2.4 KB
/
JsonToken.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "jsmn.h"
namespace ArduinoJson
{
namespace Parser
{
// A pointer to a JSON token
class JsonToken
{
public:
// Create a "null" pointer
JsonToken()
: _token(0)
{
}
// Create a pointer to the specified JSON token
JsonToken(char* json, jsmntok_t* token)
: _json(json), _token(token)
{
}
// Get content of the JSON token
char* getText();
// Get the number of children tokens
int childrenCount()
{
return _token->size;
}
// Get a pointer to the first child of the current token
JsonToken firstChild() const
{
return JsonToken(_json, _token + 1);
}
// Get a pointer to the next sibling token (ie skiping the children tokens)
JsonToken nextSibling() const;
// Test equality
bool operator!=(const JsonToken& other) const
{
return _token != other._token;
}
// Tell if the pointer is "null"
bool isValid()
{
return _token != 0;
}
// Tell if the JSON token is a JSON object
bool isObject()
{
return _token != 0 && _token->type == JSMN_OBJECT;
}
// Tell if the JSON token is a JSON array
bool isArray()
{
return _token != 0 && _token->type == JSMN_ARRAY;
}
// Tell if the JSON token is a primitive
bool isPrimitive()
{
return _token != 0 && _token->type == JSMN_PRIMITIVE;
}
// Tell if the JSON token is a string
bool isString()
{
return _token != 0 && _token->type == JSMN_STRING;
}
// Explicit wait to create a "null" JsonToken
static JsonToken null()
{
return JsonToken();
}
private:
char* _json;
jsmntok_t* _token;
static char unescapeChar(char c);
static void unescapeString(char* s);
};
}
}