MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
marley::JSON Class Reference

Classes

class  JSONConstWrapper
 
class  JSONWrapper
 

Public Types

enum class  DataType {
  Null , Object , Array , String ,
  Floating , Integral , Boolean
}
 

Public Member Functions

 JSON (const JSON &other)
 
 JSON (const std::string &s)
 
 JSON (JSON &&other)
 
 JSON (std::initializer_list< JSON > list)
 
 JSON (std::nullptr_t)
 
template<typename T>
 JSON (T b, typename std::enable_if< std::is_same< T, bool >::value >::type *=0)
 
template<typename T>
 JSON (T f, typename std::enable_if< std::is_floating_point< T >::value >::type *=0)
 
template<typename T>
 JSON (T i, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value >::type *=0)
 
template<typename T>
void append (T arg)
 
template<typename T, typename... U>
void append (T arg, U... args)
 
JSONWrapper< std::deque< JSON > > array_range ()
 
JSONConstWrapper< std::deque< JSON > > array_range () const
 
JSONat (const std::string &key)
 
const JSONat (const std::string &key) const
 
JSONat (unsigned index)
 
const JSONat (unsigned index) const
 
void check_if_object (const std::string &key) const
 
std::string dump_string (const int indent_step=-1) const
 
bool get_bool (const std::string &key) const
 
bool get_bool (const std::string &key, bool default_value) const
 
double get_double (const std::string &key) const
 
double get_double (const std::string &key, double default_value) const
 
long get_long (const std::string &key) const
 
long get_long (const std::string &key, long default_value) const
 
marley::JSON get_object (const std::string &key, bool throw_error=true) const
 
std::string get_string (const std::string &key) const
 
std::string get_string (const std::string &key, const std::string &default_value) const
 
bool has_key (const std::string &key) const
 
bool is_array () const
 
bool is_bool () const
 
bool is_float () const
 
bool is_integer () const
 
bool is_null () const
 Functions for getting primitives from the JSON object.
 
bool is_object () const
 
bool is_string () const
 
int length () const
 
JSONWrapper< std::map< std::string, JSON > > object_range ()
 
JSONConstWrapper< std::map< std::string, JSON > > object_range () const
 
JSONoperator= (const JSON &other)
 
JSONoperator= (JSON &&other)
 
template<typename T>
std::enable_if< std::is_same< T, bool >::value, JSON & >::type operator= (T b)
 
template<typename T>
std::enable_if< std::is_floating_point< T >::value, JSON & >::type operator= (T f)
 
template<typename T>
std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, JSON & >::type operator= (T i)
 
template<typename T>
std::enable_if< std::is_convertible< T, std::string >::value, JSON & >::type operator= (T s)
 
JSONoperator[] (const std::string &key)
 
JSONoperator[] (unsigned index)
 
void print (std::ostream &out, const unsigned int indent_step, bool pretty, const unsigned int current_indent=0) const
 
int size () const
 
bool to_bool () const
 
bool to_bool (bool &ok) const
 
bool to_bool_or_throw () const
 
double to_double () const
 
double to_double (bool &ok) const
 
double to_double_or_throw () const
 
long to_long () const
 
long to_long (bool &ok) const
 
long to_long_or_throw () const
 
std::string to_string () const
 
std::string to_string (bool &ok) const
 
std::string to_string_or_throw () const
 
DataType type () const
 

Static Public Member Functions

static JSON array ()
 
template<typename... T>
static JSON array (T... args)
 
static JSON load (const std::string &s)
 
static JSON load (std::istream &is)
 
static JSON load (StreamReader &reader)
 
static JSON load_file (const std::string &s)
 
static JSON make (DataType type)
 
static JSON object ()
 

Detailed Description

Definition at line 66 of file JSON.hh.

Member Enumeration Documentation

◆ DataType

enum class marley::JSON::DataType
strong

Definition at line 84 of file JSON.hh.

