cesium-native  0.41.0
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 
11 namespace CesiumJsonReader {
12 template <typename T, typename THandler>
13 class CESIUMJSONREADER_API DictionaryJsonHandler : public ObjectJsonHandler {
14 public:
15  template <typename... Ts>
16  DictionaryJsonHandler(Ts&&... args) noexcept
17  : ObjectJsonHandler(), _item(std::forward<Ts>(args)...) {}
18 
19  void reset(
20  IJsonHandler* pParent,
21  std::unordered_map<std::string, T>* pDictionary) {
22  ObjectJsonHandler::reset(pParent);
23  this->_pDictionary1 = pDictionary;
24  }
25 
26  void reset(IJsonHandler* pParent, std::map<std::string, T>* pDictionary) {
27  ObjectJsonHandler::reset(pParent);
28  this->_pDictionary2 = pDictionary;
29  }
30 
31  virtual IJsonHandler* readObjectKey(const std::string_view& str) override {
32  CESIUM_ASSERT(this->_pDictionary1 || this->_pDictionary2);
33 
34  if (this->_pDictionary1) {
35  auto it = this->_pDictionary1->emplace(str, T()).first;
36 
37  return this->property(it->first.c_str(), this->_item, it->second);
38  }
39 
40  auto it = this->_pDictionary2->emplace(str, T()).first;
41 
42  return this->property(it->first.c_str(), this->_item, it->second);
43  }
44 
45 private:
46  std::unordered_map<std::string, T>* _pDictionary1 = nullptr;
47  std::map<std::string, T>* _pDictionary2 = nullptr;
48  THandler _item;
49 };
50 } // namespace CesiumJsonReader
Classes for reading JSON.