107 lines
3.3 KiB
CMake
107 lines
3.3 KiB
CMake
cmake_minimum_required(VERSION 3.20)
|
|
|
|
##
|
|
## @brief get the type of a variable
|
|
## @param[in] INPUT_JSON Json data.
|
|
## @param[in] VARIABLE Name of the variable.
|
|
## @param[out] OUT_VAR Retrun type of the node: NULL, NUMBER, STRING, BOOLEAN, ARRAY, OBJECT or NOTFOUND (if does not exist)
|
|
##
|
|
function(json_get_type OUT_VAR INPUT_JSON VARIABLE)
|
|
string(JSON VALUE ERROR_VARIABLE ${VARIABLE} TYPE ${INPUT_JSON} ${VARIABLE})
|
|
if (${VALUE} STREQUAL ${VARIABLE}-NOTFOUND)
|
|
set(${OUT_VAR} "NOTFOUND" PARENT_SCOPE)
|
|
else()
|
|
set(${OUT_VAR} "${VALUE}" PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
|
|
function(json_size OUT_VAR INPUT_JSON VARIABLE)
|
|
string(JSON VALUE ERROR_VARIABLE ${VARIABLE} LENGTH ${INPUT_JSON} ${VARIABLE})
|
|
if (${VALUE} STREQUAL ${VARIABLE}-NOTFOUND)
|
|
set("${OUT_VAR}" 0 PARENT_SCOPE)
|
|
else ()
|
|
set("${OUT_VAR}" ${VALUE} PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
function(json_get_data OUT_VAR INPUT_JSON VARIABLE)
|
|
string(JSON VALUE GET ${INPUT_JSON} ${VARIABLE})
|
|
set("${OUT_VAR}" ${VALUE} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(json_get_data_or_default OUT_VAR INPUT_JSON VARIABLE DEFAULT_VALUE)
|
|
json_get_type(TYPE ${INPUT_JSON} ${VARIABLE})
|
|
if (${TYPE} STREQUAL "NOTFOUND")
|
|
set("${OUT_VAR}" ${DEFAULT_VALUE} PARENT_SCOPE)
|
|
else()
|
|
string(JSON VALUE GET ${INPUT_JSON} ${VARIABLE})
|
|
set("${OUT_VAR}" ${VALUE} PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
function(json_object_key OUT_VAR INPUT_JSON IDX)
|
|
string(JSON VALUE MEMBER ${INPUT_JSON} ${IDX})
|
|
set("${OUT_VAR}" ${VALUE} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(json_object_keys OUT_VAR MY_JSON_STRING VARIABLE)
|
|
json_size(SIZE ${MY_JSON_STRING} ${VARIABLE})
|
|
#message("target SIZE = ${SIZE}")
|
|
json_get_data(OBJECT_DATA ${MY_JSON_STRING} ${VARIABLE})
|
|
if (SIZE EQUAL 0)
|
|
return()
|
|
endif()
|
|
MATH(EXPR SIZE "${SIZE}-1")
|
|
set(OUT "")
|
|
foreach(IDX RANGE ${SIZE})
|
|
json_object_key(ELEMENT ${OBJECT_DATA} ${IDX})
|
|
#message(" - : ${ELEMENT}")
|
|
list(APPEND OUT ${ELEMENT})
|
|
endforeach()
|
|
set("${OUT_VAR}" ${OUT} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(json_object_values OUT_VAR INPUT_JSON VARIABLE KEYS)
|
|
string(JSON VALUE GET ${INPUT_JSON} ${VARIABLE} ${KEYS})
|
|
set("${OUT_VAR}" ${VALUE} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(json_get_list OUT_VAR INPUT_JSON VARIABLE)
|
|
string(JSON LIST_JSON_ELEMENTS ERROR_VARIABLE ${VARIABLE} GET ${INPUT_JSON} ${VARIABLE})
|
|
if (${LIST_JSON_ELEMENTS} STREQUAL ${VARIABLE}-NOTFOUND)
|
|
set("${OUT_VAR}" PARENT_SCOPE)
|
|
retrun()
|
|
endif()
|
|
#message("LIST_JSON_ELEMENTS : ${LIST_JSON_ELEMENTS}")
|
|
|
|
string(JSON LENGTH_VALUE LENGTH ${LIST_JSON_ELEMENTS})
|
|
#message("LENGTH_VALUE : ${LENGTH_VALUE}")
|
|
if (${LENGTH_VALUE} EQUAL 0)
|
|
set("${OUT_VAR}" PARENT_SCOPE)
|
|
retrun()
|
|
endif()
|
|
set(OUT_LIST)
|
|
MATH(EXPR LENGTH_VALUE "${LENGTH_VALUE}-1")
|
|
foreach(IDX RANGE ${LENGTH_VALUE})
|
|
string(JSON ELEM GET ${LIST_JSON_ELEMENTS} ${IDX})
|
|
#message(" - : ${ELEM}")
|
|
list(APPEND OUT_LIST ${ELEM})
|
|
endforeach()
|
|
#message("OUT_LIST : ${OUT_LIST}")
|
|
set("${OUT_VAR}" ${OUT_LIST} PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
|
|
function(json_get_element OUT_VAR INPUT_JSON VARIABLE)
|
|
string(JSON ELEMENT ERROR_VARIABLE ${VARIABLE} GET ${INPUT_JSON} ${VARIABLE})
|
|
if ("${ELEMENT}" STREQUAL "${VARIABLE}-NOTFOUND")
|
|
set("${OUT_VAR}" "" PARENT_SCOPE)
|
|
elseif ("${ELEMENT}" STREQUAL "NOTFOUND")
|
|
set("${OUT_VAR}" "" PARENT_SCOPE)
|
|
else()
|
|
set("${OUT_VAR}" ${ELEMENT} PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|