Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Functions
JsonHelper Namespace Reference

Functions

bool LoadJsonFromFile (const std::string &filepath, json &j)
 Load and parse a JSON file.
 
bool SaveJsonToFile (const std::string &filepath, const json &j, int indent=4)
 Save a JSON object to a file with formatting.
 
std::string GetString (const json &j, const std::string &key, const std::string &defaultValue="")
 Safely get a string value from JSON.
 
int GetInt (const json &j, const std::string &key, int defaultValue=0)
 Safely get an integer value from JSON.
 
uint32_t GetUInt (const json &j, const std::string &key, uint32_t defaultValue=0)
 Safely get an unsigned integer value from JSON.
 
float GetFloat (const json &j, const std::string &key, float defaultValue=0.0f)
 Safely get a float value from JSON.
 
double GetDouble (const json &j, const std::string &key, double defaultValue=0.0)
 Safely get a double value from JSON.
 
bool GetBool (const json &j, const std::string &key, bool defaultValue=false)
 Safely get a boolean value from JSON.
 
template<typename T >
T json_get (const json &j, const std::string &key, const T &defaultValue)
 Generic template function to safely get any type from JSON.
 
bool IsArray (const json &j, const std::string &key)
 Check if a key contains an array.
 
bool IsObject (const json &j, const std::string &key)
 Check if a key contains an object.
 
size_t GetArraySize (const json &j, const std::string &key)
 Get the size of an array.
 
void ForEachInArray (const json &j, const std::string &key, std::function< void(const json &, size_t)> callback)
 Iterate over an array with a callback function.
 
float GetNestedFloat (const json &j, const std::string &parentKey, const std::string &childKey, float defaultValue=0.0f)
 Get a nested float value from a parent object.
 
std::string GetNestedString (const json &j, const std::string &parentKey, const std::string &childKey, const std::string &defaultValue="")
 Get a nested string value from a parent object.
 
int GetNestedInt (const json &j, const std::string &parentKey, const std::string &childKey, int defaultValue=0)
 Get a nested int value from a parent object.
 
bool LoadConfig (const std::string &filepath, json &j)
 Load a configuration file (alias for LoadJsonFromFile)
 
bool SaveConfig (const std::string &filepath, const json &j)
 Save a configuration file (alias for SaveJsonToFile with indent=2)
 
bool ValidateKeys (const json &j, const std::vector< std::string > &requiredKeys)
 Validate that all required keys exist in a JSON object.
 
bool ValidateKeysVerbose (const json &j, const std::vector< std::string > &requiredKeys, const std::string &context="")
 Validate that all required keys exist and log which ones are missing.
 

Function Documentation

◆ ForEachInArray()

void JsonHelper::ForEachInArray ( const json j,
const std::string &  key,
std::function< void(const json &, size_t)>  callback 
)
inline

Iterate over an array with a callback function.

Parameters
jJSON object
keyKey to the array
callbackFunction to call for each element (takes const json& and size_t index)

Definition at line 263 of file json_helper.h.

References GetComponentTypeID_Static(), and IsArray().

Referenced by Olympe::Blueprint::EntityBlueprint::FromJson(), Graph::FromJson(), Olympe::NodeGraph::FromJson(), BehaviorTreeManager::LoadTreeFromFile(), Olympe::EnumCatalogManager::ParseCatalogJson(), and Olympe::EnumCatalogManager::ParseCatalogType().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetArraySize()

size_t JsonHelper::GetArraySize ( const json j,
const std::string &  key 
)
inline

Get the size of an array.

Parameters
jJSON object
keyKey to the array
Returns
Size of the array, or 0 if not an array

Definition at line 250 of file json_helper.h.

References GetComponentTypeID_Static(), and IsArray().

+ Here is the call graph for this function:

◆ GetBool()

bool JsonHelper::GetBool ( const json j,
const std::string &  key,
bool  defaultValue = false 
)
inline

Safely get a boolean value from JSON.

Parameters
jJSON object
keyKey to access
defaultValueDefault value if key doesn't exist or type mismatch
Returns
The bool value or default

