Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SerializationJson.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Compiler.h"
5#ifndef SC_EXPORT_LIBRARY_SERIALIZATION_TEXT
6#define SC_EXPORT_LIBRARY_SERIALIZATION_TEXT 0
7#endif
8#define SC_SERIALIZATION_TEXT_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_SERIALIZATION_TEXT)
9
10#include "../Strings/StringFormat.h" //StringFormatOutput
11#include "Internal/SerializationTextReadVersioned.h"
12#include "Internal/SerializationTextReadWriteExact.h"
13
14namespace SC
15{
16struct SC_SERIALIZATION_TEXT_EXPORT SerializationJson;
17} // namespace SC
18
24
27
50{
52 struct SC_SERIALIZATION_TEXT_EXPORT Options
53 {
55 Options() { floatDigits = 2; }
56 };
57
67 template <typename T, typename B>
68 [[nodiscard]] static bool write(T& object, B& buffer, Options options = Options())
69 {
70 GrowableBuffer<B> gb = {buffer};
71 StringFormatOutput output(StringEncoding::Ascii, gb);
72
73 Writer stream(output, options);
74 if (not stream.onSerializationStart())
75 return false;
76 if (not Serialization::SerializationTextReadWriteExact<Writer, T>::serialize(0, object, stream))
77 return false;
78 return stream.onSerializationEnd();
79 }
80
90 template <typename T>
91 [[nodiscard]] static bool loadExact(T& object, StringView text)
92 {
93 Reader stream(text);
94 return Serialization::SerializationTextReadWriteExact<Reader, T>::serialize(0, object, stream);
95 }
96
103 template <typename T>
104 [[nodiscard]] static bool loadVersioned(T& object, StringView text)
105 {
106 Reader stream(text);
107 return Serialization::SerializationTextReadVersioned<Reader, T, void>::loadVersioned(0, object, stream);
108 }
109
110 private:
113 struct SC_SERIALIZATION_TEXT_EXPORT Writer
114 {
115 StringFormatOutput& output;
116
117 Writer(StringFormatOutput& output, Options options) : output(output), options(options) {}
118
119 [[nodiscard]] bool onSerializationStart();
120 [[nodiscard]] bool onSerializationEnd();
121
122 [[nodiscard]] bool setOptions(Options opt);
123
124 [[nodiscard]] bool startObject(uint32_t index);
125 [[nodiscard]] bool endObject();
126
127 [[nodiscard]] bool startArray(uint32_t index);
128 [[nodiscard]] bool endArray();
129
130 template <typename Container>
131 [[nodiscard]] bool startArray(uint32_t index, Container& container, uint32_t& size)
132 {
133 if (not eventuallyAddComma(index))
134 return false;
135 size = static_cast<uint32_t>(container.size());
136 return output.append("["_a8);
137 }
138
139 template <typename Container>
140 [[nodiscard]] bool endArrayItem(Container&, uint32_t&)
141 {
142 return true;
143 }
144
145 [[nodiscard]] bool startObjectField(uint32_t index, StringView text);
146
147 template <typename T>
148 [[nodiscard]] bool serialize(uint32_t index, T& text)
149 {
150 return serializeStringView(index, text.view());
151 }
152
153 [[nodiscard]] bool serialize(uint32_t index, float value);
154 [[nodiscard]] bool serialize(uint32_t index, double value);
155 [[nodiscard]] bool serialize(uint32_t index, int value);
156
157 private:
158 [[nodiscard]] bool serializeStringView(uint32_t index, StringView text);
159
160 bool eventuallyAddComma(uint32_t index);
161
162 char floatFormatStorage[5];
163 StringSpan floatFormat;
164 Options options;
165 };
166
169 struct SC_SERIALIZATION_TEXT_EXPORT Reader
170 {
171 Reader(StringView text) : iteratorText(text), iterator(text.getIterator<StringIteratorASCII>()) {}
172
173 [[nodiscard]] bool onSerializationStart() { return true; }
174 [[nodiscard]] bool onSerializationEnd() { return true; }
175
176 [[nodiscard]] bool startObject(uint32_t index);
177 [[nodiscard]] bool endObject();
178
179 [[nodiscard]] bool startArray(uint32_t index);
180 [[nodiscard]] bool endArray();
181
182 template <typename Container>
183 [[nodiscard]] bool startArray(uint32_t index, Container& container, uint32_t& size)
184 {
185 if (not tokenizeArrayStart(index))
186 return false;
187 return endArrayItem(container, size);
188 }
189
190 template <typename Container>
191 [[nodiscard]] bool endArrayItem(Container& container, uint32_t& size)
192 {
193 auto oldSize = size;
194 if (not tokenizeArrayEnd(size))
195 return false;
196 if (oldSize != size)
197 return Reflection::ExtendedTypeInfo<Container>::resize(container, size);
198 return true;
199 }
200
201 [[nodiscard]] bool startObjectField(uint32_t index, StringView text);
202 [[nodiscard]] bool getNextField(uint32_t index, StringSpan& text, bool& hasMore);
203
204 [[nodiscard]] bool serialize(uint32_t index, bool& value);
205 [[nodiscard]] bool serialize(uint32_t index, float& value);
206 [[nodiscard]] bool serialize(uint32_t index, int32_t& value);
207
208 template <typename T>
209 [[nodiscard]] bool serialize(uint32_t index, T& text)
210 {
211 bool succeeded;
212 auto result = serializeInternal(index, succeeded);
213 return succeeded and text.assign(result);
214 }
215
216 private:
217 [[nodiscard]] StringView serializeInternal(uint32_t index, bool& succeeded);
218
219 [[nodiscard]] bool tokenizeArrayStart(uint32_t index);
220 [[nodiscard]] bool tokenizeArrayEnd(uint32_t& size);
221 [[nodiscard]] bool eventuallyExpectComma(uint32_t index);
222
223 StringView iteratorText;
224 StringIteratorASCII iterator;
225 };
226};
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:27
Formatting options.
Definition SerializationJson.h:53
uint8_t floatDigits
How many digits should be used when printing floating points.
Definition SerializationJson.h:54
SC::SerializationJson reads or writes C++ structures to / from json using Reflection information.
Definition SerializationJson.h:50
static bool loadVersioned(T &object, StringView text)
Parses a JSON buffer and writes C++ objects supporting reordered or missing fields.
Definition SerializationJson.h:104
static bool loadExact(T &object, StringView text)
Parses a JSON produced by SerializationJson::write loading its values into a C++ object Read a JSON b...
Definition SerializationJson.h:91
static bool write(T &object, B &buffer, Options options=Options())
Writes a C++ object to JSON using Reflection.
Definition SerializationJson.h:68
Definition StringFormat.h:27
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:47