brace
Incredibly simple JSON parser in C++
Loading...
Searching...
No Matches
brace::JsonValue Class Reference

Represents a flexible JSON value that can hold different types of data. More...

#include <brace.h>

Public Types

using Value
 Internal type definition for storing different JSON value types.
 

Public Member Functions

 JsonValue ()
 Default constructor. Creates a null JSON value.
 
 JsonValue (bool b)
 Constructs a JSON value from a boolean.
 
 JsonValue (double d)
 Constructs a JSON value from a numeric value.
 
 JsonValue (const std::string &s)
 Constructs a JSON value from a string.
 
 JsonValue (const JsonObject &obj)
 Constructs a JSON value from an object (map of key-value pairs).
 
 JsonValue (const JsonArray &arr)
 Constructs a JSON value from an array.
 
bool is_null () const
 Checks if the current value is null.
 
bool is_bool () const
 Checks if the current value is a boolean.
 
bool is_number () const
 Checks if the current value is a number.
 
bool is_string () const
 Checks if the current value is a string.
 
bool is_object () const
 Checks if the current value is an object.
 
bool is_array () const
 Checks if the current value is an array.
 
const Valuevalue () const
 Retrieves the internal variant value.
 
 operator std::string () const
 Converts the JsonValue to a string.
 
 operator int () const
 Converts the JsonValue to an integer.
 
 operator double () const
 Converts the JsonValue to a double.
 
 operator float () const
 Converts the JsonValue to a float.
 
 operator size_t () const
 Converts the JsonValue to an size_t.
 
 operator bool () const
 Converts the JsonValue to a bool.
 
const JsonArray & to_array () const
 Retrieves the array value of the JsonValue.
 
bool operator== (const std::string &other) const
 Compares the JsonValue with a string.
 
bool operator== (const char *other) const
 Compares the JsonValue with a C-style string.
 
bool operator== (double other) const
 Compares the JsonValue with a double.
 
bool operator== (int other) const
 Compares the JsonValue with an integer.
 
const JsonValueoperator[] (const std::string &key) const
 Accesses an object's value by string key.
 
const JsonValueoperator[] (const char *key) const
 Accesses an object's value by C-style string key.
 
const JsonValueoperator[] (size_t index) const
 Accesses an array's value by index.
 

Detailed Description

Represents a flexible JSON value that can hold different types of data.

Supported JSON value types:

  • Null (JsonNullValue)
  • Boolean
  • Number (double)
  • String
  • Object (unordered map of key-value pairs)
  • Array (vector of JsonValues)

Member Typedef Documentation

◆ Value

Initial value:
std::variant<
bool,
double,
std::string,
JsonObject,
JsonArray,
JsonNullValue>

Internal type definition for storing different JSON value types.

Constructor & Destructor Documentation

◆ JsonValue() [1/5]

brace::JsonValue::JsonValue ( bool b)
inline

Constructs a JSON value from a boolean.

Parameters
bThe boolean value to store

◆ JsonValue() [2/5]

brace::JsonValue::JsonValue ( double d)
inline

Constructs a JSON value from a numeric value.

Parameters
dThe numeric value to store (as a double)

◆ JsonValue() [3/5]

brace::JsonValue::JsonValue ( const std::string & s)
inline

Constructs a JSON value from a string.

Parameters
sThe string to store

◆ JsonValue() [4/5]

brace::JsonValue::JsonValue ( const JsonObject & obj)
inline

Constructs a JSON value from an object (map of key-value pairs).

Parameters
objThe JSON object to store

◆ JsonValue() [5/5]

brace::JsonValue::JsonValue ( const JsonArray & arr)
inline

Constructs a JSON value from an array.

Parameters
arrThe JSON array to store

Member Function Documentation

◆ is_array()

bool brace::JsonValue::is_array ( ) const
inline

Checks if the current value is an array.

Returns
true if the value is an array, false otherwise

◆ is_bool()

bool brace::JsonValue::is_bool ( ) const
inline

Checks if the current value is a boolean.

Returns
true if the value is a boolean, false otherwise

◆ is_null()

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

Checks if the current value is null.

Returns
true if the value is null, false otherwise

◆ is_number()

bool brace::JsonValue::is_number ( ) const
inline

Checks if the current value is a number.

Returns
true if the value is a numeric value, false otherwise

