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  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #ifndef CPL_JSON_STREAMIN_PARSER_H
30 #define CPL_JSON_STREAMIN_PARSER_H
31 
34 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
35 
36 #include <vector>
37 #include <string>
38 #include "cpl_port.h"
39 
40 class CPL_DLL CPLJSonStreamingParser
41 {
42  CPL_DISALLOW_COPY_ASSIGN(CPLJSonStreamingParser)
43 
44  enum State
45  {
46  INIT,
47  OBJECT,
48  ARRAY,
49  STRING,
50  NUMBER,
51  STATE_TRUE,
52  STATE_FALSE,
53  STATE_NULL
54  };
55 
56  bool m_bExceptionOccurred = false;
57  bool m_bElementFound = false;
58  bool m_bStopParsing = false;
59  int m_nLastChar = 0;
60  int m_nLineCounter = 1;
61  int m_nCharCounter = 1;
62  std::vector<State> m_aState{};
63  std::string m_osToken{};
64  enum class ArrayState
65  {
66  INIT,
67  AFTER_COMMA,
68  AFTER_VALUE
69  };
70  std::vector<ArrayState> m_abArrayState{};
71  bool m_bInStringEscape = false;
72  bool m_bInUnicode = false;
73  std::string m_osUnicodeHex{};
74  size_t m_nMaxDepth = 1024;
75  size_t m_nMaxStringSize = 10000000;
76 
77  enum MemberState
78  {
79  WAITING_KEY,
80  IN_KEY,
81  KEY_FINISHED,
82  IN_VALUE
83  };
84 
85  std::vector<MemberState> m_aeObjectState{};
86 
87  enum State currentState()
88  {
89  return m_aState.back();
90  }
91 
92  void SkipSpace(const char *&pStr, size_t &nLength);
93  void AdvanceChar(const char *&pStr, size_t &nLength);
94  bool EmitUnexpectedChar(char ch, const char *pszExpecting = nullptr);
95  bool StartNewToken(const char *&pStr, size_t &nLength);
96  bool CheckAndEmitTrueFalseOrNull(char ch);
97  bool CheckStackEmpty();
98  void DecodeUnicode();
99 
100  protected:
101  bool EmitException(const char *pszMessage);
102  void StopParsing();
103 
104  public:
105  CPLJSonStreamingParser();
106  virtual ~CPLJSonStreamingParser();
107 
108  void SetMaxDepth(size_t nVal);
109  void SetMaxStringSize(size_t nVal);
110 
111  bool ExceptionOccurred() const
112  {
113  return m_bExceptionOccurred;
114  }
115 
116  static std::string GetSerializedString(const char *pszStr);
117 
118  virtual void Reset();
119  virtual bool Parse(const char *pStr, size_t nLength, bool bFinished);
120 
121  virtual void String(const char * /*pszValue*/, size_t /*nLength*/)
122  {
123  }
124 
125  virtual void Number(const char * /*pszValue*/, size_t /*nLength*/)
126  {
127  }
128 
129  virtual void Boolean(bool /*b*/)
130  {
131  }
132 
133  virtual void Null()
134  {
135  }
136 
137  virtual void StartObject()
138  {
139  }
140 
141  virtual void EndObject()
142  {
143  }
144 
145  virtual void StartObjectMember(const char * /*pszKey*/, size_t /*nLength*/)
146  {
147  }
148 
149  virtual void StartArray()
150  {
151  }
152 
153  virtual void EndArray()
154  {
155  }
156 
157  virtual void StartArrayMember()
158  {
159  }
160 
161  virtual void Exception(const char * /*pszMessage*/)
162  {
163  }
164 };
165 
166 #endif // __cplusplus
167 
170 #endif // CPL_JSON_STREAMIN_PARSER_H
Core portability definitions for CPL.
#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:1042