cesium-native  0.41.0
JsonReader.h
1 #pragma once
2 
3 #include "JsonHandler.h"
4 #include "Library.h"
5 
6 #include <gsl/span>
7 #include <rapidjson/document.h>
8 
9 #include <cstddef>
10 #include <optional>
11 #include <string>
12 #include <vector>
13 
14 namespace rapidjson {
15 struct MemoryStream;
16 }
17 
18 namespace CesiumJsonReader {
19 
23 template <typename T> struct ReadJsonResult {
27  std::optional<T> value;
28 
32  std::vector<std::string> errors;
33 
37  std::vector<std::string> warnings;
38 };
39 
43 class CESIUMJSONREADER_API JsonReader {
44 public:
58  template <typename T>
60  readJson(const gsl::span<const std::byte>& data, T& handler) {
62 
63  result.value.emplace();
64 
65  FinalJsonHandler finalHandler(result.warnings);
66  handler.reset(&finalHandler, &result.value.value());
67 
68  JsonReader::internalRead(
69  data,
70  handler,
71  finalHandler,
72  result.errors,
73  result.warnings);
74 
75  if (!result.errors.empty()) {
76  result.value.reset();
77  }
78 
79  return result;
80  }
81 
95  template <typename T>
97  readJson(const rapidjson::Value& jsonValue, T& handler) {
99 
100  result.value.emplace();
101 
102  FinalJsonHandler finalHandler(result.warnings);
103  handler.reset(&finalHandler, &result.value.value());
104 
105  JsonReader::internalRead(
106  jsonValue,
107  handler,
108  finalHandler,
109  result.errors,
110  result.warnings);
111 
112  if (!result.errors.empty()) {
113  result.value.reset();
114  }
115 
116  return result;
117  }
118 
119 private:
120  class FinalJsonHandler : public JsonHandler {
121  public:
122  FinalJsonHandler(std::vector<std::string>& warnings);
123  virtual void reportWarning(
124  const std::string& warning,
125  std::vector<std::string>&& context) override;
126  void setInputStream(rapidjson::MemoryStream* pInputStream) noexcept;
127 
128  private:
129  std::vector<std::string>& _warnings;
130  rapidjson::MemoryStream* _pInputStream;
131  };
132 
133  static void internalRead(
134  const gsl::span<const std::byte>& data,
135  IJsonHandler& handler,
136  FinalJsonHandler& finalHandler,
137  std::vector<std::string>& errors,
138  std::vector<std::string>& warnings);
139 
140  static void internalRead(
141  const rapidjson::Value& jsonValue,
142  IJsonHandler& handler,
143  FinalJsonHandler& finalHandler,
144  std::vector<std::string>& errors,
145  std::vector<std::string>& warnings);
146 };
147 
148 } // namespace CesiumJsonReader
static ReadJsonResult< typename T::ValueType > readJson(const rapidjson::Value &jsonValue, T &handler)
Reads JSON from a rapidjson::Value into a statically-typed class.
Definition: JsonReader.h:97
static ReadJsonResult< typename T::ValueType > readJson(const gsl::span< const std::byte > &data, T &handler)
Reads JSON from a byte buffer into a statically-typed class.
Definition: JsonReader.h:60
Classes for reading JSON.
The result of Reader::readJson.
Definition: JsonReader.h:23
std::optional< T > value
The value read from the JSON, or std::nullopt on error.
Definition: JsonReader.h:27
std::vector< std::string > warnings
Warnings that occurred while reading.
Definition: JsonReader.h:37
std::vector< std::string > errors
Errors that occurred while reading.
Definition: JsonReader.h:32