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  int m_nLastChar = 0;
59  int m_nLineCounter = 1;
60  int m_nCharCounter = 1;
61  std::vector<State> m_aState{};
62  std::string m_osToken{};
63  std::vector<bool> m_abFirstElement{};
64  bool m_bInStringEscape = false;
65  bool m_bInUnicode = false;
66  std::string m_osUnicodeHex{};
67  size_t m_nMaxDepth = 1024;
68  size_t m_nMaxStringSize = 10000000;
69 
70  enum MemberState
71  {
72  WAITING_KEY,
73  IN_KEY,
74  KEY_FINISHED,
75  IN_VALUE
76  };
77  std::vector<MemberState> m_aeObjectState{};
78 
79  enum State currentState() { return m_aState.back(); }
80  void SkipSpace(const char*& pStr, size_t& nLength);
81  void AdvanceChar(const char*& pStr, size_t& nLength);
82  bool EmitException(const char* pszMessage);
83  bool EmitUnexpectedChar(char ch);
84  bool StartNewToken(const char*& pStr, size_t& nLength);
85  bool CheckAndEmitTrueFalseOrNull(char ch);
86  bool CheckStackEmpty();
87  void DecodeUnicode();
88 
89  public:
90  CPLJSonStreamingParser();
91  virtual ~CPLJSonStreamingParser();
92 
93  void SetMaxDepth(size_t nVal);
94  void SetMaxStringSize(size_t nVal);
95  bool ExceptionOccurred() const { return m_bExceptionOccurred; }
96 
97  static std::string GetSerializedString(const char* pszStr);
98 
99  virtual void Reset();
100  virtual bool Parse(const char* pStr, size_t nLength, bool bFinished);
101 
102  virtual void String(const char* /*pszValue*/, size_t /*nLength*/) {}
103  virtual void Number(const char* /*pszValue*/, size_t /*nLength*/) {}
104  virtual void Boolean(bool /*b*/) {}
105  virtual void Null() {}
106 
107  virtual void StartObject() {}
108  virtual void EndObject() {}
109  virtual void StartObjectMember(const char* /*pszKey*/, size_t /*nLength*/) {}
110 
111  virtual void StartArray() {}
112  virtual void EndArray() {}
113  virtual void StartArrayMember() {}
114 
115  virtual void Exception(const char* /*pszMessage*/) {}
116 };
117 
118 #endif // __cplusplus
119 
122 #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:989

Generated for GDAL by doxygen 1.8.8.