Skip to content

Commit

Permalink
remove using json=nlohmann::json from header files to avoid pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed Feb 12, 2024
1 parent cb7e32f commit d6259c3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
7 changes: 3 additions & 4 deletions modules/core/include/visp3/core/vpCannyEdgeDetection.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
// 3rd parties include
#ifdef VISP_HAVE_NLOHMANN_JSON
#include <nlohmann/json.hpp>
using json = nlohmann::json; //! json namespace shortcut
#endif

class VISP_EXPORT vpCannyEdgeDetection
Expand Down Expand Up @@ -213,7 +212,7 @@ class VISP_EXPORT vpCannyEdgeDetection
* \param[in] j : The JSON object, resulting from the parsing of a JSON file.
* \param[out] detector : The detector that will be initialized from the JSON data.
*/
friend inline void from_json(const json &j, vpCannyEdgeDetection &detector)
friend inline void from_json(const nlohmann::json &j, vpCannyEdgeDetection &detector)
{
std::string filteringAndGradientName = vpImageFilter::vpCannyFilteringAndGradientTypeToString(detector.m_filteringAndGradientType);
filteringAndGradientName = j.value("filteringAndGradientType", filteringAndGradientName);
Expand All @@ -233,10 +232,10 @@ class VISP_EXPORT vpCannyEdgeDetection
* \param[out] j : A JSON parser object.
* \param[in] detector : The vpCannyEdgeDetection object that must be parsed into JSON format.
*/
friend inline void to_json(json &j, const vpCannyEdgeDetection &detector)
friend inline void to_json(nlohmann::json &j, const vpCannyEdgeDetection &detector)
{
std::string filteringAndGradientName = vpImageFilter::vpCannyFilteringAndGradientTypeToString(detector.m_filteringAndGradientType);
j = json {
j = nlohmann::json {
{"filteringAndGradientType", filteringAndGradientName},
{"gaussianSize", detector.m_gaussianKernelSize},
{"gaussianStdev", detector.m_gaussianStdev},
Expand Down
3 changes: 3 additions & 0 deletions modules/core/src/image/vpCannyEdgeDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ vpCannyEdgeDetection::vpCannyEdgeDetection(const int &gaussianKernelSize, const
}

#ifdef VISP_HAVE_NLOHMANN_JSON

using json = nlohmann::json;

vpCannyEdgeDetection::vpCannyEdgeDetection(const std::string &jsonPath)
{
initFromJSON(jsonPath);
Expand Down
13 changes: 6 additions & 7 deletions modules/detection/include/visp3/detection/vpDetectorDNNOpenCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include <optional>
#ifdef VISP_HAVE_NLOHMANN_JSON
#include <nlohmann/json.hpp>
using json = nlohmann::json; //! json namespace shortcut
#endif

/*!
Expand Down Expand Up @@ -201,7 +200,7 @@ class VISP_EXPORT vpDetectorDNNOpenCV
* \param j The JSON object, resulting from the parsing of a JSON file.
* \param config The configuration of the network, that will be initialized from the JSON data.
*/
friend inline void from_json(const json &j, NetConfig &config)
friend inline void from_json(const nlohmann::json &j, NetConfig &config)
{
config.m_confThreshold = j.value("confidenceThreshold", config.m_confThreshold);
if (config.m_confThreshold <= 0) {
Expand Down Expand Up @@ -241,11 +240,11 @@ class VISP_EXPORT vpDetectorDNNOpenCV
* \param j A JSON parser object.
* \param config The vpDetectorDNNOpenCV::NetConfig that must be parsed into JSON format.
*/
friend inline void to_json(json &j, const NetConfig &config)
friend inline void to_json(nlohmann::json &j, const NetConfig &config)
{
std::pair<unsigned int, unsigned int> resolution = { config.m_inputSize.width, config.m_inputSize.height };
std::vector<double> v_mean = { config.m_mean[0], config.m_mean[1], config.m_mean[2] };
j = json {
j = nlohmann::json {
{"confidenceThreshold", config.m_confThreshold } ,
{"nmsThreshold" , config.m_nmsThreshold } ,
{"filterSizeRatio" , config.m_filterSizeRatio} ,
Expand Down Expand Up @@ -515,7 +514,7 @@ class VISP_EXPORT vpDetectorDNNOpenCV
* \param j The JSON object, resulting from the parsing of a JSON file.
* \param network The network, that will be initialized from the JSON data.
*/
friend inline void from_json(const json &j, vpDetectorDNNOpenCV &network)
friend inline void from_json(const nlohmann::json &j, vpDetectorDNNOpenCV &network)
{
network.m_netConfig = j.value("networkSettings", network.m_netConfig);
}
Expand All @@ -526,9 +525,9 @@ class VISP_EXPORT vpDetectorDNNOpenCV
* \param j The JSON parser.
* \param network The network we want to parse the configuration.
*/
friend inline void to_json(json &j, const vpDetectorDNNOpenCV &network)
friend inline void to_json(nlohmann::json &j, const vpDetectorDNNOpenCV &network)
{
j = json {
j = nlohmann::json {
{"networkSettings", network.m_netConfig}
};
}
Expand Down
3 changes: 3 additions & 0 deletions modules/detection/src/dnn/vpDetectorDNNOpenCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ vpDetectorDNNOpenCV::vpDetectorDNNOpenCV(const NetConfig &config, const DNNResul
}

#ifdef VISP_HAVE_NLOHMANN_JSON

using json = nlohmann::json;

/**
* \brief Construct a new vpDetectorDNNOpenCV object from a JSON file and a potential parsing method.
*
Expand Down
14 changes: 8 additions & 6 deletions modules/imgproc/include/visp3/imgproc/vpCircleHoughTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
// 3rd parties inclue
#ifdef VISP_HAVE_NLOHMANN_JSON
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#endif

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17)
Expand Down Expand Up @@ -496,6 +495,8 @@ class VISP_EXPORT vpCircleHoughTransform
*/
inline static vpCircleHoughTransformParameters createFromJSON(const std::string &jsonFile)
{
using json = nlohmann::json;

std::ifstream file(jsonFile);
if (!file.good()) {
std::stringstream ss;
Expand Down Expand Up @@ -528,6 +529,7 @@ class VISP_EXPORT vpCircleHoughTransform
*/
inline void saveConfigurationInJSON(const std::string &jsonPath) const
{
using json = nlohmann::json;
std::ofstream file(jsonPath);
const json j = *this;
file << j.dump(4);
Expand All @@ -541,7 +543,7 @@ class VISP_EXPORT vpCircleHoughTransform
* \param[in] j : The JSON object, resulting from the parsing of a JSON file.
* \param[out] params : The circle Hough transform parameters that will be initialized from the JSON data.
*/
friend inline void from_json(const json &j, vpCircleHoughTransformParameters &params)
friend inline void from_json(const nlohmann::json &j, vpCircleHoughTransformParameters &params)
{
std::string filteringAndGradientName = vpImageFilter::vpCannyFilteringAndGradientTypeToString(params.m_filteringAndGradientType);
filteringAndGradientName = j.value("filteringAndGradientType", filteringAndGradientName);
Expand Down Expand Up @@ -619,11 +621,11 @@ class VISP_EXPORT vpCircleHoughTransform
* \param[out] j : A JSON parser object.
* \param[in] params : The circle Hough transform parameters that will be serialized in the json object.
*/
friend inline void to_json(json &j, const vpCircleHoughTransformParameters &params)
friend inline void to_json(nlohmann::json &j, const vpCircleHoughTransformParameters &params)
{
std::pair<unsigned int, unsigned int> radiusLimits = { params.m_minRadius, params.m_maxRadius };

j = json {
j = nlohmann::json {
{"filteringAndGradientType", vpImageFilter::vpCannyFilteringAndGradientTypeToString(params.m_filteringAndGradientType)},
{"gaussianKernelSize", params.m_gaussianKernelSize},
{"gaussianStdev", params.m_gaussianStdev},
Expand Down Expand Up @@ -760,7 +762,7 @@ class VISP_EXPORT vpCircleHoughTransform
* \param[in] j The JSON object, resulting from the parsing of a JSON file.
* \param[out] detector The detector, that will be initialized from the JSON data.
*/
friend inline void from_json(const json &j, vpCircleHoughTransform &detector)
friend inline void from_json(const nlohmann::json &j, vpCircleHoughTransform &detector)
{
detector.m_algoParams = j;
}
Expand All @@ -771,7 +773,7 @@ class VISP_EXPORT vpCircleHoughTransform
* \param[out] j A JSON parser object.
* \param[in] detector The vpCircleHoughTransform that must be parsed into JSON format.
*/
friend inline void to_json(json &j, const vpCircleHoughTransform &detector)
friend inline void to_json(nlohmann::json &j, const vpCircleHoughTransform &detector)
{
j = detector.m_algoParams;
}
Expand Down
2 changes: 2 additions & 0 deletions modules/imgproc/src/vpCircleHoughTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ vpCircleHoughTransform::~vpCircleHoughTransform()
{ }

#ifdef VISP_HAVE_NLOHMANN_JSON
using json = nlohmann::json;

vpCircleHoughTransform::vpCircleHoughTransform(const std::string &jsonPath)
{
initFromJSON(jsonPath);
Expand Down

0 comments on commit d6259c3

Please sign in to comment.