djson v3.1.3
JSON parser
 
Loading...
Searching...
No Matches
dj::Json Class Reference

Library interface, represents a valid JSON value. More...

#include <djson/json.hpp>

Public Types

using Type = JsonType
 

Public Member Functions

 Json ()=default
 
 ~Json ()=default
 
 Json (Json &&)=default
 
auto operator= (Json &&) -> Json &=default
 
 Json (Json const &other)
 
auto operator= (Json const &other) -> Json &
 
 explicit (false) Json(std
 
auto get_type () const -> Type
 Obtain the value type of this Json.
 
auto is_null () const -> bool
 
auto is_boolean () const -> bool
 
auto is_number () const -> bool
 
auto is_string () const -> bool
 
auto is_array () const -> bool
 
auto is_object () const -> bool
 
auto as_bool (bool fallback={}) const -> bool
 
auto as_double (double fallback={}) const -> double
 
auto as_u64 (std::uint64_t fallback={}) const -> std::uint64_t
 
auto as_i64 (std::int64_t fallback={}) const -> std::int64_t
 
auto as_string_view (std::string_view fallback={}) const -> std::string_view
 
auto as_string (std::string_view const fallback={}) const -> std::string
 
template<NumericT Type>
auto as_number (Type const fallback={}) const -> Type
 
template<GettableT Type>
auto as (Type const &fallback={}) const -> Type
 
auto as_array () const -> std::span< dj::Json const >
 
auto as_object () const -> StringTable< dj::Json > const &
 
void set_null ()
 
void set_boolean (bool value)
 
void set_string (std::string_view value)
 
void set_number (std::int64_t value)
 
void set_number (std::uint64_t value)
 
void set_number (double value)
 
void set_value (Json value)
 
template<NumericT Type>
void set_number (Type const value)
 
template<SettableT Type>
void set (Type const &value)
 
void set_array ()
 Set value to empty Array.
 
void set_object ()
 Set value to empty Object.
 
auto push_back (Json value={}) -> Json &
 Insert value at the end of the Array. Converts to empty Array value first if not already one.
 
auto insert_or_assign (std::string key, Json value) -> Json &
 Insert value associated with key into the Object. Converts to empty Object value first if not already one.
 
auto operator[] (std::string_view key) const -> Json const &
 Obtain the value associated with the passed key.
 
auto operator[] (std::string_view key) -> Json &
 Obtain the value associated with the passed key.
 
auto operator[] (std::size_t index) const -> Json const &
 Obtain the value at the passed index.
 
auto operator[] (std::size_t index) -> Json &
 Obtain the value at the passed index.
 
auto serialize (SerializeOptions const &options={}) const -> std::string
 Serialize value as a string.
 
auto to_file (std::string_view path, SerializeOptions const &options={}) const -> bool
 Write serialized string to a file.
 
 operator bool () const
 

Static Public Member Functions

static auto parse (std::string_view text, ParseMode mode=ParseMode::Auto) -> Result
 Parse JSON text.
 
static auto from_file (std::string_view path, ParseMode mode=ParseMode::Auto) -> Result
 Parse JSON from a file.
 
static auto empty_array () -> Json const &
 Obtain a Json representing an empty Array value.
 
static auto empty_object () -> Json const &
 Obtain a Json representing an empty Object value.
 

Friends

class detail::Parser
 
void swap (Json &a, Json &b) noexcept
 

Detailed Description

Library interface, represents a valid JSON value.

Member Typedef Documentation

◆ Type

Constructor & Destructor Documentation

◆ Json() [1/3]

dj::Json::Json ( )
default

◆ ~Json()

dj::Json::~Json ( )
default

◆ Json() [2/3]

dj::Json::Json ( Json &&  )
default

◆ Json() [3/3]

dj::Json::Json ( Json const &  other)

Member Function Documentation

◆ as()

template<GettableT Type>
auto dj::Json::as ( Type const &  fallback = {}) const -> Type
inline

◆ as_array()

auto dj::Json::as_array ( ) const -> std::span< dj::Json const >

◆ as_bool()

auto dj::Json::as_bool ( bool  fallback = {}) const -> bool

◆ as_double()

auto dj::Json::as_double ( double  fallback = {}) const -> double

◆ as_i64()

auto dj::Json::as_i64 ( std::int64_t  fallback = {}) const -> std::int64_t

◆ as_number()

template<NumericT Type>
auto dj::Json::as_number ( Type const  fallback = {}) const -> Type
inline

◆ as_object()

auto dj::Json::as_object ( ) const -> StringTable< dj::Json > const &

◆ as_string()

auto dj::Json::as_string ( std::string_view const  fallback = {}) const -> std::string
inline

◆ as_string_view()

auto dj::Json::as_string_view ( std::string_view  fallback = {}) const -> std::string_view

◆ as_u64()

auto dj::Json::as_u64 ( std::uint64_t  fallback = {}) const -> std::uint64_t

◆ empty_array()

static auto dj::Json::empty_array ( ) -> Json const &
static

Obtain a Json representing an empty Array value.

◆ empty_object()

static auto dj::Json::empty_object ( ) -> Json const &
static

Obtain a Json representing an empty Object value.

◆ explicit()

dj::Json::explicit ( false  )
inline

◆ from_file()

static auto dj::Json::from_file ( std::string_view  path,
ParseMode  mode = ParseMode::Auto 
) -> Result
static

Parse JSON from a file.

Parameters
pathPath to JSON file.
modeParse mode.
Returns
Json if successful, else Error.

◆ get_type()

auto dj::Json::get_type ( ) const -> Type

Obtain the value type of this Json.

◆ insert_or_assign()

auto dj::Json::insert_or_assign ( std::string  key,
Json  value 
) -> Json &

Insert value associated with key into the Object. Converts to empty Object value first if not already one.

Parameters
keyKey to associate value with.
valueValue to insert.
Returns
Reference to inserted value.

◆ is_array()

auto dj::Json::is_array ( ) const -> bool
inline

◆ is_boolean()

auto dj::Json::is_boolean ( ) const -> bool
inline

◆ is_null()

auto dj::Json::is_null ( ) const -> bool
inline

◆ is_number()

auto dj::Json::is_number ( ) const -> bool
inline

◆ is_object()

auto dj::Json::is_object ( ) const -> bool
inline

◆ is_string()

auto dj::Json::is_string ( ) const -> bool
inline

◆ operator bool()

dj::Json::operator bool ( ) const
inlineexplicit

◆ operator=() [1/2]

auto dj::Json::operator= ( Json &&  ) -> Json &=default
default

◆ operator=() [2/2]

auto dj::Json::operator= ( Json const &  other) -> Json &

◆ operator[]() [1/4]

auto dj::Json::operator[] ( std::size_t  index) -> Json &

Obtain the value at the passed index.

Parameters
indexIndex to access value for.
Returns
Reference to value at index. Resizes if necessary.

◆ operator[]() [2/4]

auto dj::Json::operator[] ( std::size_t  index) const -> Json const &

Obtain the value at the passed index.

Parameters
indexIndex to access value for.
Returns
Value at index if type is Array and index is less than size, else null.

◆ operator[]() [3/4]

auto dj::Json::operator[] ( std::string_view  key) -> Json &

Obtain the value associated with the passed key.

Parameters
keyKey to lookup value for.
Returns
Reference to value if key exists, else newly inserted null value.

◆ operator[]() [4/4]

auto dj::Json::operator[] ( std::string_view  key) const -> Json const &

Obtain the value associated with the passed key.

Parameters
keyKey to lookup value for.
Returns
Value if type is Object and key exists, else null.

◆ parse()

static auto dj::Json::parse ( std::string_view  text,
ParseMode  mode = ParseMode::Auto 
) -> Result
static

Parse JSON text.

Parameters
textInput JSON text.
modeParse mode.
Returns
Json if successful, else Error.

◆ push_back()

auto dj::Json::push_back ( Json  value = {}) -> Json &

Insert value at the end of the Array. Converts to empty Array value first if not already one.

Parameters
valueValue to insert.
Returns
Reference to inserted value.

◆ serialize()

auto dj::Json::serialize ( SerializeOptions const &  options = {}) const -> std::string

Serialize value as a string.

Parameters
optionsSerialization options.
Returns
Serialized string.

◆ set()

template<SettableT Type>
void dj::Json::set ( Type const &  value)
inline

◆ set_array()

void dj::Json::set_array ( )

Set value to empty Array.

◆ set_boolean()

void dj::Json::set_boolean ( bool  value)

◆ set_null()

void dj::Json::set_null ( )

◆ set_number() [1/4]

void dj::Json::set_number ( double  value)

◆ set_number() [2/4]

void dj::Json::set_number ( std::int64_t  value)

◆ set_number() [3/4]

void dj::Json::set_number ( std::uint64_t  value)

◆ set_number() [4/4]

template<NumericT Type>
void dj::Json::set_number ( Type const  value)
inline

◆ set_object()

void dj::Json::set_object ( )

Set value to empty Object.

◆ set_string()

void dj::Json::set_string ( std::string_view  value)

◆ set_value()

void dj::Json::set_value ( Json  value)

◆ to_file()

auto dj::Json::to_file ( std::string_view  path,
SerializeOptions const &  options = {} 
) const -> bool

Write serialized string to a file.

Parameters
pathPath to write to.
optionsSerialization options.
Returns
true if file successfully written.

Friends And Related Symbol Documentation

◆ detail::Parser

friend class detail::Parser
friend

◆ swap

void swap ( Json a,
Json b 
)
friend

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