-
Notifications
You must be signed in to change notification settings - Fork 88
/
JsonStreamingParser.h
135 lines (89 loc) · 3.33 KB
/
JsonStreamingParser.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**The MIT License (MIT)
Copyright (c) 2015 by Daniel Eichhorn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
See more at http://blog.squix.ch and https://github.com/squix78/json-streaming-parser
*/
#pragma once
#include <Arduino.h>
#include "JsonListener.h"
#define STATE_START_DOCUMENT 0
#define STATE_DONE -1
#define STATE_IN_ARRAY 1
#define STATE_IN_OBJECT 2
#define STATE_END_KEY 3
#define STATE_AFTER_KEY 4
#define STATE_IN_STRING 5
#define STATE_START_ESCAPE 6
#define STATE_UNICODE 7
#define STATE_IN_NUMBER 8
#define STATE_IN_TRUE 9
#define STATE_IN_FALSE 10
#define STATE_IN_NULL 11
#define STATE_AFTER_VALUE 12
#define STATE_UNICODE_SURROGATE 13
#define STACK_OBJECT 0
#define STACK_ARRAY 1
#define STACK_KEY 2
#define STACK_STRING 3
#define BUFFER_MAX_LENGTH 512
class JsonStreamingParser {
private:
int state;
int stack[20];
int stackPos = 0;
JsonListener* myListener;
boolean doEmitWhitespace = false;
// fixed length buffer array to prepare for c code
char buffer[BUFFER_MAX_LENGTH];
int bufferPos = 0;
char unicodeEscapeBuffer[10];
int unicodeEscapeBufferPos = 0;
char unicodeBuffer[10];
int unicodeBufferPos = 0;
int characterCounter = 0;
int unicodeHighSurrogate = 0;
void increaseBufferPointer();
void endString();
void endArray();
void startValue(char c);
void startKey();
void processEscapeCharacters(char c);
boolean isDigit(char c);
boolean isHexCharacter(char c);
char convertCodepointToCharacter(int num);
void endUnicodeCharacter(int codepoint);
void startNumber(char c);
void startString();
void startObject();
void startArray();
void endNull();
void endFalse();
void endTrue();
void endDocument();
int convertDecimalBufferToInt(char myArray[], int length);
void endNumber();
void endUnicodeSurrogateInterstitial();
boolean doesCharArrayContain(char myArray[], int length, char c);
int getHexArrayAsDecimal(char hexArray[], int length);
void processUnicodeCharacter(char c);
void endObject();
public:
JsonStreamingParser();
void parse(char c);
void setListener(JsonListener* listener);
void reset();
};