forked from bblanchon/ArduinoJson
-
Notifications
You must be signed in to change notification settings - Fork 2
/
JsonToken.cpp
71 lines (54 loc) · 1.14 KB
/
JsonToken.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
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "JsonToken.h"
using namespace ArduinoJson::Parser;
char* JsonToken::getText()
{
char* s = _json + _token->start;
_json[_token->end] = 0;
unescapeString(s);
return s;
}
inline void JsonToken::unescapeString(char* s)
{
char* readPtr = s;
char* writePtr = s;
char c;
do
{
c = *readPtr++;
if (c == '\\')
{
c = unescapeChar(*readPtr++);
}
*writePtr++ = c;
} while (c != 0);
}
inline char JsonToken::unescapeChar(char c)
{
// Optimized for code size on a 8-bit AVR
const char* p = "b\bf\fn\nr\rt\t";
while (true)
{
if (p[0] == 0) return c;
if (p[0] == c) return p[1];
p += 2;
}
}
JsonToken JsonToken::nextSibling() const
{
// start with current token
jsmntok_t* t = _token;
// count the number of token to skip
int yetToVisit = 1;
// skip all nested tokens
while (yetToVisit)
{
yetToVisit += t->size - 1;
t++;
}
// build a JsonToken at the new location
return JsonToken(_json, t);
}