Conscience Core
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
conscience_utils::JSON::CscJsonObject Class Reference

#include <json.h>

Inheritance diagram for conscience_utils::JSON::CscJsonObject:
conscience_utils::JSON::CscJsonObjectReader

Public Member Functions

 CscJsonObject (const fs::path &jsonFilePath)
 
 CscJsonObject (const string &jsonString)
 
 CscJsonObject (const map< string, string > &map)
 
 CscJsonObject (yyjson_mut_doc *existingDoc, yyjson_mut_val *existingValue, bool doNotReleaseYyjsonObjects=false, CscJsonObject *lookupParent=nullptr)
 
 CscJsonObject ()
 
 ~CscJsonObject ()
 
CscJsonObject getRequiredObjectMutableValue (const string &key)
 
optional< CscJsonObjectgetObjectMutableValue (const string &key)
 
CscJsonObjectgetRequiredObjectMutablePtrValue (const string &key)
 
optional< CscJsonObject * > getObjectMutablePtrValue (const string &key)
 
ptr< CscJsonObjectgetRequiredObjectMutableSharedPtrValue (const string &key)
 
optional< ptr< CscJsonObject > > getObjectMutableSharedPtrValue (const string &key)
 
CscJsonObjectset (const string &propertyPath, bool value)
 
CscJsonObjectset (const string &propertyPath, const char *value)
 
CscJsonObjectset (const string &propertyPath, const string &value)
 
CscJsonObjectset (const string &propertyPath, double value)
 
CscJsonObjectset (const string &propertyPath, int value)
 
CscJsonObjectset (const string &propertyPath, long long value)
 
CscJsonObjectset (const string &propertyPath, unsigned long long value)
 
CscJsonObjectset (const string &propertyPath, CscJsonObject &object)
 
CscJsonObjectset (const string &propertyPath, CscJsonObject *object)
 
CscJsonObjectset (const string &propertyPath, ptr< CscJsonObject > object)
 
CscJsonObjectset (const string &propertyPath, const map< string, string > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< bool > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< string > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< float > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< double > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< int > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< long long > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< unsigned long long > &value)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< CscJsonObject * > &objects)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< CscJsonObject > &objects)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< ptr< CscJsonObject >> &objects)
 
CscJsonObjectsetArray (const string &propertyPath, const vector< map< string, string >> &value)
 
CscJsonObjectremove (const string &propertyPath)
 
void saveToFile (const fs::path &outPath, bool lineBreakAndTabulation=false)
 
yyjson_mut_val * getLowLevelMutableObject ()
 
void mergeWith (CscJsonObject &other, bool deep=true)
 
void mergeKeyWith (CscJsonObject &other, const string &subPropertyPath, bool deep=true)
 
- Public Member Functions inherited from conscience_utils::JSON::CscJsonObjectReader
vector< string > getKeys ()
 
bool hasKey (const string &key)
 
bool isArray (const string &key)
 
unsigned getArrayLength (const string &arrayKey)
 
bool isNull (const string &key)
 
string getRequiredStringValue (const string &key, bool allowEmpty=false)
 
optional< int > getIntValue (const string &key)
 
int getRequiredIntValue (const string &key)
 
optional< unsigned long long > getUnsignedLongValue (const string &key)
 
unsigned long long getRequiredUnsignedLongValue (const string &key)
 
optional< bool > getBooleanValue (const string &key)
 
bool getRequiredBooleanValue (const string &key)
 
optional< double > getDoubleValue (const string &key)
 
double getRequiredDoubleValue (const string &key)
 
optional< float > getFloatValue (const string &key)
 
float getRequiredFloatValue (const string &key)
 
optional< string > getStringValue (const string &key)
 
CscJsonObjectReader getRequiredObjectValue (const string &key)
 
optional< CscJsonObjectReadergetObjectValue (const string &key)
 
CscJsonObjectReadergetRequiredObjectPtrValue (const string &key)
 
optional< CscJsonObjectReader * > getObjectPtrValue (const string &key)
 
