#include <json.h>
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< CscJsonObject > | getObjectMutableValue (const string &key) |
| CscJsonObject * | getRequiredObjectMutablePtrValue (const string &key) |
| optional< CscJsonObject * > | getObjectMutablePtrValue (const string &key) |
| ptr< CscJsonObject > | getRequiredObjectMutableSharedPtrValue (const string &key) |
| optional< ptr< CscJsonObject > > | getObjectMutableSharedPtrValue (const string &key) |
| CscJsonObject * | set (const string &propertyPath, bool value) |
| CscJsonObject * | set (const string &propertyPath, const char *value) |
| CscJsonObject * | set (const string &propertyPath, const string &value) |
| CscJsonObject * | set (const string &propertyPath, double value) |
| CscJsonObject * | set (const string &propertyPath, int value) |
| CscJsonObject * | set (const string &propertyPath, long long value) |
| CscJsonObject * | set (const string &propertyPath, unsigned long long value) |
| CscJsonObject * | set (const string &propertyPath, CscJsonObject &object) |
| CscJsonObject * | set (const string &propertyPath, CscJsonObject *object) |
| CscJsonObject * | set (const string &propertyPath, ptr< CscJsonObject > object) |
| CscJsonObject * | set (const string &propertyPath, const map< string, string > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< bool > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< string > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< float > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< double > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< int > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< long long > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< unsigned long long > &value) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< CscJsonObject * > &objects) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< CscJsonObject > &objects) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< ptr< CscJsonObject >> &objects) |
| CscJsonObject * | setArray (const string &propertyPath, const vector< map< string, string >> &value) |
| CscJsonObject * | remove (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< CscJsonObjectReader > | getObjectValue (const string &key) |
| CscJsonObjectReader * | getRequiredObjectPtrValue (const string &key) |
| optional< CscJsonObjectReader * > | getObjectPtrValue (const string &key) |
| ptr< CscJsonObjectReader > | getRequiredObjectSharedPtrValue (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 > | |
| 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) |
| CscJsonObject * | toMutable (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) |
| CscJsonObject * | set (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) |
This class provides full json features, read and write @notethis class is NOT thread safe
| conscience_utils::JSON::CscJsonObject::CscJsonObject | ( | const fs::path & | jsonFilePath | ) |
| conscience_utils::JSON::CscJsonObject::CscJsonObject | ( | const string & | jsonString | ) |
| conscience_utils::JSON::CscJsonObject::CscJsonObject | ( | const map< string, string > & | map | ) |
Creates a modifiable json object from existing values in given map
| 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
| conscience_utils::JSON::CscJsonObject::CscJsonObject | ( | ) |
Creates a new and empty modifiable json object
| conscience_utils::JSON::CscJsonObject::~CscJsonObject | ( | ) |
|
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.
| json | The compact JSON string to be reformatted. |
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.
| yyjson_mut_val * conscience_utils::JSON::CscJsonObject::getLowLevelMutableObject | ( | ) |
|
protected |
| optional< CscJsonObject * > conscience_utils::JSON::CscJsonObject::getObjectMutablePtrValue | ( | const string & | key | ) |
gets optional object value, you need to delete it
| optional< ptr< CscJsonObject > > conscience_utils::JSON::CscJsonObject::getObjectMutableSharedPtrValue | ( | const string & | key | ) |
gets optional object value
| optional< CscJsonObject > conscience_utils::JSON::CscJsonObject::getObjectMutableValue | ( | const string & | key | ) |
gets optional object value
|
protected |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::getRequiredObjectMutablePtrValue | ( | const string & | key | ) |
gets object value, or throw error, you need to delete it
| ptr< CscJsonObject > conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableSharedPtrValue | ( | const string & | key | ) |
gets object value, or throw error
| CscJsonObject conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableValue | ( | const string & | key | ) |
gets object value, or throw error
|
overrideprotectedvirtual |
Design note : getYyjsonXXX() - This part is a bit tricky Lazy init of read-only and mutable objects :
Reimplemented from conscience_utils::JSON::CscJsonObjectReader.
|
protected |
|
protected |
|
overrideprotectedvirtual |
Reimplemented from conscience_utils::JSON::CscJsonObjectReader.
| void conscience_utils::JSON::CscJsonObject::mergeKeyWith | ( | CscJsonObject & | other, |
| const string & | subPropertyPath, | ||
| bool | deep = true |
||
| ) |
|
protected |
| 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
| deep | pass to false if you only want to process this object and not nested objects |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::remove | ( | const string & | propertyPath | ) |
WARNING : current implementation support property path from root only
| void conscience_utils::JSON::CscJsonObject::saveToFile | ( | const fs::path & | outPath, |
| bool | lineBreakAndTabulation = false |
||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| bool | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| const char * | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| const map< string, string > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| const string & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| CscJsonObject & | object | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| CscJsonObject * | object | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| double | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| int | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| long long | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| ptr< CscJsonObject > | object | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::set | ( | const string & | propertyPath, |
| unsigned long long | value | ||
| ) |
|
protected |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< bool > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< CscJsonObject * > & | objects | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< CscJsonObject > & | objects | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< double > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< float > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< int > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< long long > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< map< string, string >> & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< ptr< CscJsonObject >> & | objects | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< string > & | value | ||
| ) |
| CscJsonObject * conscience_utils::JSON::CscJsonObject::setArray | ( | const string & | propertyPath, |
| const vector< unsigned long long > & | value | ||
| ) |
|
protected |
yyjson has 2 APIs : read & write. In this class, mutable model has priority, read-only objects are created from mutable objects
|
protected |