Plasma Engine  2.0
Loading...
Searching...
No Matches
JSONParser.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Containers/HybridArray.h>
5#include <Foundation/IO/Stream.h>
6
8
12class PL_FOUNDATION_DLL plJSONParser
13{
14public:
17
18 virtual ~plJSONParser() = default;
19
21 void SetLogInterface(plLogInterface* pLog) { m_pLogInterface = pLog; }
22
23protected:
25 void SetInputStream(plStreamReader& stream, plUInt32 uiFirstLineOffset = 0);
26
33 bool ContinueParsing();
34
36 void ParseAll();
37
40 void SkipObject();
41
44 void SkipArray();
45
47 void ParsingError(plStringView sMessage, bool bFatal);
48
49 plLogInterface* m_pLogInterface = nullptr;
50
51private:
57 virtual bool OnVariable(plStringView sVarName) = 0;
58
63 virtual void OnReadValue(plStringView sValue) = 0;
64
66 virtual void OnReadValue(double fValue) = 0;
67
69 virtual void OnReadValue(bool bValue) = 0;
70
72 virtual void OnReadValueNULL() = 0;
73
78 virtual void OnBeginObject() = 0;
79
81 virtual void OnEndObject() = 0;
82
87 virtual void OnBeginArray() = 0;
88
90 virtual void OnEndArray() = 0;
91
98 virtual void OnParsingError(plStringView sMessage, bool bFatal, plUInt32 uiLine, plUInt32 uiColumn) {}
99
100private:
101 enum State
102 {
103 NotStarted,
104 Finished,
105 ReadingObject,
106 ReadingArray,
107 ReadingValue,
108 ReadingVariable,
109 ExpectSeparator
110 };
111
112 struct JSONState
113 {
114 JSONState() { m_State = NotStarted; }
115
116 State m_State;
117 };
118
119 void StartParsing();
120 void SkipWhitespace();
121 void SkipString();
122 void ReadString();
123 double ReadNumber();
124 void ReadWord();
125
126 void ContinueObject();
127 void ContinueArray();
128 void ContinueVariable();
129 void ContinueValue();
130 void ContinueSeparator();
131
132 bool ReadCharacter(bool bSkipComments);
133 void ReadNextByte();
134
135 void SkipStack(State s);
136
137 plUInt8 m_uiCurByte;
138 plUInt8 m_uiNextByte;
139 plUInt32 m_uiCurLine;
140 plUInt32 m_uiCurColumn;
141
142 plStreamReader* m_pInput;
143 plHybridArray<JSONState, 32> m_StateStack;
144 plHybridArray<plUInt8, 4096> m_TempString;
145
146 bool m_bSkippingMode;
147};
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
A low level JSON parser that can incrementally parse the structure of a JSON document.
Definition JSONParser.h:13
virtual void OnEndArray()=0
Called when the end of an array is encountered.
virtual void OnBeginArray()=0
Called when a new array is encountered.
void SetLogInterface(plLogInterface *pLog)
Allows to specify an plLogInterface through which errors and warnings are reported.
Definition JSONParser.h:21
virtual void OnReadValue(double fValue)=0
Called whenever a new value is read.
virtual void OnParsingError(plStringView sMessage, bool bFatal, plUInt32 uiLine, plUInt32 uiColumn)
Called when something unexpected is encountered in the JSON document.
Definition JSONParser.h:98
virtual bool OnVariable(plStringView sVarName)=0
Called whenever a new variable is encountered. The variable name is passed along. At this point the t...
virtual void OnBeginObject()=0
Called when a new object is encountered.
virtual void OnEndObject()=0
Called when the end of an object is encountered.
virtual void OnReadValue(bool bValue)=0
Called whenever a new value is read.
virtual void OnReadValueNULL()=0
Called whenever a new value is read.
virtual void OnReadValue(plStringView sValue)=0
Called whenever a new value is read.
Base class for all logging classes.
Definition Log.h:77
Interface for binary in (read) streams.
Definition Stream.h:22
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34