ptr< CscJsonObjectReadergetRequiredObjectSharedPtrValue (const string &key)
 
optional< ptr< CscJsonObjectReader > > getObjectSharedPtrValue (const string &key)
 
map< string, optional< any > > getRequiredObjectValueAsMap (const string &key)
 
map< string, string > getRequiredObjectValueAsMapOfStrings (const string &key)
 
optional< map< string, optional< any > > > getObjectValueAsMap (const string &key)
 
optional< any > getAnyValue (string key)
 
template<class T >
vector< T > getRequiredArrayValue (const string &key)
 
template<class T >
optional< vector< T > > getArrayValue (const string &key)
 
template<class T >
optional< T > getArrayItemValue (const string &arrayKey, size_t index)
 
template<class T >
getRequiredArrayItemValue (const string &arrayKey, size_t index)
 
template<class T >
vector< T > asArrayValue ()
 
 CscJsonObjectReader (ptr< yyjson_doc > yyjsonDoc, yyjson_val *yyjsonValue=nullptr, bool doNotReleaseYyjsonObjects=false)
 
 CscJsonObjectReader (const fs::path filePath)
 
 CscJsonObjectReader (const string &jsonContent)
 
virtual ~CscJsonObjectReader ()
 
string toString ()
 
string toJsonString (bool prettyPrint=false)
 
CscJsonObjecttoMutable (yyjson_mut_doc *parentDoc=nullptr)
 
yyjson_val * getLowLevelParser ()
 

Static Public Member Functions

static string addLineBreakAndTabulation (const string &json)
 Adds line breaks and tabulations to a JSON string for improved readability. More...
 
- Static Public Member Functions inherited from conscience_utils::JSON::CscJsonObjectReader
static vector< CscJsonObject * > toMutableList (const vector< ptr< CscJsonObjectReader >> values)
 

Protected Member Functions

virtual yyjson_val * getYyjsonValue () override
 
virtual ptr< yyjson_doc > getYyjsonDoc () override
 
yyjson_mut_doc * getYyjsonMutableDoc ()
 
yyjson_mut_val * getYyjsonMutableValue ()
 
yyjson_mut_val * getMutableValueByPropertyPath (const string &propertyPath)
 
yyjson_mut_val * getOrCreateObjectByPropertyPath (const string &propertyPath)
 
CscJsonObjectset (const string &propertyPath, yyjson_mut_val *newValue)
 
void mergeObjects (yyjson_mut_val *thisObj, yyjson_mut_val *otherObj, bool deep)
 
- Protected Member Functions inherited from conscience_utils::JSON::CscJsonObjectReader
yyjson_val * getValueByPropertyPath (const string &propertyPath)
 

Protected Attributes

yyjson_mut_doc * yyjsonMutableDoc = nullptr
 
yyjson_mut_val * yyjsonMutableValue = nullptr
 
- Protected Attributes inherited from conscience_utils::JSON::CscJsonObjectReader
ptr< yyjson_doc > yyjsonDoc = nullptr
 
yyjson_val * yyjsonValue = nullptr
 

Additional Inherited Members

- Static Protected Member Functions inherited from conscience_utils::JSON::CscJsonObjectReader
static ptr< yyjson_doc > createYyjsonSharedPtr (yyjson_doc *doc)
 
static yyjson_doc * parseFromFile (const fs::path filePath)
 
static yyjson_doc * parseFromString (const string &jsonString)
 

Detailed Description

This class provides full json features, read and write @notethis class is NOT thread safe

Constructor & Destructor Documentation

◆ CscJsonObject() [1/5]

conscience_utils::JSON::CscJsonObject::CscJsonObject ( const fs::path &  jsonFilePath)

◆ CscJsonObject() [2/5]

conscience_utils::JSON::CscJsonObject::CscJsonObject ( const string &  jsonString)

◆ CscJsonObject() [3/5]

conscience_utils::JSON::CscJsonObject::CscJsonObject ( const map< string, string > &  map)