84 {
85 Null,
86 Object,
87 Array,
88 String,
89 Floating,
90 Integral,
91 Boolean
92 };

Constructor & Destructor Documentation

◆ JSON() [1/9]

marley::JSON::JSON ( )
inline

Definition at line 135 of file JSON.hh.

135: data_(), type_(DataType::Null) {}

◆ JSON() [2/9]

marley::JSON::JSON ( std::initializer_list< JSON > list)
inline

Definition at line 137 of file JSON.hh.

137 : JSON()
138 {
139 set_type(DataType::Object);
140 for(auto i = list.begin(), e = list.end(); i != e; ++i, ++i)
141 operator[](i->to_string()) = *std::next(i);
142 }

◆ JSON() [3/9]

marley::JSON::JSON ( JSON && other)
inline

Definition at line 144 of file JSON.hh.

144 : data_(other.data_), type_(other.type_)
145 { other.type_ = DataType::Null; other.data_.map_ = nullptr; }

◆ JSON() [4/9]

marley::JSON::JSON ( const JSON & other)
inline

Definition at line 155 of file JSON.hh.

155 {
156 switch(other.type_) {
157 case DataType::Object:
158 data_.map_ = new std::map<std::string,JSON>(
159 other.data_.map_->begin(), other.data_.map_->end());
160 break;
161 case DataType::Array:
162 data_.list_ = new std::deque<JSON>(other.data_.list_->begin(),
163 other.data_.list_->end());
164 break;
165 case DataType::String:
166 data_.string_ = new std::string(*other.data_.string_);
167 break;
168 default:
169 data_ = other.data_;
170 }
171 type_ = other.type_;
172 }

◆ ~JSON()

marley::JSON::~JSON ( )
inline

Definition at line 194 of file JSON.hh.

194 {
195 switch(type_) {
196 case DataType::Array:
197 delete data_.list_;
198 break;
199 case DataType::Object:
200 delete data_.map_;
201 break;
202 case DataType::String:
203 delete data_.string_;
204 break;
205 default:;
206 }
207 }

◆ JSON() [5/9]

template<typename T>
marley::JSON::JSON ( T b,
typename std::enable_if< std::is_same< T, bool >::value >::type * = 0 )
inline

Definition at line 209 of file JSON.hh.

211 : data_(b), type_(DataType::Boolean) {}

◆ JSON() [6/9]

template<typename T>
marley::JSON::JSON ( T i,
typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value >::type * = 0 )
inline

Definition at line 213 of file JSON.hh.

216 : data_(static_cast<long>(i)), type_(DataType::Integral) {}

◆ JSON() [7/9]

template<typename T>
marley::JSON::JSON ( T f,
typename std::enable_if< std::is_floating_point< T >::value >::type * = 0 )
inline

Definition at line 218 of file JSON.hh.

220 : data_(static_cast<double>(f)), type_(DataType::Floating) {}

◆ JSON() [8/9]

marley::JSON::JSON ( const std::string & s)
inlineexplicit

Definition at line 222 of file JSON.hh.

223 : data_(s), type_(DataType::String) {}

◆ JSON() [9/9]

marley::JSON::JSON ( std::nullptr_t )
inline

Definition at line 230 of file JSON.hh.

230: data_(), type_(DataType::Null) {}

Member Function Documentation

◆ append() [1/2]

template<typename T>
void marley::JSON::append ( T arg)
inline

Definition at line 259 of file JSON.hh.

259 {
260 set_type(DataType::Array);
261 data_.list_->emplace_back(arg);
262 }

◆ append() [2/2]

template<typename T, typename... U>
void marley::JSON::append ( T arg,
U... args )
inline

Definition at line 264 of file JSON.hh.

264 {
265 append(arg); append(args...);
266 }

◆ array() [1/2]

static JSON marley::JSON::array ( )
inlinestatic

Definition at line 238 of file JSON.hh.

