Coverage Report

Created: 2025-11-15 08:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/port/cpl_json_streaming_parser.h
Line
Count
Source
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
16
/*! @cond Doxygen_Suppress */
17
18
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
19
20
#include <vector>
21
#include <string>
22
#include <string_view>
23
#include "cpl_port.h"
24
25
class CPL_DLL CPLJSonStreamingParser /* non final */
26
{
27
  public:
28
    CPLJSonStreamingParser();
29
    virtual ~CPLJSonStreamingParser();
30
31
    void SetMaxDepth(size_t nVal);
32
    void SetMaxStringSize(size_t nVal);
33
34
    bool ExceptionOccurred() const
35
3.74k
    {
36
3.74k
        return m_bExceptionOccurred;
37
3.74k
    }
38
39
    static std::string GetSerializedString(std::string_view sStr);
40
41
    virtual void Reset();
42
    virtual bool Parse(std::string_view sStr, bool bFinished);
43
44
  protected:
45
    bool EmitException(const char *pszMessage);
46
    void StopParsing();
47
48
    virtual void String(std::string_view /* sValue */)
49
8.37k
    {
50
8.37k
    }
51
52
    virtual void Number(std::string_view /* sValue */)
53
38.0k
    {
54
38.0k
    }
55
56
    virtual void Boolean(bool /*b*/)
57
5.91k
    {
58
5.91k
    }
59
60
    virtual void Null()
61
14.4k
    {
62
14.4k
    }
63
64
    virtual void StartObject()
65
0
    {
66
0
    }
67
68
    virtual void EndObject()
69
0
    {
70
0
    }
71
72
    virtual void StartObjectMember(std::string_view /* sKey */)
73
0
    {
74
0
    }
75
76
    virtual void StartArray()
77
0
    {
78
0
    }
79
80
    virtual void EndArray()
81
0
    {
82
0
    }
83
84
    virtual void StartArrayMember()
85
61.6k
    {
86
61.6k
    }
87
88
    virtual void Exception(const char * /*pszMessage*/)
89
9.81k
    {
90
9.81k
    }
91
92
  private:
93
    CPL_DISALLOW_COPY_ASSIGN(CPLJSonStreamingParser)
94
95
    enum State
96
    {
97
        INIT,
98
        OBJECT,
99
        ARRAY,
100
        STRING,
101
        NUMBER,
102
        STATE_TRUE,
103
        STATE_FALSE,
104
        STATE_NULL
105
    };
106
107
    bool m_bExceptionOccurred = false;
108
    bool m_bElementFound = false;
109
    bool m_bStopParsing = false;
110
    int m_nLastChar = 0;
111
    int m_nLineCounter = 1;
112
    int m_nCharCounter = 1;
113
    std::vector<State> m_aState{};
114
    std::string m_osToken{};
115
    enum class ArrayState
116
    {
117
        INIT,
118
        AFTER_COMMA,
119
        AFTER_VALUE
120
    };
121
    std::vector<ArrayState> m_abArrayState{};
122
    bool m_bInStringEscape = false;
123
    bool m_bInUnicode = false;
124
    std::string m_osUnicodeHex{};
125
    size_t m_nMaxDepth = 1024;
126
    size_t m_nMaxStringSize = 10000000;
127
128
    enum MemberState
129
    {
130
        WAITING_KEY,
131
        IN_KEY,
132
        KEY_FINISHED,
133
        IN_VALUE
134
    };
135
136
    std::vector<MemberState> m_aeObjectState{};
137
138
    enum State currentState()
139
12.5M
    {
140
12.5M
        return m_aState.back();
141
12.5M
    }
142
143
    void SkipSpace(const char *&pStr, size_t &nLength);
144
    void AdvanceChar(const char *&pStr, size_t &nLength);
145
    bool EmitUnexpectedChar(char ch, const char *pszExpecting = nullptr);
146
    bool StartNewToken(const char *&pStr, size_t &nLength);
147
    bool CheckAndEmitTrueFalseOrNull(char ch);
148
    bool CheckStackEmpty();
149
    void DecodeUnicode();
150
};
151
152
#endif  // __cplusplus
153
154
/*! @endcond */
155
156
#endif  // CPL_JSON_STREAMIN_PARSER_H