Cthulhu  0.2.10
Cthulhu compiler collection
ctu::json::Json Class Reference

a json value a json node from a parsed json document More...

#include <json.hpp>

Public Member Functions

constexpr Json (json_t *ast)
 
constexpr Json ()
 
CT_PUREFN bool is_string () const
 check if the value is a string More...
 
CT_PUREFN bool is_integer () const
 check if the value is an integer More...
 
CT_PUREFN bool is_float () const
 check if the value is a float More...
 
CT_PUREFN bool is_bool () const
 check if the value is a boolean More...
 
CT_PUREFN bool is_array () const
 check if the value is an array More...
 
CT_PUREFN bool is_object () const
 check if the value is an object More...
 
CT_PUREFN bool is_null () const
 check if the value is null More...
 
constexpr CT_PUREFN bool is_valid () const
 check if the value is valid this should always be checked before using any other methods More...
 
constexpr operator bool () const
 check if the value is valid More...
 
CT_PUREFN json_kind_t get_kind () const
 get the kind of the value More...
 
CT_PUREFN bool is_kind (json_kind_t kind) const
 check if the value is a specific kind More...
 
CT_PUREFN std::string_view as_string () const
 get the string value More...
 
void as_integer (mpz_t integer) const
 get the integer value More...
 
CT_PUREFN float as_float () const
 get the float value More...
 
CT_PUREFN bool as_bool () const
 get the boolean value More...
 
CT_PUREFN Array as_array () const
 get the array value More...
 
CT_PUREFN Object as_object () const
 get the object value More...
 
CT_PUREFN size_t length () const
 get the length of an array More...
 
Json get (const char *key) const
 get a value from an object by key More...
 
Json get (size_t index) const
 get a value from an array by index More...
 
constexpr json_tget_ast () const
 get the underlying ast node More...
 
Json operator[] (const char *key) const
 get a value from an object by key same as get(const char*) More...
 
Json operator[] (size_t index) const
 get a value from an array by index same as get(size_t) More...
 
template<typename F >
auto visit (F &&func) const
 visit the value calls the appropriate function for the value kind More...
 

Friends

class JsonParser
 
class Object
 
class Array
 
class ObjectIterator
 
class ArrayIterator
 

Detailed Description

a json value a json node from a parsed json document

Warning
this class may be in an invalid state in certain cases always check the validity of the value with is_valid before using it

Definition at line 118 of file json.hpp.

Constructor & Destructor Documentation

◆ Json() [1/2]

constexpr ctu::json::Json::Json ( json_t ast)
inlineconstexpr

Definition at line 128 of file json.hpp.

◆ Json() [2/2]

constexpr ctu::json::Json::Json ( )
inlineconstexpr

Definition at line 132 of file json.hpp.

Member Function Documentation

◆ as_array()

Array Json::as_array ( ) const

get the array value

Precondition
ctu::json::Json::is_array() is true mostly useful for iterating over the array
Returns
the array value

Definition at line 100 of file json.cpp.

◆ as_bool()

bool Json::as_bool ( ) const

get the boolean value

Precondition
ctu::json::Json::is_bool() is true
Returns
the boolean value

Definition at line 95 of file json.cpp.

◆ as_float()

float Json::as_float ( ) const

get the float value

Precondition
ctu::json::Json::is_float() is true
Returns
the float value

Definition at line 90 of file json.cpp.

◆ as_integer()

void Json::as_integer ( mpz_t  integer) const

get the integer value

Precondition
ctu::json::Json::is_integer() is true
Parameters
[out]integerthe integer output value

Definition at line 85 of file json.cpp.

◆ as_object()

Object Json::as_object ( ) const

get the object value

Precondition
ctu::json::Json::is_object() is true mostly useful for iterating over the object
Returns
the object value

Definition at line 105 of file json.cpp.

◆ as_string()

std::string_view Json::as_string ( ) const

get the string value

Precondition
ctu::json::Json::is_string() is true returns a text view as string values may have embedded nulls
Returns
the string value

Definition at line 79 of file json.cpp.

◆ get() [1/2]

Json Json::get ( const char *  key) const

get a value from an object by key

Precondition
ctu::json::Json::is_object() is true
Parameters
keythe key to access
Returns
the value at key

