HoviTron Video Pipeline
Public Types | Public Member Functions | Static Public Member Functions
json::Node Class Reference

Public Types

enum class  Type {
  number , string , array , object ,
  boolean , null
}
 

Public Member Functions

void setOverrides (Node overrides)
 
Type type () const
 
Node optional (std::string const &key) const
 
Node require (std::string const &key) const
 
Node at (std::size_t index) const
 
std::size_t size () const
 
double asDouble () const
 
float asFloat () const
 
int asInt () const
 
std::string const & asString () const
 
bool asBool () const
 
 operator bool () const
 

Static Public Member Functions

static Node readFrom (std::istream &)
 

Detailed Description

Definition at line 25 of file JsonParser.h.

Member Enumeration Documentation

◆ Type

enum class json::Node::Type
strong

Definition at line 28 of file JsonParser.h.

29 {
30 number,
31 string,
32 array,
33 object,
34 boolean,
35 null
36 };

Member Function Documentation

◆ asBool()

bool json::Node::asBool ( ) const

Query the value of a boolean

Definition at line 322 of file JsonParser.cpp.

323 {
324 if (type() != Type::boolean) {
325 throw std::runtime_error("JSON parser: Expected a boolean");
326 }
327 return dynamic_cast<Bool&>(*m_value).value;
328 }
Type type() const
Definition: JsonParser.cpp:233

◆ asDouble()

double json::Node::asDouble ( ) const

Query the value of a number

Definition at line 290 of file JsonParser.cpp.

291 {
292 if (type() != Type::number) {
293 throw std::runtime_error("JSON parser: Expected a number");
294 }
295 return dynamic_cast<Number&>(*m_value).value;
296 }

◆ asFloat()

float json::Node::asFloat ( ) const

Query the value of a number

Definition at line 298 of file JsonParser.cpp.

299 {
300 return static_cast<float>(asDouble());
301 }
double asDouble() const
Definition: JsonParser.cpp:290

◆ asInt()

int json::Node::asInt ( ) const

Query the value of a number and require it to be an integer

Definition at line 303 of file JsonParser.cpp.

304 {
305 auto value = asDouble();
306 auto rounded = static_cast<int>(std::lround(value));
307 auto error = value - rounded;
308 if (error > 1e-6) {
309 throw std::runtime_error("JSON parser: Expected an integer value");
310 }
311 return rounded;
312 }

◆ asString()

std::string const & json::Node::asString ( ) const

Query the value of a string

Definition at line 314 of file JsonParser.cpp.

315 {
316 if (type() != Type::string) {
317 throw std::runtime_error("JSON parser: Expected a string");
318 }
319 return dynamic_cast<String&>(*m_value).value;
320 }

◆ at()

Node json::Node::at ( std::size_t  index) const

Query an array by index

Definition at line 269 of file JsonParser.cpp.

270 {
271 if (type() != Type::array) {
272 throw std::runtime_error("JSON parser: Expected an array");
273 }
274 return{ dynamic_cast<Array&>(*m_value).value.at(index) };
275 }

◆ operator bool()

json::Node::operator bool ( ) const

Anything apart from false and null is true

Definition at line 330 of file JsonParser.cpp.

331 {
332 switch (type()) {
333 case Type::null:
334 return false;
335 case Type::boolean:
336 return asBool();
337 default:
338 return true;
339 }
340 }
bool asBool() const
Definition: JsonParser.cpp:322

◆ optional()

Node json::Node::optional ( std::string const &  key) const

Query an object parameter, Null if not found

It is possible to specify overrides

Definition at line 238 of file JsonParser.cpp.

239 {
240 if (m_overrides) {
241 try {
242 return{ m_overrides->value.at(key) };
243 }
244 catch (std::out_of_range&) {}
245 }
246 try {
247 return{ dynamic_cast<Object&>(*m_value).value.at(key) };
248 }
249 catch (std::out_of_range&) {
250 return{ std::make_shared<Null>() };
251 }
252 catch (std::bad_cast&) {
253 std::ostringstream what;
254 what << "JSON parser: Querying optional key '" << key << "', but node is not an object";
255 throw std::runtime_error(what.str());
256 }
257 }

◆ readFrom()

Node json::Node::readFrom ( std::istream &  stream)
static

Read JSON from an input stream

Definition at line 203 of file JsonParser.cpp.

204 {
205 try {
206 stream.exceptions(std::ios::badbit | std::ios::failbit);
207 auto value = readValue(stream);
208 skipWhitespace(stream);
209
210 if (!stream.eof()) {
211 auto ch = stream.get();
212 std::ostringstream what;
213 what << "Stray character " << static_cast<char>(ch) << " (0x" << std::ios::hex << ch << ")";
214 throw std::runtime_error(what.str());
215 }
216
217 return{ value };
218 }
219 catch (std::runtime_error& e) {
220 throw std::runtime_error(std::string("JSON parser: ") + e.what());
221 }
222 catch (std::exception& e) {
223 throw std::logic_error(std::string("JSON parser bug: ") + e.what());
224 }
225 }

◆ require()

Node json::Node::require ( std::string const &  key) const

Query an object parameter, throws "parameter KEY is required" unless found

It is possible to specify overrides

Definition at line 259 of file JsonParser.cpp.

260 {
261 auto node = optional(key);
262 if (node)
263 return node;
264 std::ostringstream stream;
265 stream << "JSON parser: Parameter " << key << " is required but missing";
266 throw std::runtime_error(stream.str());
267 }
Node optional(std::string const &key) const
Definition: JsonParser.cpp:238

◆ setOverrides()

void json::Node::setOverrides ( Node  overrides)

Definition at line 227 of file JsonParser.cpp.

228 {
229 assert(type() == Type::object && overrides.type() == Type::object);
230 m_overrides = std::dynamic_pointer_cast<Object>(overrides.m_value);
231 }

◆ size()

std::size_t json::Node::size ( ) const

Query the size of an array

Definition at line 277 of file JsonParser.cpp.

278 {
279 switch (type()) {
280 case Type::array:
281 return dynamic_cast<Array&>(*m_value).value.size();
282 case Type::object:
283 return dynamic_cast<Object&>(*m_value).value.size();
284 default:
285 throw std::runtime_error("JSON parser: Expected an array or object");
286 }
287
288 }

◆ type()

Node::Type json::Node::type ( ) const

Query the value type of this node

Definition at line 233 of file JsonParser.cpp.

234 {
235 return m_value->type;
236 }

The documentation for this class was generated from the following files: