Devel Lib
Devel::IO::CJsonDocument Class Reference

A class for representing and handling a JSON document. More...

#include <JsonDocument.h>

Public Member Functions

 CJsonDocument ()
 Default constructor for CJsonDocument. More...
 
 CJsonDocument (const std::string &i_stJson)
 Constructor for CJsonDocument from a JSON string. More...
 
 CJsonDocument (const CJsonDocument &i_oOther)
 Copy constructor for CJsonDocument. More...
 
 CJsonDocument (CJsonDocument &&i_oOther) noexcept
 Move constructor for CJsonDocument. More...
 
virtual ~CJsonDocument ()
 Destructor for CJsonDocument.
 
void parse (const std::string &i_stJson)
 Parses a JSON string. More...
 
std::string serialize (bool i_bFormatted)
 Serializes the encapsulated JSON object into a JSON string. More...
 
void clear ()
 Clears the encapsulated JSON object. More...
 
CJsonObjectgetObject (const std::string &i_stKey)
 Gets a JSON object by key from the encapsulated JSON object. More...
 
CJsonObjecttoObject ()
 Gets the encapsulated JSON object. More...
 
CJsonObjectoperator[] (const std::string &i_stKey)
 Overload of assignment operator for CJsonDocument. More...
 
CJsonDocumentoperator= (const CJsonDocument &i_oOther)
 Overload of move assignment operator for CJsonDocument. More...
 
CJsonDocumentoperator= (CJsonDocument &&i_oOther) noexcept
 Moves the ownership of the CJsonObject from the given CJsonDocument to this CJsonDocument. More...
 

Detailed Description

A class for representing and handling a JSON document.

This class encapsulates a JSON object and provides functions for parsing and serializing JSON. Example

// Create a JSON document
// Parse a JSON string
doc.parse("{\"key\": \"value\"}");
// Get the object with the key "key"
// Serialize the document into a JSON string
std::string jsonString = doc.serialize(true);
A class for representing and handling a JSON document.
Definition: JsonDocument.h:35
CJsonObject & getObject(const std::string &i_stKey)
Gets a JSON object by key from the encapsulated JSON object.
Definition: JsonDocument.h:239
std::string serialize(bool i_bFormatted)
Serializes the encapsulated JSON object into a JSON string.
Definition: JsonDocument.cpp:406
void parse(const std::string &i_stJson)
Parses a JSON string.
Definition: JsonDocument.cpp:378
A class that encapsulates a JSON object.
Definition: JsonObject.h:50
Exceptions
std::runtime_errorIf the format or encoding of the JSON is invalid.

Constructor & Destructor Documentation

◆ CJsonDocument() [1/4]

Devel::IO::CJsonDocument::CJsonDocument ( )

Default constructor for CJsonDocument.

Initializes an empty CJsonDocument.

◆ CJsonDocument() [2/4]

Devel::IO::CJsonDocument::CJsonDocument ( const std::string &  i_stJson)

Constructor for CJsonDocument from a JSON string.

Parameters
i_stJson- The JSON string to initialize the CJsonDocument.

◆ CJsonDocument() [3/4]

Devel::IO::CJsonDocument::CJsonDocument ( const CJsonDocument i_oOther)
inline

Copy constructor for CJsonDocument.

Parameters
i_oOther- The CJsonDocument to copy.

Initializes a CJsonDocument by copying another CJsonDocument.

◆ CJsonDocument() [4/4]

Devel::IO::CJsonDocument::CJsonDocument ( CJsonDocument &&  i_oOther)
inlinenoexcept

Move constructor for CJsonDocument.

Parameters
i_oOther- The CJsonDocument to move.

Initializes a CJsonDocument by moving another CJsonDocument.

Member Function Documentation

◆ clear()

void Devel::IO::CJsonDocument::clear ( )
inline

Clears the encapsulated JSON object.

Clears the encapsulated JSON object.

◆ getObject()

CJsonObject& Devel::IO::CJsonDocument::getObject ( const std::string &  i_stKey)
inline

Gets a JSON object by key from the encapsulated JSON object.

Parameters
i_stKey- The key of the JSON object to get.
Returns
The JSON object with the given key.

Gets a JSON object by key from the encapsulated JSON object.

◆ operator=() [1/2]

CJsonDocument& Devel::IO::CJsonDocument::operator= ( CJsonDocument &&  i_oOther)
inlinenoexcept

Moves the ownership of the CJsonObject from the given CJsonDocument to this CJsonDocument.

Parameters
i_oOther- The CJsonDocument from which the ownership of the CJsonObject is to be moved.
Returns
Reference to this CJsonDocument.

This method will move the ownership of the CJsonObject from the given CJsonDocument to this CJsonDocument and return a reference to this CJsonDocument.

◆ operator=() [2/2]

CJsonDocument& Devel::IO::CJsonDocument::operator= ( const CJsonDocument i_oOther)
inline

Overload of move assignment operator for CJsonDocument.

Parameters
i_oOther- The CJsonDocument to move.
Returns
A reference to this CJsonDocument.

Moves the values of another CJsonDocument to this CJsonDocument.

◆ operator[]()

CJsonObject& Devel::IO::CJsonDocument::operator[] ( const std::string &  i_stKey)
inline

Overload of assignment operator for CJsonDocument.

Parameters
i_oOther- The CJsonDocument to copy.
Returns
A reference to this CJsonDocument.

Assigns the values of another CJsonDocument to this CJsonDocument.

◆ parse()

void Devel::IO::CJsonDocument::parse ( const std::string &  i_stJson)

Parses a JSON string.

Parameters
i_stJson- The JSON string to parse.

Parses a JSON string and populates the encapsulated JSON object.

◆ serialize()

std::string Devel::IO::CJsonDocument::serialize ( bool  i_bFormatted = false)

Serializes the encapsulated JSON object into a JSON string.

Parameters
i_bFormatted- Whether the output string should be pretty-printed.
Returns
The serialized JSON string.

Serializes the encapsulated JSON object into a JSON string.

◆ toObject()

CJsonObject& Devel::IO::CJsonDocument::toObject ( )
inline

Gets the encapsulated JSON object.

Returns
The encapsulated JSON object.

Gets the encapsulated JSON object.


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