Definition at line 182 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by Olympe::AnimationEditorWindow::ImportBankJSON(), Olympe::BehaviorTreeDebugWindow::LoadBTConfig(), Olympe::EnumCatalogManager::ParseCatalogParameter(), OlympeAnimation::AnimationBank::ParseJSON(), and OlympeAnimation::AnimationGraph::ParseJSON().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetDouble()

double JsonHelper::GetDouble ( const json j,
const std::string &  key,
double  defaultValue = 0.0 
)
inline

Safely get a double value from JSON.

Parameters
jJSON object
keyKey to access
defaultValueDefault value if key doesn't exist or type mismatch
Returns
The double value or default

Definition at line 168 of file json_helper.h.

References GetComponentTypeID_Static().

+ Here is the call graph for this function:

◆ GetFloat()

float JsonHelper::GetFloat ( const json j,
const std::string &  key,
float  defaultValue = 0.0f 
)
inline

Safely get a float value from JSON.

Parameters
jJSON object
keyKey to access
defaultValueDefault value if key doesn't exist or type mismatch
Returns
The float value or default

Definition at line 154 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by Graph::FromJson(), Olympe::NodeGraph::FromJson(), GetNestedFloat(), Olympe::AnimationEditorWindow::ImportBankJSON(), Olympe::BehaviorTreeDebugWindow::LoadBTConfig(), BehaviorTreeManager::LoadTreeFromFile(), OlympeAnimation::AnimationBank::ParseJSON(), and OlympeAnimation::AnimationGraph::ParseJSON().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetInt()

int JsonHelper::GetInt ( const json j,
const std::string &  key,
int  defaultValue = 0 
)
inline

Safely get an integer value from JSON.

Parameters
jJSON object
keyKey to access
defaultValueDefault value if key doesn't exist or type mismatch
Returns
The int value or default

Definition at line 120 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by Graph::FromJson(), Olympe::NodeGraph::FromJson(), GetNestedInt(), Olympe::AnimationEditorWindow::ImportBankJSON(), Olympe::BehaviorTreeDebugWindow::LoadBTConfig(), BehaviorTreeManager::LoadTreeFromFile(), OlympeAnimation::AnimationBank::ParseJSON(), and OlympeAnimation::AnimationGraph::ParseJSON().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetNestedFloat()

float JsonHelper::GetNestedFloat ( const json j,
const std::string &  parentKey,
const std::string &  childKey,
float  defaultValue = 0.0f 
)
inline

Get a nested float value from a parent object.

Parameters
jJSON object
parentKeyKey to the parent object
childKeyKey within the parent object
defaultValueDefault value if not found
Returns
The float value or default

Definition at line 283 of file json_helper.h.

References GetComponentTypeID_Static(), GetFloat(), and IsObject().

+ Here is the call graph for this function:

◆ GetNestedInt()

int JsonHelper::GetNestedInt ( const json j,
const std::string &  parentKey,
const std::string &  childKey,
int  defaultValue = 0 
)
inline

Get a nested int value from a parent object.

Parameters
jJSON object
parentKeyKey to the parent object
childKeyKey within the parent object
defaultValueDefault value if not found
Returns
The int value or default

Definition at line 317 of file json_helper.h.

References GetComponentTypeID_Static(), GetInt(), and IsObject().

+ Here is the call graph for this function:

◆ GetNestedString()

std::string JsonHelper::GetNestedString ( const json j,
const std::string &  parentKey,
const std::string &  childKey,
const std::string &  defaultValue = "" 
)
inline

Get a nested string value from a parent object.

Parameters
jJSON object
parentKeyKey to the parent object
childKeyKey within the parent object
defaultValueDefault value if not found
Returns
The string value or default

Definition at line 300 of file json_helper.h.

References GetComponentTypeID_Static(), GetString(), and IsObject().

+ Here is the call graph for this function:

◆ GetString()

std::string JsonHelper::GetString ( const json j,
const std::string &  key,
const std::string &  defaultValue = "" 
)
inline

◆ GetUInt()

uint32_t JsonHelper::GetUInt ( const json j,
const std::string &  key,
uint32_t  defaultValue = 0 
)
inline

Safely get an unsigned integer value from JSON.

Parameters
jJSON object
keyKey to access
defaultValueDefault value if key doesn't exist or type mismatch
Returns
The uint32_t value or default