238 {
239 return JSON::make(JSON::DataType::Array);
240 }

◆ array() [2/2]

template<typename... T>
static JSON marley::JSON::array ( T... args)
inlinestatic

Definition at line 243 of file JSON.hh.

244 {
245 JSON arr = JSON::make(JSON::DataType::Array);
246 arr.append(args...);
247 return arr;
248 }

◆ array_range() [1/2]

JSONWrapper< std::deque< JSON > > marley::JSON::array_range ( )
inline

Definition at line 440 of file JSON.hh.

440 {
441 if (type_ == DataType::Array)
442 return JSONWrapper<std::deque<JSON>>(data_.list_);
443 else return JSONWrapper<std::deque<JSON>>(nullptr);
444 }

◆ array_range() [2/2]

JSONConstWrapper< std::deque< JSON > > marley::JSON::array_range ( ) const
inline

Definition at line 453 of file JSON.hh.

453 {
454 if ( type_ == DataType::Array )
455 return JSONConstWrapper<std::deque<JSON>>(data_.list_);
456 else return JSONConstWrapper<std::deque<JSON>>(nullptr);
457 }

◆ at() [1/4]

JSON & marley::JSON::at ( const std::string & key)
inline

Definition at line 313 of file JSON.hh.

313 {
314 return operator[](key);
315 }

◆ at() [2/4]

const JSON & marley::JSON::at ( const std::string & key) const
inline

Definition at line 317 of file JSON.hh.

317 {
318 return data_.map_->at(key);
319 }

◆ at() [3/4]

JSON & marley::JSON::at ( unsigned index)
inline

Definition at line 321 of file JSON.hh.

321 {
322 return operator[](index);
323 }

◆ at() [4/4]

const JSON & marley::JSON::at ( unsigned index) const
inline

Definition at line 325 of file JSON.hh.

325 {
326 return data_.list_->at(index);
327 }

◆ check_if_object()

void marley::JSON::check_if_object ( const std::string & key) const
inline

Definition at line 574 of file JSON.hh.

574 {
575 if ( type_ != DataType::Object ) throw marley::Error( "Attempted"
576 " to retrieve a value for the key '" + key + "' from a JSON"
577 " primitive that is not an object" );
578 }

◆ dump_string()

std::string marley::JSON::dump_string ( const int indent_step = -1) const
inline

Definition at line 462 of file JSON.hh.

462 {
463 std::stringstream out;
464 // Enable pretty-printing if the user specified a nonnegative
465 // indent_step value
466 if (indent_step >= 0)
467 print(out, static_cast<unsigned int>(indent_step), true);
468 // Otherwise, print the JSON object in the most compact form possible
469 else print(out, 0, false);
470
471 // Return the completed JSON string
472 return out.str();
473 }

◆ get_bool() [1/2]

bool marley::JSON::get_bool ( const std::string & key) const
inline

Definition at line 665 of file JSON.hh.

665 {
666 check_if_object( key );
667 if ( has_key(key) ) return this->at( key ).to_bool_or_throw();
668 else throw marley::Error( "Missing JSON key '" + key + '\'' );
669 return 0.;
670 }

◆ get_bool() [2/2]

bool marley::JSON::get_bool ( const std::string & key,
bool default_value ) const
inline

Definition at line 675 of file JSON.hh.

675 {
676 check_if_object( key );
677 if ( !has_key(key) ) return default_value;
678 else return this->at( key ).to_bool_or_throw();
679 }

◆ get_double() [1/2]

double marley::JSON::get_double ( const std::string & key) const
inline

Definition at line 629 of file JSON.hh.

629 {
630 check_if_object( key );
631 if ( has_key(key) ) return this->at( key ).to_double_or_throw();
632 else throw marley::Error( "Missing JSON key '" + key + '\'' );
633 return 0.;
634 }

◆ get_double() [2/2]

double marley::JSON::get_double ( const std::string & key,
double default_value ) const
inline

