cesium-native  0.41.0
JsonReaderOptions.h
1 #pragma once
2 
3 #include "IExtensionJsonHandler.h"
4 #include "Library.h"
5 
6 #include <functional>
7 #include <map>
8 #include <memory>
9 #include <optional>
10 #include <string>
11 #include <vector>
12 
13 namespace CesiumJsonReader {
14 
18 enum class ExtensionState {
26  Enabled,
27 
35  JsonOnly,
36 
42  Disabled
43 };
44 
48 class CESIUMJSONREADER_API JsonReaderOptions {
49 public:
57  return this->_captureUnknownProperties;
58  }
59 
66  void setCaptureUnknownProperties(bool value) {
67  this->_captureUnknownProperties = value;
68  }
69 
78  template <typename TExtended, typename TExtensionHandler>
79  void registerExtension(const std::string& extensionName) {
80  auto it =
81  this->_extensions.emplace(extensionName, ObjectTypeToHandler()).first;
82  it->second.insert_or_assign(
83  TExtended::TypeName,
84  ExtensionReaderFactory([](const JsonReaderOptions& context) {
85  return std::make_unique<TExtensionHandler>(context);
86  }));
87  }
88 
98  template <typename TExtended, typename TExtensionHandler>
100  auto it =
101  this->_extensions
102  .emplace(TExtensionHandler::ExtensionName, ObjectTypeToHandler())
103  .first;
104  it->second.insert_or_assign(
105  TExtended::TypeName,
106  ExtensionHandlerFactory([](const JsonReaderOptions& context) {
107  return std::make_unique<TExtensionHandler>(context);
108  }));
109  }
110 
118  ExtensionState getExtensionState(const std::string& extensionName) const;
119 
138  void
139  setExtensionState(const std::string& extensionName, ExtensionState newState);
140 
141  std::unique_ptr<IExtensionJsonHandler> createExtensionHandler(
142  const std::string_view& extensionName,
143  const std::string& extendedObjectType) const;
144 
145 private:
146  using ExtensionHandlerFactory =
147  std::function<std::unique_ptr<IExtensionJsonHandler>(
148  const JsonReaderOptions&)>;
149  using ObjectTypeToHandler = std::map<std::string, ExtensionHandlerFactory>;
150  using ExtensionNameMap = std::map<std::string, ObjectTypeToHandler>;
151 
152  ExtensionNameMap _extensions;
153  std::unordered_map<std::string, ExtensionState> _extensionStates;
154  bool _captureUnknownProperties = true;
155 };
156 
157 } // namespace CesiumJsonReader
Holds options for reading statically-typed data structures from JSON.
void setCaptureUnknownProperties(bool value)
Sets a value indicating whether the values of unknown properties are captured in the ExtensibleObject...
ExtensionState getExtensionState(const std::string &extensionName) const
Returns whether an extension is enabled or disabled.
void registerExtension(const std::string &extensionName)
Registers an extension for an object.
void setExtensionState(const std::string &extensionName, ExtensionState newState)
Enables or disables an extension.
void registerExtension()
Registers an extension for an object.
bool getCaptureUnknownProperties() const
Gets a value indicating whether the values of unknown properties are captured in the ExtensibleObject...
Classes for reading JSON.
ExtensionState
The state of an extension.
@ Enabled
The extension is enabled.
@ Disabled
The extension is disabled.
@ JsonOnly
The extension is enabled but will always be deserialized as a CesiumUtility::JsonValue.