Definition at line 134 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by BehaviorTreeManager::LoadTreeFromFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsArray()

bool JsonHelper::IsArray ( const json j,
const std::string &  key 
)
inline

Check if a key contains an array.

Parameters
jJSON object
keyKey to check
Returns
true if the key exists and is an array

Definition at line 228 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by ForEachInArray(), Olympe::Blueprint::EntityBlueprint::FromJson(), Graph::FromJson(), Olympe::NodeGraph::FromJson(), GetArraySize(), BehaviorTreeManager::LoadTreeFromFile(), Olympe::EnumCatalogManager::ParseCatalogJson(), and Olympe::EnumCatalogManager::ParseCatalogType().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsObject()

bool JsonHelper::IsObject ( const json j,
const std::string &  key 
)
inline

Check if a key contains an object.

Parameters
jJSON object
keyKey to check
Returns
true if the key exists and is an object

Definition at line 239 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by GetNestedFloat(), GetNestedInt(), GetNestedString(), and Olympe::BehaviorTreeDebugWindow::LoadBTConfig().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_get()

template<typename T >
T JsonHelper::json_get ( const json j,
const std::string &  key,
const T defaultValue 
)
inline

Generic template function to safely get any type from JSON.

Template Parameters
TType to extract
Parameters
jJSON object
keyKey to access
defaultValueDefault value if key doesn't exist or type mismatch
Returns
The value of type T or default
Note
Use specialized functions (GetInt, GetFloat, etc.) when available for better type safety. This template is useful for custom types or when working with generic code.

Definition at line 202 of file json_helper.h.

References GetComponentTypeID_Static().

+ Here is the call graph for this function:

◆ LoadConfig()

bool JsonHelper::LoadConfig ( const std::string &  filepath,
json j 
)
inline

Load a configuration file (alias for LoadJsonFromFile)

Parameters
filepathPath to the config file
jOutput json object
Returns
true if successful, false otherwise

Definition at line 336 of file json_helper.h.

References GetComponentTypeID_Static(), and LoadJsonFromFile().

+ Here is the call graph for this function:

◆ LoadJsonFromFile()

bool JsonHelper::LoadJsonFromFile ( const std::string &  filepath,
json j 
)
inline

◆ SaveConfig()

bool JsonHelper::SaveConfig ( const std::string &  filepath,
const json j 
)
inline

Save a configuration file (alias for SaveJsonToFile with indent=2)

Parameters
filepathPath to save the config file
jJSON object to save
Returns
true if successful, false otherwise

Definition at line 347 of file json_helper.h.

References GetComponentTypeID_Static(), and SaveJsonToFile().

+ Here is the call graph for this function:

◆ SaveJsonToFile()

bool JsonHelper::SaveJsonToFile ( const std::string &  filepath,
const json j,
int  indent = 4 
)
inline

Save a JSON object to a file with formatting.

Parameters
filepathPath to save the file
jJSON object to save
indentNumber of spaces for indentation (default: 4)
Returns
true if successful, false otherwise

Definition at line 73 of file json_helper.h.

References GetComponentTypeID_Static().

Referenced by Olympe::AnimationEditorWindow::ExportBankJSON(), Olympe::BehaviorTreeDebugWindow::Save(), SaveConfig(), and Olympe::Blueprint::EntityBlueprint::SaveToFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ValidateKeys()

bool JsonHelper::ValidateKeys ( const json j,
const std::vector< std::string > &  requiredKeys 
)
inline

Validate that all required keys exist in a JSON object.

Parameters
jJSON object to validate
requiredKeysVector of required key names
Returns
true if all keys exist, false otherwise

Definition at line 362 of file json_helper.h.

References GetComponentTypeID_Static().

+ Here is the call graph for this function:

◆ ValidateKeysVerbose()

bool JsonHelper::ValidateKeysVerbose ( const json j,
const std::vector< std::string > &  requiredKeys,
const std::string &  context = "" 
)
inline

Validate that all required keys exist and log which ones are missing.

Parameters
jJSON object to validate
requiredKeysVector of required key names
contextContext string for error messages
Returns
true if all keys exist, false otherwise

Definition at line 382 of file json_helper.h.

References GetComponentTypeID_Static().

+ Here is the call graph for this function: