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
4#include "jsonwriter.h"
5
6#include <base/dbg.h>
7#include <base/io.h>
8#include <base/str.h>
9
10#include <engine/shared/json.h>
11
12CJsonWriter::CJsonWriter()
13{
14 m_Indentation = 0;
15}
16
17void CJsonWriter::BeginObject()
18{
19 dbg_assert(CanWriteDatatype(), "Cannot write object here");
20 WriteIndent(EndElement: false);
21 WriteInternal(pStr: "{");
22 PushState(NewState: STATE_OBJECT);
23}
24
25void CJsonWriter::EndObject()
26{
27 dbg_assert(TopState()->m_Kind == STATE_OBJECT, "Cannot end object here");
28 PopState();
29 CompleteDataType();
30 WriteIndent(EndElement: true);
31 WriteInternal(pStr: "}");
32}
33
34void CJsonWriter::BeginArray()
35{
36 dbg_assert(CanWriteDatatype(), "Cannot write array here");
37 WriteIndent(EndElement: false);
38 WriteInternal(pStr: "[");
39 PushState(NewState: STATE_ARRAY);
40}
41
42void CJsonWriter::EndArray()
43{
44 dbg_assert(TopState()->m_Kind == STATE_ARRAY, "Cannot end array here");
45 PopState();
46 CompleteDataType();
47 WriteIndent(EndElement: true);
48 WriteInternal(pStr: "]");
49}
50
51void CJsonWriter::WriteAttribute(const char *pName)
52{
53 dbg_assert(TopState()->m_Kind == STATE_OBJECT, "Cannot write attribute here");
54 WriteIndent(EndElement: false);
55 WriteInternalEscaped(pStr: pName);
56 WriteInternal(pStr: ": ");
57 PushState(NewState: STATE_ATTRIBUTE);
58}
59
60void CJsonWriter::WriteStrValue(const char *pValue)
61{
62 dbg_assert(CanWriteDatatype(), "Cannot write value here");
63 WriteIndent(EndElement: false);
64 WriteInternalEscaped(pStr: pValue);
65 CompleteDataType();
66}
67
68void CJsonWriter::WriteIntValue(int Value)
69{
70 dbg_assert(CanWriteDatatype(), "Cannot write value here");
71 WriteIndent(EndElement: false);
72 char aBuf[32];
73 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Value);
74 WriteInternal(pStr: aBuf);
75 CompleteDataType();
76}
77
78void CJsonWriter::WriteBoolValue(bool Value)
79{
80 dbg_assert(CanWriteDatatype(), "Cannot write value here");
81 WriteIndent(EndElement: false);
82 WriteInternal(pStr: Value ? "true" : "false");
83 CompleteDataType();
84}
85
86void CJsonWriter::WriteNullValue()
87{
88 dbg_assert(CanWriteDatatype(), "Cannot write value here");
89 WriteIndent(EndElement: false);
90 WriteInternal(pStr: "null");
91 CompleteDataType();
92}
93
94bool CJsonWriter::CanWriteDatatype()
95{
96 return m_States.empty() || TopState()->m_Kind == STATE_ARRAY || TopState()->m_Kind == STATE_ATTRIBUTE;
97}
98
99void CJsonWriter::WriteInternalEscaped(const char *pStr)
100{
101 dbg_assert(str_utf8_check(pStr), "CJsonWriter::WriteInternalEscaped: input must be valid UTF-8");
102
103 WriteInternal(pStr: "\"");
104 int UnwrittenFrom = 0;
105 int Length = str_length(str: pStr);
106 for(int i = 0; i < Length; i++)
107 {
108 char SimpleEscape = EscapeJsonChar(c: pStr[i]);
109 // Assuming ASCII/UTF-8, exactly everything below 0x20 is a
110 // control character.
111 bool NeedsEscape = SimpleEscape || (unsigned char)pStr[i] < 0x20;
112 if(NeedsEscape)
113 {
114 if(i - UnwrittenFrom > 0)
115 {
116 WriteInternal(pStr: pStr + UnwrittenFrom, Length: i - UnwrittenFrom);
117 }
118
119 if(SimpleEscape)
120 {
121 char aStr[2];
122 aStr[0] = '\\';
123 aStr[1] = SimpleEscape;
124 WriteInternal(pStr: aStr, Length: sizeof(aStr));
125 }
126 else
127 {
128 char aStr[7];
129 str_format(buffer: aStr, buffer_size: sizeof(aStr), format: "\\u%04x", pStr[i]);
130 WriteInternal(pStr: aStr);
131 }
132 UnwrittenFrom = i + 1;
133 }
134 }
135 if(Length - UnwrittenFrom > 0)
136 {
137 WriteInternal(pStr: pStr + UnwrittenFrom, Length: Length - UnwrittenFrom);
138 }
139 WriteInternal(pStr: "\"");
140}
141
142void CJsonWriter::WriteIndent(bool EndElement)
143{
144 const bool NotRootOrAttribute = !m_States.empty() && TopState()->m_Kind != STATE_ATTRIBUTE;
145
146 if(NotRootOrAttribute && !TopState()->m_Empty && !EndElement)
147 WriteInternal(pStr: ",");
148
149 if(NotRootOrAttribute || EndElement)
150 WriteInternal(pStr: "\n");
151
152 if(NotRootOrAttribute)
153 for(int i = 0; i < m_Indentation; i++)
154 WriteInternal(pStr: "\t");
155}
156
157void CJsonWriter::PushState(EJsonStateKind NewState)
158{
159 if(!m_States.empty())
160 {
161 m_States.top().m_Empty = false;
162 }
163 m_States.emplace(args&: NewState);
164 if(NewState != STATE_ATTRIBUTE)
165 {
166 m_Indentation++;
167 }
168}
169
170CJsonWriter::SState *CJsonWriter::TopState()
171{
172 dbg_assert(!m_States.empty(), "json stack is empty");
173 return &m_States.top();
174}
175
176CJsonWriter::EJsonStateKind CJsonWriter::PopState()
177{
178 dbg_assert(!m_States.empty(), "json stack is empty");
179 SState TopState = m_States.top();
180 m_States.pop();
181 if(TopState.m_Kind != STATE_ATTRIBUTE)
182 {
183 m_Indentation--;
184 }
185 return TopState.m_Kind;
186}
187
188void CJsonWriter::CompleteDataType()
189{
190 if(!m_States.empty() && TopState()->m_Kind == STATE_ATTRIBUTE)
191 PopState(); // automatically complete the attribute
192
193 if(!m_States.empty())
194 TopState()->m_Empty = false;
195}
196
197CJsonFileWriter::CJsonFileWriter(IOHANDLE IO)
198{
199 dbg_assert((bool)IO, "IO handle invalid");
200 m_IO = IO;
201}
202
203CJsonFileWriter::~CJsonFileWriter()
204{
205 // Ensure newline at the end
206 WriteInternal(pStr: "\n");
207 io_close(io: m_IO);
208}
209
210void CJsonFileWriter::WriteInternal(const char *pStr, int Length)
211{
212 io_write(io: m_IO, buffer: pStr, size: Length < 0 ? str_length(str: pStr) : Length);
213}
214
215void CJsonStringWriter::WriteInternal(const char *pStr, int Length)
216{
217 dbg_assert(!m_RetrievedOutput, "Writer output has already been retrieved");
218 if(Length < 0)
219 {
220 m_OutputString.append(s: pStr);
221 }
222 else
223 {
224 m_OutputString.append(s: pStr, n: Length);
225 }
226}
227
228std::string &&CJsonStringWriter::GetOutputString()
229{
230 // Ensure newline at the end. Modify member variable so we can move it when returning.
231 WriteInternal(pStr: "\n");
232 m_RetrievedOutput = true; // prevent further usage of this writer
233 return std::move(m_OutputString);
234}
235