Definition at line 110 of file json.cpp.

◆ get() [2/2]

Json Json::get ( size_t  index) const

get a value from an array by index

Precondition
ctu::json::Json::is_array() and index < length
Parameters
indexthe index to access
Returns
the value at index

Definition at line 115 of file json.cpp.

◆ get_ast()

constexpr json_t* ctu::json::Json::get_ast ( ) const
inlineconstexpr

get the underlying ast node

Returns
the ast node

Definition at line 262 of file json.hpp.

◆ get_kind()

json_kind_t Json::get_kind ( ) const

get the kind of the value

Returns
the kind of the value

Definition at line 74 of file json.cpp.

◆ is_array()

bool Json::is_array ( ) const

check if the value is an array

Return values
trueif the value is an array
falseif the value is not an array

Definition at line 68 of file json.cpp.

◆ is_bool()

bool Json::is_bool ( ) const

check if the value is a boolean

Return values
trueif the value is a boolean
falseif the value is not a boolean

Definition at line 67 of file json.cpp.

◆ is_float()

bool Json::is_float ( ) const

check if the value is a float

Return values
trueif the value is a float
falseif the value is not a float

Definition at line 66 of file json.cpp.

◆ is_integer()

bool Json::is_integer ( ) const

check if the value is an integer

Return values
trueif the value is an integer
falseif the value is not an integer

Definition at line 65 of file json.cpp.

◆ is_kind()

bool Json::is_kind ( json_kind_t  kind) const

check if the value is a specific kind

Parameters
kindthe kind to check for
Return values
trueif the value is of kind kind
falseif the value is not of kind kind

Definition at line 72 of file json.cpp.

◆ is_null()

bool Json::is_null ( ) const

check if the value is null

Return values
trueif the value is null
falseif the value is not null

Definition at line 70 of file json.cpp.

◆ is_object()

bool Json::is_object ( ) const

check if the value is an object

Return values
trueif the value is an object
falseif the value is not an object

Definition at line 69 of file json.cpp.

◆ is_string()

bool Json::is_string ( ) const

check if the value is a string

Return values
trueif the value is a string
falseif the value is not a string

Definition at line 64 of file json.cpp.

◆ is_valid()

constexpr CT_PUREFN bool ctu::json::Json::is_valid ( ) const
inlineconstexpr

check if the value is valid this should always be checked before using any other methods

Return values
trueif the value is valid
falseif the value is not valid

Definition at line 176 of file json.hpp.

◆ length()

size_t Json::length ( ) const

get the length of an array

Precondition
ctu::json::Json::is_array() is true
Returns
the length of the array

Definition at line 120 of file json.cpp.

◆ operator bool()

constexpr ctu::json::Json::operator bool ( ) const
inlineconstexpr

check if the value is valid

Precondition
ctu::json::Json::is_valid() is true
Return values
trueif the value is valid
falseif the value is not valid

Definition at line 183 of file json.hpp.

◆ operator[]() [1/2]

Json Json::operator[] ( const char *  key) const

get a value from an object by key same as get(const char*)

Parameters
keythe key to access
Returns
the value at key

Definition at line 125 of file json.cpp.

◆ operator[]() [2/2]

Json Json::operator[] ( size_t  index) const

get a value from an array by index same as get(size_t)

Parameters
indexthe index to access
Returns
the value at index

Definition at line 129 of file json.cpp.

◆ visit()

template<typename F >
auto ctu::json::Json::visit ( F &&  func) const
inline

visit the value calls the appropriate function for the value kind

Template Parameters
Fthe function type
Parameters
functhe function to call
Returns
the result of the function

Definition at line 288 of file json.hpp.

Friends And Related Function Documentation

◆ Array

friend class Array
friend

Definition at line 121 of file json.hpp.

◆ ArrayIterator

friend class ArrayIterator
friend

Definition at line 123 of file json.hpp.

◆ JsonParser

friend class JsonParser
friend

Definition at line 119 of file json.hpp.

◆ Object

friend class Object
friend

Definition at line 120 of file json.hpp.

◆ ObjectIterator

friend class ObjectIterator
friend

Definition at line 122 of file json.hpp.


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