Definition at line 639 of file JSON.hh.

639 {
640 check_if_object( key );
641 if ( !has_key(key) ) return default_value;
642 else return this->at( key ).to_double_or_throw();
643 }

◆ get_long() [1/2]

long marley::JSON::get_long ( const std::string & key) const
inline

Definition at line 647 of file JSON.hh.

647 {
648 check_if_object( key );
649 if ( has_key(key) ) return this->at( key ).to_long_or_throw();
650 else throw marley::Error( "Missing JSON key '" + key + '\'' );
651 return 0.;
652 }

◆ get_long() [2/2]

long marley::JSON::get_long ( const std::string & key,
long default_value ) const
inline

Definition at line 657 of file JSON.hh.

657 {
658 check_if_object( key );
659 if ( !has_key(key) ) return default_value;
660 else return this->at( key ).to_long_or_throw();
661 }

◆ get_object()

marley::JSON marley::JSON::get_object ( const std::string & key,
bool throw_error = true ) const
inline

Definition at line 703 of file JSON.hh.

705 {
706 check_if_object( key );
707 if ( has_key(key) ) return this->at( key );
708 else if ( throw_error ) throw marley::Error(
709 "Missing JSON key '" + key + '\'' );
710 return JSON::make( JSON::DataType::Object );
711 }

◆ get_string() [1/2]

std::string marley::JSON::get_string ( const std::string & key) const
inline

Definition at line 683 of file JSON.hh.

683 {
684 check_if_object( key );
685 if ( has_key(key) ) return this->at( key ).to_string_or_throw();
686 else throw marley::Error( "Missing JSON key '" + key + '\'' );
687 return std::string( "" );
688 }

◆ get_string() [2/2]

std::string marley::JSON::get_string ( const std::string & key,
const std::string & default_value ) const
inline

Definition at line 693 of file JSON.hh.

695 {
696 check_if_object( key );
697 if ( !has_key(key) ) return default_value;
698 else return this->at( key ).to_string_or_throw();
699 }

◆ has_key()

bool marley::JSON::has_key ( const std::string & key) const
inline

Definition at line 334 of file JSON.hh.

334 {
335 if (type_ == DataType::Object)
336 return data_.map_->find( key ) != data_.map_->end();
337 else return false;
338 }

◆ is_array()

bool marley::JSON::is_array ( ) const
inline

Definition at line 354 of file JSON.hh.

354{ return type_ == DataType::Array; }

◆ is_bool()

bool marley::JSON::is_bool ( ) const
inline

Definition at line 358 of file JSON.hh.

358{ return type_ == DataType::Boolean; }

◆ is_float()

bool marley::JSON::is_float ( ) const
inline

Definition at line 356 of file JSON.hh.

356{ return type_ == DataType::Floating; }

◆ is_integer()

bool marley::JSON::is_integer ( ) const
inline

Definition at line 357 of file JSON.hh.

357{ return type_ == DataType::Integral; }

◆ is_null()

bool marley::JSON::is_null ( ) const
inline

Functions for getting primitives from the JSON object.

Definition at line 352 of file JSON.hh.

352{ return type_ == DataType::Null; }

◆ is_object()

bool marley::JSON::is_object ( ) const
inline

Definition at line 353 of file JSON.hh.

353{ return type_ == DataType::Object; }

◆ is_string()

bool marley::JSON::is_string ( ) const
inline

Definition at line 355 of file JSON.hh.

355{ return type_ == DataType::String; }

◆ length()

int marley::JSON::length ( ) const
inline

Definition at line 329 of file JSON.hh.

329 {
330 if (type_ == DataType::Array) return data_.list_->size();
331 else return -1;
332 }

◆ load() [1/3]

JSON marley::JSON::load ( const std::string & s)
inlinestatic

Definition at line 1181 of file JSON.hh.

