DDraceNetwork Docs
jsonwriter.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_JSONWRITER_H
4#define ENGINE_SHARED_JSONWRITER_H
5
6#include <base/types.h>
7
8#include <stack>
9#include <string>
10
15{
17 {
21 };
22
23 struct SState
24 {
26 bool m_Empty = true;
27
29 m_Kind(Kind)
30 {
31 }
32 };
33
34 std::stack<SState> m_States;
36
37 bool CanWriteDatatype();
38 void WriteInternalEscaped(const char *pStr);
39 void WriteIndent(bool EndElement);
40 void PushState(EJsonStateKind NewState);
43 void CompleteDataType();
44
45protected:
46 // String must be zero-terminated when Length is -1.
47 virtual void WriteInternal(const char *pStr, int Length = -1) = 0;
48
49public:
51 virtual ~CJsonWriter() = default;
52
53 // The root is created by beginning the first datatype (object, array, value).
54 // The writer must not be used after ending the root, which must be unique.
55
56 // Begin writing a new object
57 void BeginObject();
58 // End current object
59 void EndObject();
60
61 // Begin writing a new array
62 void BeginArray();
63 // End current array
64 void EndArray();
65
66 // Write attribute with the given name inside the current object.
67 // Names inside one object should be unique, but this is not checked here.
68 // Must be used to begin writing anything inside objects and only there.
69 // Must be followed by a datatype for the attribute value.
70 void WriteAttribute(const char *pName);
71
72 // Functions for writing value literals:
73 // - As array values in arrays.
74 // - As attribute values after beginning an attribute inside an object.
75 // - As root value (only once).
76 void WriteStrValue(const char *pValue);
77 void WriteIntValue(int Value);
78 void WriteBoolValue(bool Value);
79 void WriteNullValue();
80};
81
86{
88
89protected:
90 void WriteInternal(const char *pStr, int Length = -1) override;
91
92public:
99};
100
105{
106 std::string m_OutputString;
107 bool m_RetrievedOutput = false;
108
109protected:
110 void WriteInternal(const char *pStr, int Length = -1) override;
111
112public:
113 CJsonStringWriter() = default;
115 std::string &&GetOutputString();
116};
117
118#endif
Definition: jsonwriter.h:86
CJsonFileWriter(IOHANDLE IO)
Definition: jsonwriter.cpp:206
~CJsonFileWriter()
Definition: jsonwriter.cpp:212
IOHANDLE m_IO
Definition: jsonwriter.h:87
void WriteInternal(const char *pStr, int Length=-1) override
Definition: jsonwriter.cpp:219
Definition: jsonwriter.h:105
std::string && GetOutputString()
Definition: jsonwriter.cpp:230
std::string m_OutputString
Definition: jsonwriter.h:106
void WriteInternal(const char *pStr, int Length=-1) override
Definition: jsonwriter.cpp:224
bool m_RetrievedOutput
Definition: jsonwriter.h:107
CJsonStringWriter()=default
~CJsonStringWriter()=default
Definition: jsonwriter.h:15
void WriteIndent(bool EndElement)
Definition: jsonwriter.cpp:151
virtual void WriteInternal(const char *pStr, int Length=-1)=0
void CompleteDataType()
Definition: jsonwriter.cpp:197
void WriteInternalEscaped(const char *pStr)
Definition: jsonwriter.cpp:110
SState * TopState()
Definition: jsonwriter.cpp:179
void PushState(EJsonStateKind NewState)
Definition: jsonwriter.cpp:166
void WriteIntValue(int Value)
Definition: jsonwriter.cpp:79
int m_Indentation
Definition: jsonwriter.h:35
virtual ~CJsonWriter()=default
std::stack< SState > m_States
Definition: jsonwriter.h:34
void WriteAttribute(const char *pName)
Definition: jsonwriter.cpp:62
void WriteBoolValue(bool Value)
Definition: jsonwriter.cpp:89
CJsonWriter()
Definition: jsonwriter.cpp:23
void BeginArray()
Definition: jsonwriter.cpp:45
void BeginObject()
Definition: jsonwriter.cpp:28
EJsonStateKind PopState()
Definition: jsonwriter.cpp:185
void WriteStrValue(const char *pValue)
Definition: jsonwriter.cpp:71
void EndObject()
Definition: jsonwriter.cpp:36
void WriteNullValue()
Definition: jsonwriter.cpp:97
void EndArray()
Definition: jsonwriter.cpp:53
bool CanWriteDatatype()
Definition: jsonwriter.cpp:105
EJsonStateKind
Definition: jsonwriter.h:17
@ STATE_ARRAY
Definition: jsonwriter.h:19
@ STATE_ATTRIBUTE
Definition: jsonwriter.h:20
@ STATE_OBJECT
Definition: jsonwriter.h:18
void * IOHANDLE
Definition: logger.h:11
Definition: jsonwriter.h:24
bool m_Empty
Definition: jsonwriter.h:26
EJsonStateKind m_Kind
Definition: jsonwriter.h:25
SState(EJsonStateKind Kind)
Definition: jsonwriter.h:28