pulsatrix 1.1.0
Loading...
Searching...
No Matches
pulsatrix::JsonValue Class Reference

One JSON value: null, a boolean, a number, a string, an array or an object. More...

#include <json.hpp>

Public Types

enum class  Type {
  Null , Bool , Number , String ,
  Array , Object
}
 
using Array = std::vector< JsonValue >
 
using Object = std::vector< std::pair< std::string, JsonValue > >
 

Public Member Functions

 JsonValue ()=default
 A null.
 
 JsonValue (std::nullptr_t)
 A null.
 
 JsonValue (bool b)
 
 JsonValue (double d)
 A number written with the shortest text that reads back as the same double.
 
 JsonValue (int64_t i)
 
 JsonValue (int i)
 
 JsonValue (std::string s)
 
 JsonValue (const char *s)
 
 JsonValue (Array a)
 
 JsonValue (Object o)
 
Type type () const
 
bool is_null () const
 
const JsonValue * find (std::string_view key) const
 The member named key of an object, or nullptr if there is none.
 
void add (std::string key, JsonValue value)
 Appends a member to an object. Keys must be unique.
 
void push_back (JsonValue value)
 Appends an element to an array.
 
Typed access. Each throws std::invalid_argument if the value is another type.
bool as_bool () const
 
double as_double () const
 
float as_float () const
 
int64_t as_int64 () const
 
const std::string & as_string () const
 
const Array & as_array () const
 
const Object & as_object () const
 
const std::string & number_text () const
 A number's text exactly as written or parsed, for example "0.1".
 

Static Public Member Functions

static JsonValue Float (float f)
 A number written with the shortest text that reads back as the same float.
 

Friends

class JsonParser
 
JsonValue ParseJson (std::string_view text)
 Parses one JSON document.
 

Detailed Description

One JSON value: null, a boolean, a number, a string, an array or an object.

Note
Objects keep their keys in insertion order, and the writer emits them in that order, so the same value always serializes to the same bytes. A number keeps the exact text it was parsed from, so as_float() and as_double() each round the decimal text once, directly to their own type, instead of going through double first.

Member Typedef Documentation

◆ Array

using pulsatrix::JsonValue::Array = std::vector<JsonValue>

◆ Object

using pulsatrix::JsonValue::Object = std::vector<std::pair<std::string, JsonValue> >

Member Enumeration Documentation

◆ Type

enum class pulsatrix::JsonValue::Type
strong
Enumerator
Null 
Bool 
Number 
String 
Array 
Object 

Constructor & Destructor Documentation

◆ JsonValue() [1/10]

pulsatrix::JsonValue::JsonValue ( )
default

A null.

◆ JsonValue() [2/10]

pulsatrix::JsonValue::JsonValue ( std::nullptr_t  )
inline

A null.

◆ JsonValue() [3/10]

pulsatrix::JsonValue::JsonValue ( bool  b)

◆ JsonValue() [4/10]

pulsatrix::JsonValue::JsonValue ( double  d)

A number written with the shortest text that reads back as the same double.

Exceptions
std::invalid_argumentif d is NaN or infinite, which JSON can't represent.

◆ JsonValue() [5/10]

pulsatrix::JsonValue::JsonValue ( int64_t  i)

◆ JsonValue() [6/10]

pulsatrix::JsonValue::JsonValue ( int  i)
inline

◆ JsonValue() [7/10]

pulsatrix::JsonValue::JsonValue ( std::string  s)

◆ JsonValue() [8/10]

pulsatrix::JsonValue::JsonValue ( const char *  s)
inline

◆ JsonValue() [9/10]

pulsatrix::JsonValue::JsonValue ( Array  a)

◆ JsonValue() [10/10]

pulsatrix::JsonValue::JsonValue ( Object  o)

Member Function Documentation

◆ add()

void pulsatrix::JsonValue::add ( std::string  key,
JsonValue  value 
)

Appends a member to an object. Keys must be unique.

Exceptions
std::invalid_argumentif this value isn't an object or already has key.

◆ as_array()

const Array & pulsatrix::JsonValue::as_array ( ) const

◆ as_bool()

bool pulsatrix::JsonValue::as_bool ( ) const

◆ as_double()

double pulsatrix::JsonValue::as_double ( ) const

◆ as_float()

float pulsatrix::JsonValue::as_float ( ) const

◆ as_int64()

int64_t pulsatrix::JsonValue::as_int64 ( ) const
Exceptions
std::invalid_argumentalso if the number isn't an integer in int64_t's range.

◆ as_object()

const Object & pulsatrix::JsonValue::as_object ( ) const

◆ as_string()

const std::string & pulsatrix::JsonValue::as_string ( ) const

◆ find()

const JsonValue * pulsatrix::JsonValue::find ( std::string_view  key) const

The member named key of an object, or nullptr if there is none.

Exceptions
std::invalid_argumentif this value isn't an object.

◆ Float()

static JsonValue pulsatrix::JsonValue::Float ( float  f)
static

A number written with the shortest text that reads back as the same float.

Exceptions
std::invalid_argumentif f is NaN or infinite.

◆ is_null()

bool pulsatrix::JsonValue::is_null ( ) const
inline

◆ number_text()

const std::string & pulsatrix::JsonValue::number_text ( ) const

A number's text exactly as written or parsed, for example "0.1".

◆ push_back()

void pulsatrix::JsonValue::push_back ( JsonValue  value)

Appends an element to an array.

Exceptions
std::invalid_argumentif this value isn't an array.

◆ type()

Type pulsatrix::JsonValue::type ( ) const
inline

Friends And Related Symbol Documentation

◆ JsonParser

friend class JsonParser
friend

◆ ParseJson

JsonValue ParseJson ( std::string_view  text)
friend

Parses one JSON document.

Note
Strict: no comments, trailing commas, NaN or Infinity literals, duplicate object keys, invalid UTF-8, unpaired surrogate escapes or trailing content. Nesting deeper than 256 levels is rejected, so a hostile file can't exhaust the stack.
Exceptions
std::invalid_argumentnaming the byte offset of the first error.

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