1181 {
1182 std::stringstream iss(str);
1183 return load(iss);
1184 }

◆ load() [2/3]

JSON marley::JSON::load ( std::istream & is)
inlinestatic

Definition at line 1163 of file JSON.hh.

1163 {
1164 StreamReader reader(in);
1165 return load(reader);
1166 }

◆ load() [3/3]

JSON marley::JSON::load ( StreamReader & reader)
inlinestatic

Definition at line 1168 of file JSON.hh.

1168 {
1169 char first = get_next_char( reader );
1170 if (first != '{') {
1171 throw marley::Error("Missing '{' at beginning of JSON object");
1172 reader.unget(first);
1173 return parse_object(reader);
1174 }
1175 else {
1176 reader.unget(first);
1177 return parse_next(reader);
1178 }
1179 }

◆ load_file()

JSON marley::JSON::load_file ( const std::string & s)
inlinestatic

Definition at line 1151 of file JSON.hh.

1151 {
1152 auto stream = std::make_unique<std::ifstream>(filename);
1153 if (stream->good()) {
1154 StreamReader reader(std::move(stream), filename);
1155 return load(reader);
1156 }
1157 else {
1158 throw marley::Error("Could not open the file \"" + filename + "\"");
1159 return JSON::make(JSON::DataType::Null);
1160 }
1161 }

◆ make()

static JSON marley::JSON::make ( DataType type)
inlinestatic

Definition at line 232 of file JSON.hh.

232 {
233 JSON ret;
234 ret.set_type(type);
235 return ret;
236 }

◆ object()

static JSON marley::JSON::object ( )
inlinestatic

Definition at line 250 of file JSON.hh.

250 {
251 return JSON::make(JSON::DataType::Object);
252 }

◆ object_range() [1/2]

JSONWrapper< std::map< std::string, JSON > > marley::JSON::object_range ( )
inline

Definition at line 434 of file JSON.hh.

434 {
435 if (type_ == DataType::Object)
436 return JSONWrapper<std::map<std::string,JSON>>(data_.map_);
437 else return JSONWrapper<std::map<std::string,JSON>>(nullptr);
438 }

◆ object_range() [2/2]

JSONConstWrapper< std::map< std::string, JSON > > marley::JSON::object_range ( ) const
inline

Definition at line 446 of file JSON.hh.

446 {
447 if (type_ == DataType::Object)
450 }

◆ operator=() [1/6]

JSON & marley::JSON::operator= ( const JSON & other)
inline

Definition at line 174 of file JSON.hh.

174 {
175 switch(other.type_) {
176 case DataType::Object:
177 data_.map_ = new std::map<std::string,JSON>(
178 other.data_.map_->begin(), other.data_.map_->end());
179 break;
180 case DataType::Array:
181 data_.list_ = new std::deque<JSON>( other.data_.list_->begin(),
182 other.data_.list_->end());
183 break;
184 case DataType::String:
185 data_.string_ = new std::string(*other.data_.string_);
186 break;
187 default:
188 data_ = other.data_;
189 }
190 type_ = other.type_;
191 return *this;
192 }

◆ operator=() [2/6]

JSON & marley::JSON::operator= ( JSON && other)
inline

Definition at line 147 of file JSON.hh.

147 {
148 data_ = other.data_;
149 type_ = other.type_;
150 other.data_.map_ = nullptr;
151 other.type_ = DataType::Null;
152 return *this;
153 }

◆ operator=() [3/6]

template<typename T>
std::enable_if< std::is_same< T, bool >::value, JSON & >::type marley::JSON::operator= ( T b)
inline

Definition at line 270 of file JSON.hh.

271 {
272 set_type(DataType::Boolean);
273 data_.boolean_ = b;
274 return *this;
275 }

◆ operator=() [4/6]

template<typename T>
std::enable_if< std::is_floating_point< T >::value, JSON & >::type marley::JSON::operator= ( T f)
inline

