cesium-native 0.43.0
Loading...
Searching...
No Matches
JsonReader.h
1#pragma once
2
3#include "JsonHandler.h"
4#include "Library.h"
5
6#include <rapidjson/document.h>
7
8#include <cstddef>
9#include <optional>
10#include <span>
11#include <string>
12#include <vector>
13
14namespace rapidjson {
15struct MemoryStream;
16}
17
18namespace CesiumJsonReader {
19
23template <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
43class CESIUMJSONREADER_API JsonReader {
44public:
58 template <typename T>
60 readJson(const std::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
119private:
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 std::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
A dummy implementation of IJsonHandler that will report a warning and return its parent when any of i...
Definition JsonHandler.h:19
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 std::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 JsonReader::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