SDTS_AL
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
40class CPL_DLL CPLJSonStreamingParser
41{
42 enum State
43 {
44 INIT,
45 OBJECT,
46 ARRAY,
47 STRING,
48 NUMBER,
49 STATE_TRUE,
50 STATE_FALSE,
51 STATE_NULL
52 };
53
54 bool m_bExceptionOccurred;
55 bool m_bElementFound;
56 int m_nLastChar;
57 int m_nLineCounter;
58 int m_nCharCounter;
59 std::vector<State> m_aState;
60 std::string m_osToken;
61 std::vector<bool> m_abFirstElement;
62 bool m_bInStringEscape;
63 bool m_bInUnicode;
64 std::string m_osUnicodeHex;
65 size_t m_nMaxDepth;
66 size_t m_nMaxStringSize;
67
68 enum MemberState
69 {
70 WAITING_KEY,
71 IN_KEY,
72 KEY_FINISHED,
73 IN_VALUE
74 };
75 std::vector<MemberState> m_aeObjectState;
76
77 enum State currentState() { return m_aState.back(); }
78 void SkipSpace(const char*& pStr, size_t& nLength);
79 void AdvanceChar(const char*& pStr, size_t& nLength);
80 bool EmitException(const char* pszMessage);
81 bool EmitUnexpectedChar(char ch);
82 bool StartNewToken(const char*& pStr, size_t& nLength);
83 bool CheckAndEmitTrueFalseOrNull(char ch);
84 bool CheckStackEmpty();
85 void DecodeUnicode();
86
87 public:
88 CPLJSonStreamingParser();
89 virtual ~CPLJSonStreamingParser();
90
91 void SetMaxDepth(size_t nVal);
92 void SetMaxStringSize(size_t nVal);
93 bool ExceptionOccurred() const { return m_bExceptionOccurred; }
94
95 static std::string GetSerializedString(const char* pszStr);
96
97 virtual void Reset();
98 virtual bool Parse(const char* pStr, size_t nLength, bool bFinished);
99
100 virtual void String(const char* /*pszValue*/, size_t /*nLength*/) {}
101 virtual void Number(const char* /*pszValue*/, size_t /*nLength*/) {}
102 virtual void Boolean(bool /*b*/) {}
103 virtual void Null() {}
104
105 virtual void StartObject() {}
106 virtual void EndObject() {}
107 virtual void StartObjectMember(const char* /*pszKey*/, size_t /*nLength*/) {}
108
109 virtual void StartArray() {}
110 virtual void EndArray() {}
111 virtual void StartArrayMember() {}
112
113 virtual void Exception(const char* /*pszMessage*/) {}
114};
115
116#endif // __cplusplus
117
120#endif // CPL_JSON_STREAMIN_PARSER_H