Creates a modifiable json object from existing values in given map

◆ CscJsonObject() [4/5]

conscience_utils::JSON::CscJsonObject::CscJsonObject ( yyjson_mut_doc *  existingDoc,
yyjson_mut_val *  existingValue,
bool  doNotReleaseYyjsonObjects = false,
CscJsonObject lookupParent = nullptr 
)

Creates a modifiable json object from existing mutable doc and value

◆ CscJsonObject() [5/5]

conscience_utils::JSON::CscJsonObject::CscJsonObject ( )

Creates a new and empty modifiable json object

◆ ~CscJsonObject()

conscience_utils::JSON::CscJsonObject::~CscJsonObject ( )

Member Function Documentation

◆ addLineBreakAndTabulation()

string conscience_utils::JSON::CscJsonObject::addLineBreakAndTabulation ( const string &  json)
static

Adds line breaks and tabulations to a JSON string for improved readability.

This function takes a compact JSON string and reformats it by inserting line breaks and tabulations. Line breaks are added after each comma (,) and closing brace (}), and tabulations are added for indentation to visually represent the structure of nested JSON objects.

Parameters
jsonThe compact JSON string to be reformatted.
Returns
A new string that represents the input JSON with added line breaks and tabulations for readability.
Note
The function assumes the input JSON string is well-formed and does not perform any validation on it.

Example usage:

string compactJSON = R"({"name":"John","age":30,"cars":{"Ford","BMW","Fiat"}})";
string formattedJSON = obj.addLineBreakAndTabulation(compactJSON);
cout << formattedJSON << endl;

This would output:

{
    "name":"John",
    "age":30,
    "cars":{
        "Ford",
        "BMW",
        "Fiat"
    }
}

The function primarily aims to enhance the readability of the JSON data by adding visual structure through indentation and line breaks, making it easier to understand the hierarchy and organization of the data.

◆ getLowLevelMutableObject()

yyjson_mut_val * conscience_utils::JSON::CscJsonObject::getLowLevelMutableObject ( )

◆ getMutableValueByPropertyPath()

yyjson_mut_val * conscience_utils::JSON::CscJsonObject::getMutableValueByPropertyPath ( const string &  propertyPath)
protected

◆ getObjectMutablePtrValue()

optional< CscJsonObject * > conscience_utils::JSON::CscJsonObject::getObjectMutablePtrValue ( const string &  key)

gets optional object value, you need to delete it

◆ getObjectMutableSharedPtrValue()

optional< ptr< CscJsonObject > > conscience_utils::JSON::CscJsonObject::getObjectMutableSharedPtrValue ( const string &  key)

gets optional object value

◆ getObjectMutableValue()

optional< CscJsonObject > conscience_utils::JSON::CscJsonObject::getObjectMutableValue ( const string &  key)

gets optional object value

◆ getOrCreateObjectByPropertyPath()

yyjson_mut_val * conscience_utils::JSON::CscJsonObject::getOrCreateObjectByPropertyPath ( const string &  propertyPath)
protected

◆ getRequiredObjectMutablePtrValue()

CscJsonObject * conscience_utils::JSON::CscJsonObject::getRequiredObjectMutablePtrValue ( const string &  key)

gets object value, or throw error, you need to delete it

◆ getRequiredObjectMutableSharedPtrValue()

ptr< CscJsonObject > conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableSharedPtrValue ( const string &  key)

gets object value, or throw error

◆ getRequiredObjectMutableValue()

CscJsonObject conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableValue ( const string &  key)

gets object value, or throw error

◆ getYyjsonDoc()

ptr< yyjson_doc > conscience_utils::JSON::CscJsonObject::getYyjsonDoc ( )
overrideprotectedvirtual
See also
in cpp file - Design note : getYyjsonXXX()

Design note : getYyjsonXXX() - This part is a bit tricky Lazy init of read-only and mutable objects :

  • if read-only objects are null, create equivalent from mutable version
  • if mutable objects are null, instantiate new empty objects

