cesium-native 0.48.0
Loading...
Searching...
No Matches
DictionaryJsonHandler.h
1#pragma once
2
3#include <CesiumJsonReader/IntegerJsonHandler.h>
4#include <CesiumJsonReader/Library.h>
5#include <CesiumJsonReader/ObjectJsonHandler.h>
6#include <CesiumUtility/Assert.h>
7
8#include <map>
9#include <unordered_map>
10
11namespace CesiumJsonReader {
19template <typename T, typename THandler>
20class CESIUMJSONREADER_API DictionaryJsonHandler : public ObjectJsonHandler {
21public:
26 template <typename... Ts>
27 DictionaryJsonHandler(Ts&&... args) noexcept
28 : ObjectJsonHandler(), _item(std::forward<Ts>(args)...) {}
29
40 void reset(
41 IJsonHandler* pParent,
42 std::unordered_map<std::string, T>* pDictionary) {
44 this->_pDictionary1 = pDictionary;
45 }
46
51 void reset(IJsonHandler* pParent, std::map<std::string, T>* pDictionary) {
53 this->_pDictionary2 = pDictionary;
54 }
55
57 virtual IJsonHandler* readObjectKey(const std::string_view& str) override {
58 CESIUM_ASSERT(this->_pDictionary1 || this->_pDictionary2);
59
60 if (this->_pDictionary1) {
61 auto it = this->_pDictionary1->emplace(str, T()).first;
62
63 return this->property(it->first.c_str(), this->_item, it->second);
64 }
65
66 auto it = this->_pDictionary2->emplace(str, T()).first;
67
68 return this->property(it->first.c_str(), this->_item, it->second);
69 }
70
71private:
72 std::unordered_map<std::string, T>* _pDictionary1 = nullptr;
73 std::map<std::string, T>* _pDictionary2 = nullptr;
74 THandler _item;
75};
76} // namespace CesiumJsonReader
virtual IJsonHandler * readObjectKey(const std::string_view &str) override
Called when the JSON parser encounters a key while reading an object.
void reset(IJsonHandler *pParent, std::unordered_map< std::string, T > *pDictionary)
Resets the parent of this IJsonHandler and sets the destination pointer of this handler to an std::un...
void reset(IJsonHandler *pParent, std::map< std::string, T > *pDictionary)
Resets the parent of this IJsonHandler and sets the destination pointer of this handler to an std::ma...
DictionaryJsonHandler(Ts &&... args) noexcept
Creates a new DictionaryJsonHandler, passing the specified arguments to the constructor of THandler.
Base interface for all JSON handlers. Types that need to be deserialized from JSON should implement I...
void reset(IJsonHandler *pParent)
Resets the parent IJsonHandler of this handler.
IJsonHandler * property(const char *currentKey, TAccessor &accessor, TProperty &value)
Called from IJsonHandler::readObjectKey to read a property into an object.
Classes for reading JSON.