cesium-native 0.43.0
Loading...
Searching...
No Matches
DictionaryJsonHandler.h
1#pragma once
2
3#include "CesiumUtility/Assert.h"
4#include "IntegerJsonHandler.h"
5#include "Library.h"
6#include "ObjectJsonHandler.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) {
43 ObjectJsonHandler::reset(pParent);
44 this->_pDictionary1 = pDictionary;
45 }
46
51 void reset(IJsonHandler* pParent, std::map<std::string, T>* pDictionary) {
52 ObjectJsonHandler::reset(pParent);
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
Reads the keys and values of a JSON object into a std::map<std::string, T> or an std::unordered_map<s...
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...
IJsonHandler for handling JSON objects.
Classes for reading JSON.