Reimplemented from conscience_utils::JSON::CscJsonObjectReader.

◆ getYyjsonMutableDoc()

yyjson_mut_doc * conscience_utils::JSON::CscJsonObject::getYyjsonMutableDoc ( )
protected
See also
in cpp file - Design note : getYyjsonXXX()

◆ getYyjsonMutableValue()

yyjson_mut_val * conscience_utils::JSON::CscJsonObject::getYyjsonMutableValue ( )
protected
See also
in cpp file - Design note : getYyjsonXXX()

◆ getYyjsonValue()

yyjson_val * conscience_utils::JSON::CscJsonObject::getYyjsonValue ( )
overrideprotectedvirtual
See also
in cpp file - Design note : getYyjsonXXX()

Reimplemented from conscience_utils::JSON::CscJsonObjectReader.

◆ mergeKeyWith()

void conscience_utils::JSON::CscJsonObject::mergeKeyWith ( CscJsonObject other,
const string &  subPropertyPath,
bool  deep = true 
)

◆ mergeObjects()

void conscience_utils::JSON::CscJsonObject::mergeObjects ( yyjson_mut_val *  thisObj,
yyjson_mut_val *  otherObj,
bool  deep 
)
protected

◆ mergeWith()

void conscience_utils::JSON::CscJsonObject::mergeWith ( CscJsonObject other,
bool  deep = true 
)

this will take all values from other and put it in this, except if this already has a value for this key. Merge can be deep

Parameters
deeppass to false if you only want to process this object and not nested objects

◆ remove()

CscJsonObject * conscience_utils::JSON::CscJsonObject::remove ( const string &  propertyPath)

WARNING : current implementation support property path from root only

◆ saveToFile()

void conscience_utils::JSON::CscJsonObject::saveToFile ( const fs::path &  outPath,
bool  lineBreakAndTabulation = false 
)

◆ set() [1/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
bool  value 
)

◆ set() [2/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
const char *  value 
)

◆ set() [3/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
const map< string, string > &  value 
)

◆ set() [4/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
const string &  value 
)

◆ set() [5/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
CscJsonObject object 
)

◆ set() [6/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
CscJsonObject object 
)
Note
object will be DELETED

◆ set() [7/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
double  value 
)

◆ set() [8/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
int  value 
)

◆ set() [9/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
long long  value 
)

◆ set() [10/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
ptr< CscJsonObject object 
)

◆ set() [11/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
unsigned long long  value 
)

◆ set() [12/12]

CscJsonObject * conscience_utils::JSON::CscJsonObject::set ( const string &  propertyPath,
yyjson_mut_val *  newValue 
)
protected

◆ setArray() [1/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< bool > &  value 
)

◆ setArray() [2/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< CscJsonObject * > &  objects 
)
Note
objects will be DELETED

◆ setArray() [3/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< CscJsonObject > &  objects 
)

◆ setArray() [4/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< double > &  value 
)

◆ setArray() [5/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< float > &  value 
)

◆ setArray() [6/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< int > &  value 
)

◆ setArray() [7/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< long long > &  value 
)

◆ setArray() [8/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< map< string, string >> &  value 
)

◆ setArray() [9/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< ptr< CscJsonObject >> &  objects 
)

◆ setArray() [10/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< string > &  value 
)

◆ setArray() [11/11]

CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray ( const string &  propertyPath,
const vector< unsigned long long > &  value 
)

Member Data Documentation

◆ yyjsonMutableDoc

yyjson_mut_doc* conscience_utils::JSON::CscJsonObject::yyjsonMutableDoc = nullptr
protected

yyjson has 2 APIs : read & write. In this class, mutable model has priority, read-only objects are created from mutable objects

See also
getYyjsonValue()
getYyjsonDoc()
getYyjsonMutableDoc()
getYyjsonMutableValue()

◆ yyjsonMutableValue

yyjson_mut_val* conscience_utils::JSON::CscJsonObject::yyjsonMutableValue = nullptr
protected

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