Definition at line 287 of file JSON.hh.

288 {
289 set_type(DataType::Floating);
290 data_.float_ = f;
291 return *this;
292 }

◆ operator=() [5/6]

template<typename T>
std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, JSON & >::type marley::JSON::operator= ( T i)
inline

Definition at line 278 of file JSON.hh.

279 {
280 set_type( DataType::Integral );
281 data_.integer_ = i;
282 return *this;
283 }

◆ operator=() [6/6]

template<typename T>
std::enable_if< std::is_convertible< T, std::string >::value, JSON & >::type marley::JSON::operator= ( T s)
inline

Definition at line 295 of file JSON.hh.

296 {
297 set_type(DataType::String);
298 *data_.string_ = std::string(s);
299 return *this;
300 }

◆ operator[]() [1/2]

JSON & marley::JSON::operator[] ( const std::string & key)
inline

Definition at line 302 of file JSON.hh.

302 {
303 set_type(DataType::Object);
304 return data_.map_->operator[](key);
305 }

◆ operator[]() [2/2]

JSON & marley::JSON::operator[] ( unsigned index)
inline

Definition at line 307 of file JSON.hh.

307 {
308 set_type(DataType::Array);
309 if (index >= data_.list_->size()) data_.list_->resize(index + 1);
310 return data_.list_->operator[](index);
311 }

◆ print()

void marley::JSON::print ( std::ostream & out,
const unsigned int indent_step,
bool pretty,
const unsigned int current_indent = 0 ) const
inline

Definition at line 477 of file JSON.hh.

479 {
480 // Use max_digits10 for outputting double-precision floating-point
481 // numbers. This ensures that repeated input/output via JSON will
482 // not result in any loss of precision. For more information, please
483 // see http://tinyurl.com/p8wyhnn
484 static std::ostringstream out_float;
485 static bool set_precision = false;
486 if (!set_precision) {
487 out_float.precision(std::numeric_limits<double>::max_digits10);
488 set_precision = true;
489 }
490
491 unsigned int indent = current_indent;
492
493 switch( type_ ) {
494 case DataType::Null:
495 out << "null";
496 return;
497 case DataType::Object: {
498 out << '{';
499 if (pretty) {
500 indent += indent_step;
501 out << '\n';
502 }
503 bool skip = true;
504 for( auto &p : *data_.map_ ) {
505 if ( !skip ) {
506 out << ',';
507 if (pretty) out << '\n';
508 }
509
510 out << std::string(indent, ' ') << '\"'
511 << json_escape( p.first ) << '\"';
512
513 if (pretty) out << " : ";
514 else out << ':';
515
516 p.second.print( out, indent_step, pretty, indent );
517 skip = false;
518 }
519 if (pretty) {
520 indent -= indent_step;
521 out << '\n';
522 }
523 out << std::string(indent, ' ') + '}';
524 return;
525 }
526 case DataType::Array: {
527 out << '[';
528 if (pretty) {
529 indent += indent_step;
530 out << '\n';
531 }
532 bool skip = true;
533 for( auto &p : *data_.list_ ) {
534 if ( !skip ) {
535 out << ',';
536 if (pretty) out << '\n';
537 }
538 out << std::string(indent, ' ');
539 p.print( out, indent_step, pretty, indent );
540 skip = false;
541 }
542 if (pretty) {
543 indent -= indent_step;
544 out << '\n';
545 }
546 out << std::string(indent, ' ') << ']';
547 return;
548 }
549 case DataType::String:
550 out << '\"' + json_escape( *data_.string_ ) + '\"';
551 return;
552 case DataType::Floating:
553 // Clear any previous contents of the stringstream
554 out_float.str("");
555 out_float.clear();
556 // Fill it with the new floating-point number
557 out_float << data_.float_;
558 // Output the resulting string to the stream
559 out << out_float.str();
560 return;
561 case DataType::Integral:
562 out << data_.integer_;
563 return;
564 case DataType::Boolean:
565 out << (data_.boolean_ ? "true" : "false");
566 return;
567 default:
568 break;
569 }
570
571 return;
572 }

