GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
1#pragma once
2#include "json.hpp" // nlohmann::json
3
4namespace ga {
5using Json = nlohmann::json;
6
7namespace json {
8 // json parser utility functions
9
10 template <typename T>
11 inline T getValue( const ga::Json& json, const std::string& keyOrPointer, const T& defaultValue )
12 {
13 T ret = defaultValue;
14 try {
15 if ( keyOrPointer.empty() || keyOrPointer.at( 0 ) == '/' ) {
16 // allow for json pointer paths - these are either empty or begin with '/'
17 ret = json.at(ga::Json::json_pointer( keyOrPointer )).get<T>();
18 } else {
19 // assume key is a normal key for a json object
20 ret = json.at(keyOrPointer).get<T>();
21 }
22 } catch ( ... ) {
23 // todo: log warning? accept an ostream& for logging?
24 }
25 return ret;
26 }
27
28 inline std::string getValue( const ga::Json& json, const std::string& keyOrPointer, const char* defaultValue )
29 {
30 return getValue( json, keyOrPointer, std::string( defaultValue ) );
31 }
32
33} // namespace json
34} // namespace ga
T getValue(const ga::Json &json, const std::string &keyOrPointer, const T &defaultValue)
Definition: json.h:11
Definition: color.h:9
nlohmann::json Json
Definition: json.h:5