Skip to content

Commit

Permalink
Cleaned up header files
Browse files Browse the repository at this point in the history
- added schenity example
  • Loading branch information
GavinNL committed Dec 19, 2023
1 parent a963db4 commit dc7406e
Show file tree
Hide file tree
Showing 23 changed files with 860 additions and 48 deletions.
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,22 @@ if( IMJSCHEMA_BUILD_EXECUTABLES)
set(srcFiles example.cpp ) # all the source files for this library
set(PublicLinkedTargets "" )
set(PrivateLinkedTargets ImJSchema::ImJSchema SDL2::SDL2 nlohmann_json::nlohmann_json imguiLib)

#myproject_set_project_warnings(ImJSchema TRUE "" "" "" "")

add_executable( ${outName} ${srcFiles} )
target_link_libraries( ${outName} PUBLIC ${PublicLinkedTargets} )
target_link_libraries( ${outName} PRIVATE ${PrivateLinkedTargets} )
#-------------------------------------------------------------------------------


set(outName schenity) # name of the library
set(srcFiles schenity.cpp ) # all the source files for this library
set(PublicLinkedTargets "" )
set(PrivateLinkedTargets ImJSchema::ImJSchema SDL2::SDL2 nlohmann_json::nlohmann_json imguiLib)
add_executable( ${outName} ${srcFiles} )

target_link_libraries( ${outName} PUBLIC ${PublicLinkedTargets} )
target_link_libraries( ${outName} PRIVATE ${PrivateLinkedTargets} )
#-------------------------------------------------------------------------------

################################################################################
enable_testing()
add_subdirectory(test)

endif()
3 changes: 3 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
sdl/2.26.1
nlohmann_json/3.9.1
catch2/3.3.2
imgui/1.90-docking

[options]
sdl:shared=False

[generators]
cmake_find_package

[imports]
./res, * -> imgui_src @ root_package=imgui
136 changes: 97 additions & 39 deletions include/ImJSchema/detail/json_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,73 @@ namespace ImJSchema
{
using json = nlohmann::json;

/**
* @brief jsonExpandAllReferences
* @param J
*
* Given a json object that has a property named, ref, which is
* a string that represents a path to an object in defsRoot
*
# eg:
"items": {
"name" : "Hello",
"defaultValue" : "World",
"$ref": "#/$defs/positiveInteger"
}
// defs
{
"$defs": {
"positiveInteger": {
"type": "integer",
"exclusiveMinimum": 0,
"defaultValue" : "Hello"
}
}
}
Copies the values from defs which are not in the original object
{
"items": {
"name" : "Hello",
"defaultValue" : "World",
"type": "integer",
"exclusiveMinimum": 0
}
}
*/
inline void jsonExpandAllReferences(json & J, json const & defs, std::string ref= "$ref");
inline void jsonExpandAllReferences(json & J, std::string ref= "$ref");

/**
* @brief jsonFindPath
* @param path
* @param obj
* @return
*
* Returns a pointer to a json object that exists in obj based on its path.
*
* eg:
* a path looks like: "objName/array/0"
*
* {
* "objName" : {
* "array" : ["first" , "second" ]
* }
* }
*/
inline json * jsonFindPath(std::string_view const path, json & obj);
inline json const* jsonFindPath(std::string_view const path, json const & obj);


/**
* @brief doIfKeyExists
* @param K
* @param J
* @param C
*
* Executes the callable if a key, K, exists in J
*/
template<typename jsonObject, typename Callable_type>
void doIfKeyExists(json::object_t::key_type const &K, jsonObject & J, Callable_type && C)
{
Expand All @@ -18,6 +85,16 @@ void doIfKeyExists(json::object_t::key_type const &K, jsonObject & J, Callable_t
}


/**
* @brief JValue
* @param J
* @param key
* @param default_value
* @return
*
* Returns the ValueType, if the key, exists in J and is the same type as ValueType, If not,
* returns the default value
*/
template<typename ValueType>
ValueType JValue(json const & J, json::object_t::key_type const & key, const ValueType& default_value)
{
Expand Down Expand Up @@ -58,8 +135,10 @@ ValueType JValue(json const & J, json::object_t::key_type const & key, const Val
}
}



template<typename T>
inline T * _jsonFindPath(std::string_view path, T & obj);
inline T * _jsonFindPath(std::string_view const path, T & obj);

/**
* @brief jsonFindPath
Expand All @@ -69,49 +148,18 @@ inline T * _jsonFindPath(std::string_view path, T & obj);
*
* Given a path in a json object. Returns a pointer to the json object
*/
inline json const* jsonFindPath(std::string_view path, json const & obj)
inline json const* jsonFindPath(std::string_view const path, json const & obj)
{
return _jsonFindPath<json const>(path, obj);
}
inline json * jsonFindPath(std::string_view path, json & obj)
inline json * jsonFindPath(std::string_view const path, json & obj)
{
return _jsonFindPath<json>(path, obj);
}

/**
* @brief jsonExpandDef
* @param J
*
* Given a json object that has a property named, ref, which is
* a string that represents a path to an object in defsRoot
# eg:
"items": {
"name" : "Hello",
"defaultValue" : "World",
"$ref": "#/$defs/positiveInteger"
}

// defsRoot
{
"$defs": {
"positiveInteger": {
"type": "integer",
"exclusiveMinimum": 0,
"defaultValue" : "Hello"
}
}
}

Copies the values from defsRoot which are not in the original object
{
"items": {
"name" : "Hello",
"defaultValue" : "World",
"type": "integer",
"exclusiveMinimum": 0
}
}
*/
//=============== Private Functions ======================

/**
* @brief jsonExpandDefs
Expand Down Expand Up @@ -205,7 +253,15 @@ inline void _jsonExpandReference(json & J, json const & defs, std::string ref =
}


inline void jsonExpandAllReferences(json & J, json const & defs, std::string ref= "$ref")
/**
* @brief jsonExpandAllReferences
* @param J
* @param defs
* @param ref
*
* Recursive fuction which expands ALL definitions
*/
inline void jsonExpandAllReferences(json & J, json const & defs, std::string ref)
{
if(J.is_array())
{
Expand All @@ -224,15 +280,17 @@ inline void jsonExpandAllReferences(json & J, json const & defs, std::string ref
}
}

inline void jsonExpandAllReferences(json & J, std::string ref= "$ref")
inline void jsonExpandAllReferences(json & J, std::string ref)
{
jsonExpandAllReferences(J, J, ref);
}


//=============== Private Functions ======================



template<typename T>
inline T * _jsonFindPath(std::string_view path, T & obj)
inline T * _jsonFindPath(std::string_view const path, T & obj)
{
// path == "grandParent/parent/child"
if(path.empty())
Expand Down
Loading

0 comments on commit dc7406e

Please sign in to comment.