cesium-native 0.43.0
Loading...
Searching...
No Matches
writeJsonExtensions.h
1#pragma once
2
3#include "CesiumJsonWriter/ExtensionWriterContext.h"
4#include "CesiumJsonWriter/JsonWriter.h"
5
6#include <spdlog/fmt/fmt.h>
7
8namespace CesiumJsonWriter {
9
23template <typename TExtended>
25 const TExtended& obj,
26 JsonWriter& jsonWriter,
27 const ExtensionWriterContext& context) {
28 if (obj.extensions.empty()) {
29 return;
30 }
31 jsonWriter.StartObject();
32 for (const auto& item : obj.extensions) {
33 auto handler = context.createExtensionHandler(
34 item.first,
35 item.second,
36 TExtended::TypeName);
37 if (handler) {
38 jsonWriter.Key(item.first);
39 handler(item.second, jsonWriter, context);
40 }
41 }
42 jsonWriter.EndObject();
43}
44
59template <typename TExtended>
61 const TExtended& obj,
62 JsonWriter& jsonWriter,
63 const ExtensionWriterContext& context) {
64 bool hasRegisteredExtensions = false;
65
66 for (const auto& item : obj.extensions) {
67 auto handler = context.createExtensionHandler(
68 item.first,
69 item.second,
70 TExtended::TypeName);
71 if (handler) {
73 } else if (
74 context.getExtensionState(item.first) != ExtensionState::Disabled) {
75 jsonWriter.emplaceWarning(fmt::format(
76 "Encountered unregistered extension {}. This extension will be "
77 "ignored. To silence this warning, disable the extension with "
78 "ExtensionWriterContext::setExtensionState.",
79 item.first));
80 }
81 }
82
84}
85} // namespace CesiumJsonWriter
A context for writing extensions where known extensions and their handlers can be registered.
ExtensionState getExtensionState(const std::string &extensionName) const
Returns whether an extension is enabled or disabled.
ExtensionHandler< std::any > createExtensionHandler(const std::string_view &extensionName, const std::any &obj, const std::string &extendedObjectType) const
Attempts to create an ExtensionHandler for the given object, returning nullptr if no handler could be...
Wrapper around rapidjson::Writer for writing objects to JSON.
Definition JsonWriter.h:18
void emplaceWarning(WarningStr &&warning)
Emplaces a new warning into the internal warning buffer.
Definition JsonWriter.h:275
virtual bool StartObject()
Writes the start of a JSON object to the output.
virtual bool EndObject()
Writes the end of a JSON object to the output.
virtual bool Key(std::string_view string)
Writes the given string as an object key to the output.
Classes for writing JSON.
bool hasRegisteredExtensions(const TExtended &obj, JsonWriter &jsonWriter, const ExtensionWriterContext &context)
Checks if the provided object has any extensions attached that have been registered with the provided...
@ Disabled
The extension is disabled.
void writeJsonExtensions(const TExtended &obj, JsonWriter &jsonWriter, const ExtensionWriterContext &context)
Writes the extensions attached to the provided object as a new JSON object.