GDAL

cpl_json_streaming_parser.h
1/******************************************************************************
2 *
3 * Project: CPL - Common Portability Library
4 * Purpose: JSon streaming parser
5 * Author: Even Rouault, even.rouault at spatialys.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef CPL_JSON_STREAMIN_PARSER_H
14#define CPL_JSON_STREAMIN_PARSER_H
15
18#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
19
20#include <vector>
21#include <string>
22#include "cpl_port.h"
23
24 class CPL_DLL CPLJSonStreamingParser
25{
26 CPL_DISALLOW_COPY_ASSIGN(CPLJSonStreamingParser)
27
28 enum State
29 {
30 INIT,
31 OBJECT,
32 ARRAY,
33 STRING,
34 NUMBER,
35 STATE_TRUE,
36 STATE_FALSE,
37 STATE_NULL
38 };
39
40 bool m_bExceptionOccurred = false;
41 bool m_bElementFound = false;
42 bool m_bStopParsing = false;
43 int m_nLastChar = 0;
44 int m_nLineCounter = 1;
45 int m_nCharCounter = 1;
46 std::vector<State> m_aState{};
47 std::string m_osToken{};
48 enum class ArrayState
49 {
50 INIT,
51 AFTER_COMMA,
52 AFTER_VALUE
53 };
54 std::vector<ArrayState> m_abArrayState{};
55 bool m_bInStringEscape = false;
56 bool m_bInUnicode = false;
57 std::string m_osUnicodeHex{};
58 size_t m_nMaxDepth = 1024;
59 size_t m_nMaxStringSize = 10000000;
60
61 enum MemberState
62 {
63 WAITING_KEY,
64 IN_KEY,
65 KEY_FINISHED,
66 IN_VALUE
67 };
68
69 std::vector<MemberState> m_aeObjectState{};
70
71 enum State currentState()
72 {
73 return m_aState.back();
74 }
75
76 void SkipSpace(const char *&pStr, size_t &nLength);
77 void AdvanceChar(const char *&pStr, size_t &nLength);
78 bool EmitUnexpectedChar(char ch, const char *pszExpecting = nullptr);
79 bool StartNewToken(const char *&pStr, size_t &nLength);
80 bool CheckAndEmitTrueFalseOrNull(char ch);
81 bool CheckStackEmpty();
82 void DecodeUnicode();
83
84 protected:
85 bool EmitException(const char *pszMessage);
86 void StopParsing();
87
88 public:
89 CPLJSonStreamingParser();
90 virtual ~CPLJSonStreamingParser();
91
92 void SetMaxDepth(size_t nVal);
93 void SetMaxStringSize(size_t nVal);
94
95 bool ExceptionOccurred() const
96 {
97 return m_bExceptionOccurred;
98 }
99
100 static std::string GetSerializedString(const char *pszStr);
101
102 virtual void Reset();
103 virtual bool Parse(const char *pStr, size_t nLength, bool bFinished);
104
105 virtual void String(const char * /*pszValue*/, size_t /*nLength*/)
106 {
107 }
108
109 virtual void Number(const char * /*pszValue*/, size_t /*nLength*/)
110 {
111 }
112
113 virtual void Boolean(bool /*b*/)
114 {
115 }
116
117 virtual void Null()
118 {
119 }
120
121 virtual void StartObject()
122 {
123 }
124
125 virtual void EndObject()
126 {
127 }
128
129 virtual void StartObjectMember(const char * /*pszKey*/, size_t /*nLength*/)
130 {
131 }
132
133 virtual void StartArray()
134 {
135 }
136
137 virtual void EndArray()
138 {
139 }
140
141 virtual void StartArrayMember()
142 {
143 }
144
145 virtual void Exception(const char * /*pszMessage*/)
146 {
147 }
148};
149
150#endif // __cplusplus
151
154#endif // CPL_JSON_STREAMIN_PARSER_H
cpl_port.h
Core portability definitions for CPL.
CPL_DISALLOW_COPY_ASSIGN
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:1030