Plasma Engine  2.0
Loading...
Searching...
No Matches
JSONWriter.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Containers/HybridArray.h>
5#include <Foundation/IO/Stream.h>
6#include <Foundation/Types/Variant.h>
7
11class PL_FOUNDATION_DLL plJSONWriter
12{
13public:
15 enum class WhitespaceMode
16 {
17 All,
18 LessIndentation,
19 NoIndentation,
20 NewlinesOnly,
21 None,
22 };
23
25 enum class ArrayMode
26 {
27 InOneLine,
28 OneLinePerItem,
29 };
30
33
35 virtual ~plJSONWriter();
36
38 void SetWhitespaceMode(WhitespaceMode whitespaceMode) { m_WhitespaceMode = whitespaceMode; }
39
41 void SetArrayMode(ArrayMode arrayMode) { m_ArrayMode = arrayMode; }
42
44 void AddVariableBool(plStringView sName, bool value); // [tested]
45
47 void AddVariableInt32(plStringView sName, plInt32 value); // [tested]
48
50 void AddVariableUInt32(plStringView sName, plUInt32 value); // [tested]
51
53 void AddVariableInt64(plStringView sName, plInt64 value); // [tested]
54
56 void AddVariableUInt64(plStringView sName, plUInt64 value); // [tested]
57
59 void AddVariableFloat(plStringView sName, float value); // [tested]
60
62 void AddVariableDouble(plStringView sName, double value); // [tested]
63
65 void AddVariableString(plStringView sName, plStringView value); // [tested]
66
68 void AddVariableNULL(plStringView sName); // [tested]
69
71 void AddVariableTime(plStringView sName, plTime value); // [tested]
72
74 void AddVariableUuid(plStringView sName, plUuid value); // [tested]
75
77 void AddVariableAngle(plStringView sName, plAngle value); // [tested]
78
80 void AddVariableColor(plStringView sName, const plColor& value); // [tested]
81
83 void AddVariableColorGamma(plStringView sName, const plColorGammaUB& value); // [tested]
84
86 void AddVariableVec2(plStringView sName, const plVec2& value); // [tested]
87
89 void AddVariableVec3(plStringView sName, const plVec3& value); // [tested]
90
92 void AddVariableVec4(plStringView sName, const plVec4& value); // [tested]
93
95 void AddVariableVec2I32(plStringView sName, const plVec2I32& value); // [tested]
96
98 void AddVariableVec3I32(plStringView sName, const plVec3I32& value); // [tested]
99
101 void AddVariableVec4I32(plStringView sName, const plVec4I32& value); // [tested]
102
104 void AddVariableQuat(plStringView sName, const plQuat& value); // [tested]
105
107 void AddVariableMat3(plStringView sName, const plMat3& value); // [tested]
108
110 void AddVariableMat4(plStringView sName, const plMat4& value); // [tested]
111
113 void AddVariableDataBuffer(plStringView sName, const plDataBuffer& value); // [tested]
114
116 void AddVariableVariant(plStringView sName, const plVariant& value); // [tested]
117
118
120 virtual void WriteBool(bool value) = 0;
121
123 virtual void WriteInt32(plInt32 value) = 0;
124
126 virtual void WriteUInt32(plUInt32 value) = 0;
127
129 virtual void WriteInt64(plInt64 value) = 0;
130
132 virtual void WriteUInt64(plUInt64 value) = 0;
133
135 virtual void WriteFloat(float value) = 0;
136
138 virtual void WriteDouble(double value) = 0;
139
141 virtual void WriteString(plStringView value) = 0;
142
144 virtual void WriteNULL() = 0;
145
147 virtual void WriteTime(plTime value) = 0;
148
152 virtual void WriteColor(const plColor& value) = 0;
153
157 virtual void WriteColorGamma(const plColorGammaUB& value) = 0;
158
162 virtual void WriteVec2(const plVec2& value) = 0;
163
167 virtual void WriteVec3(const plVec3& value) = 0;
168
172 virtual void WriteVec4(const plVec4& value) = 0;
173
177 virtual void WriteVec2I32(const plVec2I32& value) = 0;
178
182 virtual void WriteVec3I32(const plVec3I32& value) = 0;
183
187 virtual void WriteVec4I32(const plVec4I32& value) = 0;
188
192 virtual void WriteQuat(const plQuat& value) = 0;
193
197 virtual void WriteMat3(const plMat3& value) = 0;
198
202 virtual void WriteMat4(const plMat4& value) = 0;
203
207 virtual void WriteUuid(const plUuid& value) = 0;
208
212 virtual void WriteAngle(plAngle value) = 0; // [tested]
213
217 virtual void WriteDataBuffer(const plDataBuffer& value) = 0; // [tested]
218
222 virtual void WriteVariant(const plVariant& value); // [tested]
223
228 virtual void WriteBinaryData(plStringView sDataType, const void* pData, plUInt32 uiBytes, plStringView sValueString = nullptr) = 0;
229
234 virtual void BeginVariable(plStringView sName) = 0;
235
237 virtual void EndVariable() = 0;
238
244 virtual void BeginArray(plStringView sName = nullptr) = 0;
245
247 virtual void EndArray() = 0;
248
254 virtual void BeginObject(plStringView sName = nullptr) = 0;
255
257 virtual void EndObject() = 0;
258
262 bool HadWriteError() const;
263
264protected:
265 WhitespaceMode m_WhitespaceMode = WhitespaceMode::All;
266 ArrayMode m_ArrayMode = ArrayMode::InOneLine;
267
269 void SetWriteErrorState();
270
271private:
272 bool m_bHadWriteError = false;
273};
274
275
283class PL_FOUNDATION_DLL plStandardJSONWriter : public plJSONWriter
284{
285public:
287 plStandardJSONWriter(); // [tested]
288
290 ~plStandardJSONWriter(); // [tested]
291
293 void SetOutputStream(plStreamWriter* pOutput); // [tested]
294
296 virtual void WriteBool(bool value) override; // [tested]
297
299 virtual void WriteInt32(plInt32 value) override; // [tested]
300
302 virtual void WriteUInt32(plUInt32 value) override; // [tested]
303
305 virtual void WriteInt64(plInt64 value) override; // [tested]
306
308 virtual void WriteUInt64(plUInt64 value) override; // [tested]
309
311 virtual void WriteFloat(float value) override; // [tested]
312
314 virtual void WriteDouble(double value) override; // [tested]
315
317 virtual void WriteString(plStringView value) override; // [tested]
318
320 virtual void WriteNULL() override; // [tested]
321
323 virtual void WriteTime(plTime value) override; // [tested]
324
326 virtual void WriteColor(const plColor& value) override; // [tested]
327
329 virtual void WriteColorGamma(const plColorGammaUB& value) override; // [tested]
330
332 virtual void WriteVec2(const plVec2& value) override; // [tested]
333
335 virtual void WriteVec3(const plVec3& value) override; // [tested]
336
338 virtual void WriteVec4(const plVec4& value) override; // [tested]
339
341 virtual void WriteVec2I32(const plVec2I32& value) override; // [tested]
342
344 virtual void WriteVec3I32(const plVec3I32& value) override; // [tested]
345
347 virtual void WriteVec4I32(const plVec4I32& value) override; // [tested]
348
350 virtual void WriteQuat(const plQuat& value) override; // [tested]
351
353 virtual void WriteMat3(const plMat3& value) override; // [tested]
354
356 virtual void WriteMat4(const plMat4& value) override; // [tested]
357
359 virtual void WriteUuid(const plUuid& value) override; // [tested]
360
362 virtual void WriteAngle(plAngle value) override; // [tested]
363
365 virtual void WriteDataBuffer(const plDataBuffer& value) override; // [tested]
366
369 virtual void WriteBinaryData(plStringView sDataType, const void* pData, plUInt32 uiBytes, plStringView sValueString = nullptr) override; // [tested]
370
372 virtual void BeginVariable(plStringView sName) override; // [tested]
373
375 virtual void EndVariable() override; // [tested]
376
378 virtual void BeginArray(plStringView sName = {}) override; // [tested]
379
381 virtual void EndArray() override; // [tested]
382
384 virtual void BeginObject(plStringView sName = {}) override; // [tested]
385
387 virtual void EndObject() override; // [tested]
388
389protected:
390 void End();
391
392 enum State
393 {
394 Invalid,
395 Empty,
396 Variable,
397 Object,
398 NamedObject,
399 Array,
400 NamedArray,
401 };
402
404 {
405 JSONState();
406
407 State m_State;
408 bool m_bRequireComma;
409 bool m_bValueWasWritten;
410 };
411
413 {
415 ~CommaWriter();
416
417 plStandardJSONWriter* m_pWriter;
418 };
419
420 void OutputString(plStringView s);
421 void OutputEscapedString(plStringView s);
422 void OutputIndentation();
423
424 plInt32 m_iIndentation;
425 plStreamWriter* m_pOutput;
426
427 plHybridArray<JSONState, 16> m_StateStack;
428};
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:99
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
The base class for JSON writers.
Definition JSONWriter.h:12
virtual void EndObject()=0
Ends outputting an object variable.
virtual void WriteUInt32(plUInt32 value)=0
Writes a uint32 to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begin...
virtual void BeginObject(plStringView sName=nullptr)=0
Begins outputting an object variable.
virtual void WriteBool(bool value)=0
Writes a bool to the JSON file. Can only be called between BeginVariable() / EndVariable() or BeginAr...
virtual void WriteAngle(plAngle value)=0
Writes an plAngle to the JSON file. Can only be called between BeginVariable() / EndVariable() or Beg...
virtual void BeginArray(plStringView sName=nullptr)=0
Begins outputting an array variable.
virtual void WriteTime(plTime value)=0
Writes a time value to the JSON file. Can only be called between BeginVariable() / EndVariable() or B...
void SetWhitespaceMode(WhitespaceMode whitespaceMode)
Configures how much whitespace is output.
Definition JSONWriter.h:38
virtual void WriteDouble(double value)=0
Writes a double to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begin...
virtual void WriteString(plStringView value)=0
Writes a string to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begin...
virtual void BeginVariable(plStringView sName)=0
Begins outputting a variable. szName is the variable name.
virtual ~plJSONWriter()
Destructor.
virtual void WriteUuid(const plUuid &value)=0
Writes an plUuid to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begi...
virtual void WriteBinaryData(plStringView sDataType, const void *pData, plUInt32 uiBytes, plStringView sValueString=nullptr)=0
Outputs a chunk of memory in some JSON form that can be interpreted as binary data when reading it ag...
virtual void WriteInt32(plInt32 value)=0
Writes an int32 to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begin...
void SetArrayMode(ArrayMode arrayMode)
Configures how arrays are written.
Definition JSONWriter.h:41
plJSONWriter()
Constructor.
virtual void WriteUInt64(plUInt64 value)=0
Writes a uint64 to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begin...
virtual void WriteNULL()=0
Writes the value 'null' to the JSON file. Can only be called between BeginVariable() / EndVariable() ...
virtual void EndVariable()=0
Ends outputting a variable.
virtual void WriteInt64(plInt64 value)=0
Writes an int64 to the JSON file. Can only be called between BeginVariable() / EndVariable() or Begin...
virtual void EndArray()=0
Ends outputting an array variable.
ArrayMode
Modes to configure how arrays are written.
Definition JSONWriter.h:26
WhitespaceMode
Modes to configure how much whitespace the JSON writer will output.
Definition JSONWriter.h:16
virtual void WriteFloat(float value)=0
Writes a float to the JSON file. Can only be called between BeginVariable() / EndVariable() or BeginA...
Implements a standard compliant JSON writer, all numbers are output as double values.
Definition JSONWriter.h:284
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
Definition JSONWriter.h:413
Definition JSONWriter.h:404
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12