◆ is_object()

bool brace::JsonValue::is_object ( ) const
inline

Checks if the current value is an object.

Returns
true if the value is an object, false otherwise

◆ is_string()

bool brace::JsonValue::is_string ( ) const
inline

Checks if the current value is a string.

Returns
true if the value is a string, false otherwise

◆ operator bool()

brace::JsonValue::operator bool ( ) const
inline

Converts the JsonValue to a bool.

Precondition
The JsonValue must be a boolean type
Exceptions
Assertsin debug build the value must be a bool.
Returns
The stored value converted to an bool

◆ operator double()

brace::JsonValue::operator double ( ) const
inline

Converts the JsonValue to a double.

Precondition
The JsonValue must be a numeric type
Exceptions
Assertsin debug build the value must be a number.
Returns
The stored value converted to a double

◆ operator float()

brace::JsonValue::operator float ( ) const
inline

Converts the JsonValue to a float.

Precondition
The JsonValue must be a numeric type
Exceptions
Assertsin debug build the value must be a number.
Returns
The stored value converted to a float

◆ operator int()

brace::JsonValue::operator int ( ) const
inline

Converts the JsonValue to an integer.

Precondition
The JsonValue must be a numeric type
Exceptions
Assertsin debug build the value must be a number.
Returns
The stored value converted to an int

◆ operator size_t()

brace::JsonValue::operator size_t ( ) const
inline

Converts the JsonValue to an size_t.

Precondition
The JsonValue must be a numeric type
Exceptions
Assertsin debug build the value must be a number.
Returns
The stored value converted to an size_t

◆ operator std::string()

brace::JsonValue::operator std::string ( ) const
inline

Converts the JsonValue to a string.

Precondition
The JsonValue must be a string type
Exceptions
Assertsin debug build the value must be a string.
Returns
The stored string value

◆ operator==() [1/4]

bool brace::JsonValue::operator== ( const char * other) const
inline

Compares the JsonValue with a C-style string.

Parameters
otherC-string to compare against
Returns
true if the JsonValue is a string and matches the provided C-string, false otherwise

◆ operator==() [2/4]

bool brace::JsonValue::operator== ( const std::string & other) const
inline

Compares the JsonValue with a string.

Parameters
otherString to compare against
Returns
true if the JsonValue is a string and matches the provided string, false otherwise

◆ operator==() [3/4]

bool brace::JsonValue::operator== ( double other) const
inline

Compares the JsonValue with a double.

Parameters
otherDouble value to compare against
Returns
true if the JsonValue is a number and matches the provided double, false otherwise

◆ operator==() [4/4]

bool brace::JsonValue::operator== ( int other) const
inline

Compares the JsonValue with an integer.

Parameters
otherInteger value to compare against
Returns
true if the JsonValue is a number and matches the provided integer, false otherwise

◆ operator[]() [1/3]

const JsonValue & brace::JsonValue::operator[] ( const char * key) const
inline

Accesses an object's value by C-style string key.

Parameters
keyThe C-string key to look up in the JSON object
Precondition
The JsonValue must be an object type
Exceptions
Assertsin debug builds the value must be an object
Returns
Reference to the JsonValue associated with the key

◆ operator[]() [2/3]

const JsonValue & brace::JsonValue::operator[] ( const std::string & key) const
inline

Accesses an object's value by string key.

Parameters
keyThe key to look up in the JSON object
Precondition
The JsonValue must be an object type
Exceptions
Assertsin debug builds the value must be an object
Returns
Reference to the JsonValue associated with the key

◆ operator[]() [3/3]

const JsonValue & brace::JsonValue::operator[] ( size_t index) const
inline

Accesses an array's value by index.

Parameters
indexThe index to access in the JSON array
Precondition
The JsonValue must be an array type
Exceptions
Assertsin debug builds the value must be an array
Returns
Reference to the JsonValue at the specified index

◆ to_array()

const JsonArray & brace::JsonValue::to_array ( ) const
inline

Retrieves the array value of the JsonValue.

Precondition
The JsonValue must be an array type
Exceptions
Assertsin debug build the value must be an array.
Returns
Reference to the stored JsonArray

◆ value()

const Value & brace::JsonValue::value ( ) const
inline

Retrieves the internal variant value.

Returns
const Reference to the underlying std::variant value

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