◆ size()

int marley::JSON::size ( ) const
inline

Definition at line 340 of file JSON.hh.

340 {
341 if (type_ == DataType::Object)
342 return data_.map_->size();
343 else if (type_ == DataType::Array)
344 return data_.list_->size();
345 else
346 return -1;
347 }

◆ to_bool() [1/2]

bool marley::JSON::to_bool ( ) const
inline

Definition at line 416 of file JSON.hh.

416 {
417 bool b;
418 return to_bool( b );
419 }

◆ to_bool() [2/2]

bool marley::JSON::to_bool ( bool & ok) const
inline

Definition at line 421 of file JSON.hh.

421 {
422 ok = (type_ == DataType::Boolean);
423 return ok ? data_.boolean_ : false;
424 }

◆ to_bool_or_throw()

bool marley::JSON::to_bool_or_throw ( ) const
inline

Definition at line 426 of file JSON.hh.

426 {
427 bool ok;
428 double result = to_bool(ok);
429 if (!ok) throw marley::Error("Failed to convert JSON value '"
430 + to_string() + "' to bool");
431 return result;
432 }

◆ to_double() [1/2]

double marley::JSON::to_double ( ) const
inline

Definition at line 377 of file JSON.hh.

377 {
378 bool b;
379 return to_double(b);
380 }

◆ to_double() [2/2]

double marley::JSON::to_double ( bool & ok) const
inline

Definition at line 382 of file JSON.hh.

382 {
383 ok = (type_ == DataType::Floating);
384 if (ok) return data_.float_;
385 ok = (type_ == DataType::Integral);
386 if (ok) return data_.integer_;
387 return 0.;
388 }

◆ to_double_or_throw()

double marley::JSON::to_double_or_throw ( ) const
inline

Definition at line 390 of file JSON.hh.

390 {
391 bool ok;
392 double result = to_double(ok);
393 if (!ok) throw marley::Error("Failed to convert JSON value '"
394 + to_string() + "' to double");
395 return result;
396 }

◆ to_long() [1/2]

long marley::JSON::to_long ( ) const
inline

Definition at line 398 of file JSON.hh.

398 {
399 bool b;
400 return to_long( b );
401 }

◆ to_long() [2/2]

long marley::JSON::to_long ( bool & ok) const
inline

Definition at line 403 of file JSON.hh.

403 {
404 ok = (type_ == DataType::Integral);
405 return ok ? data_.integer_ : 0;
406 }

◆ to_long_or_throw()

long marley::JSON::to_long_or_throw ( ) const
inline

Definition at line 408 of file JSON.hh.

408 {
409 bool ok;
410 double result = to_long(ok);
411 if (!ok) throw marley::Error("Failed to convert JSON value '"
412 + to_string() + "' to long");
413 return result;
414 }

◆ to_string() [1/2]

std::string marley::JSON::to_string ( ) const
inline

Definition at line 360 of file JSON.hh.

360 {
361 bool b;
362 return to_string(b);
363 }

◆ to_string() [2/2]

std::string marley::JSON::to_string ( bool & ok) const
inline

Definition at line 365 of file JSON.hh.

365 {
366 ok = (type_ == DataType::String);
367 return ok ? json_escape(*data_.string_) : std::string("");
368 }

◆ to_string_or_throw()

std::string marley::JSON::to_string_or_throw ( ) const
inline

Definition at line 370 of file JSON.hh.

370 {
371 bool ok;
372 std::string result = to_string(ok);
373 if (!ok) throw marley::Error("Failed to convert JSON value to string");
374 return result;
375 }

◆ type()

DataType marley::JSON::type ( ) const
inline

Definition at line 349 of file JSON.hh.

349{ return type_; }

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