cesium-native 0.43.0
Loading...
Searching...
No Matches
JsonWriter.h
1#pragma once
2
3#include <rapidjson/stringbuffer.h>
4#include <rapidjson/writer.h>
5
6#include <cstddef>
7#include <cstdint>
8#include <functional>
9#include <memory>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace CesiumJsonWriter {
19public:
20 JsonWriter();
21 virtual ~JsonWriter() {}
22
27 virtual bool Null();
33 virtual bool Bool(bool b);
39 virtual bool Int(int i);
45 virtual bool Uint(unsigned int i);
51 virtual bool Uint64(std::uint64_t i);
57 virtual bool Int64(std::int64_t i);
63 virtual bool Double(double d);
73 virtual bool RawNumber(const char* str, unsigned int length, bool copy);
80 virtual bool Key(std::string_view string);
87 virtual bool String(std::string_view string);
93 virtual bool StartObject();
99 virtual bool EndObject();
105 virtual bool StartArray();
111 virtual bool EndArray();
112
118 virtual void Primitive(std::int32_t value);
124 virtual void Primitive(std::uint32_t value);
130 virtual void Primitive(std::int64_t value);
136 virtual void Primitive(std::uint64_t value);
142 virtual void Primitive(float value);
148 virtual void Primitive(double value);
154 virtual void Primitive(std::nullptr_t value);
160 virtual void Primitive(std::string_view string);
161
169 virtual void KeyPrimitive(std::string_view keyName, std::int32_t value);
177 virtual void KeyPrimitive(std::string_view keyName, std::uint32_t value);
185 virtual void KeyPrimitive(std::string_view keyName, std::int64_t value);
193 virtual void KeyPrimitive(std::string_view keyName, std::uint64_t value);
201 virtual void KeyPrimitive(std::string_view keyName, std::string_view value);
209 virtual void KeyPrimitive(std::string_view keyName, float value);
217 virtual void KeyPrimitive(std::string_view keyName, double value);
225 virtual void KeyPrimitive(std::string_view keyName, std::nullptr_t value);
226
237 virtual void
238 KeyArray(std::string_view keyName, std::function<void(void)> insideArray);
249 virtual void
250 KeyObject(std::string_view keyName, std::function<void(void)> insideObject);
251
255 virtual std::string toString();
259 virtual std::string_view toStringView();
263 virtual std::vector<std::byte> toBytes();
264
268 template <typename ErrorStr> void emplaceError(ErrorStr&& error) {
269 _errors.emplace_back(std::forward<ErrorStr>(error));
270 }
271
275 template <typename WarningStr> void emplaceWarning(WarningStr&& warning) {
276 _warnings.emplace_back(std::forward<WarningStr>(warning));
277 }
278
282 const std::vector<std::string>& getErrors() const { return _errors; }
286 const std::vector<std::string>& getWarnings() const { return _warnings; }
287
288private:
289 rapidjson::StringBuffer _compactBuffer;
290 std::unique_ptr<rapidjson::Writer<rapidjson::StringBuffer>> _compact;
291
292 std::vector<std::string> _errors;
293 std::vector<std::string> _warnings;
294};
295} // namespace CesiumJsonWriter
Wrapper around rapidjson::Writer for writing objects to JSON.
Definition JsonWriter.h:18
virtual void KeyPrimitive(std::string_view keyName, double value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual bool Int(int i)
Writes a signed integer value to the output.
virtual void Primitive(std::uint32_t value)
Writes the given primitive to the output. This is a convenience function for Uint.
virtual void KeyPrimitive(std::string_view keyName, std::uint64_t value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
void emplaceWarning(WarningStr &&warning)
Emplaces a new warning into the internal warning buffer.
Definition JsonWriter.h:275
const std::vector< std::string > & getErrors() const
Obtains the current error buffer.
Definition JsonWriter.h:282
virtual bool StartObject()
Writes the start of a JSON object to the output.
virtual void Primitive(std::int32_t value)
Writes the given primitive to the output. This is a convenience function for Int.
virtual void Primitive(std::nullptr_t value)
Writes the given primitive to the output. This is a convenience function for Null.
virtual bool Double(double d)
Writes a 64-bit floating point value to the output.
virtual void KeyPrimitive(std::string_view keyName, float value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual void Primitive(float value)
Writes the given primitive to the output. This is a convenience function for Double.
virtual void KeyPrimitive(std::string_view keyName, std::string_view value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual void KeyArray(std::string_view keyName, std::function< void(void)> insideArray)
Writes an array to the output with the given key and calls the provided callback to write values insi...
virtual bool EndObject()
Writes the end of a JSON object to the output.
virtual bool Bool(bool b)
Writes a boolean value to the output.
void emplaceError(ErrorStr &&error)
Emplaces a new error into the internal error buffer.
Definition JsonWriter.h:268
virtual void KeyObject(std::string_view keyName, std::function< void(void)> insideObject)
Writes an object to the output with the given key and calls the provided callback to write values ins...
virtual bool RawNumber(const char *str, unsigned int length, bool copy)
Writes the given string as a number to the output without any kind of special handling.
virtual std::string toString()
Obtains the written output as a string.
virtual void Primitive(double value)
Writes the given primitive to the output. This is a convenience function for Double.
virtual bool Key(std::string_view string)
Writes the given string as an object key to the output.
virtual void KeyPrimitive(std::string_view keyName, std::int64_t value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual bool Uint64(std::uint64_t i)
Writes an unsigned 64-bit integer value to the output.
virtual void Primitive(std::int64_t value)
Writes the given primitive to the output. This is a convenience function for Int64.
virtual void Primitive(std::string_view string)
Writes the given primitive to the output. This is a convenience function for String.
virtual bool EndArray()
Writes the end of a JSON array to the output.
virtual void Primitive(std::uint64_t value)
Writes the given primitive to the output. This is a convenience function for Uint64.
virtual void KeyPrimitive(std::string_view keyName, std::nullptr_t value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual bool Null()
Writes a null value to the output.
virtual bool String(std::string_view string)
Writes the given string as a value to the output.
virtual void KeyPrimitive(std::string_view keyName, std::uint32_t value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual void KeyPrimitive(std::string_view keyName, std::int32_t value)
Writes the given key and its corresponding value primitive to the output. This is a convenience funct...
virtual std::string_view toStringView()
Obtains the written output as a string_view.
const std::vector< std::string > & getWarnings() const
Obtains the current warning buffer.
Definition JsonWriter.h:286
virtual bool Uint(unsigned int i)
Writes an unsigned integer value to the output.
virtual std::vector< std::byte > toBytes()
Obtains the written output as a buffer of bytes.
virtual bool Int64(std::int64_t i)
Writes an signed 64-bit integer value to the output.
virtual bool StartArray()
Writes the start of a JSON array to the output.
Classes for writing JSON.