Commit c2e80d3b authored by Philipp Schäfer's avatar Philipp Schäfer
Browse files

UtilsJSON: switched to nlohmann/json library and added "test"

parent 459d7780
#include <ITABase/UtilsJSON.h>
#include <libjson.h>
#include <fstream>
#include<nlohmann/json.hpp>
using namespace nlohmann;
#define JSON_INDENT 3
//-----ITASpectrum-----
void ITABase::Utils::JSON::Export(const CITASpectrum* pSpectrum, const std::string& sTargetFilePath)
{
JSONNode jnRoot;
jnRoot.push_back(JSONNode("name", pSpectrum->GetName()));
jnRoot.push_back(JSONNode("num_bands", pSpectrum->GetNumBands()));
jnRoot.push_back(JSONNode("value_unit", pSpectrum->GetValueUnit()));
json jRoot;
jRoot["name"] = pSpectrum->GetName ( );
jRoot["num_bands"] = pSpectrum->GetNumBands ( );
jRoot["value_unit"] = pSpectrum->GetValueUnit ( );
JSONNode jnSpectrum;
jnSpectrum.set_name("spectrum");
for (int i = 0; i < pSpectrum->GetNumBands(); i++)
json jSpectrum;
for ( int i = 0; i < pSpectrum->GetNumBands ( ); i++ )
{
JSONNode jnBin;
jnBin.set_name("bin" + std::to_string(i));
jnBin.push_back(JSONNode("center_frequency", pSpectrum->GetCenterFrequencies()[i]));
jnBin.push_back(JSONNode("value", pSpectrum->GetValues()[i]));
jnSpectrum.push_back(jnBin);
jSpectrum.push_back ( { { "center_frequency", pSpectrum->GetCenterFrequencies ( )[i] }, { "value", pSpectrum->GetValues ( )[i] } } );
}
jnRoot.push_back(jnSpectrum);
jRoot["spectrum"] = jSpectrum;
std::ofstream fsOut(sTargetFilePath);
fsOut << jnRoot.write_formatted();
fsOut.close();
std::ofstream fsOut ( sTargetFilePath );
fsOut << jRoot.dump ( JSON_INDENT );
fsOut.close ( );
}
// Statistics
//-----Statistics-----
JSONNode StatsToJSON(const ITABase::CStatistics& oStats)
json StatsToJSON ( const ITABase::CStatistics& oStats )
{
JSONNode jnStats;
jnStats.set_name(oStats.sName);
jnStats.push_back(JSONNode("name", oStats.sName));
jnStats.push_back(JSONNode("N", oStats.uiNumSamples));
jnStats.push_back(JSONNode("units", oStats.sUnits));
jnStats.push_back(JSONNode("min", oStats.dMin));
jnStats.push_back(JSONNode("max", oStats.dMax));
jnStats.push_back(JSONNode("mean", oStats.dMean));
jnStats.push_back(JSONNode("stddev", oStats.dStdDev));
return jnStats;
json jStats;
jStats["name"] = oStats.sName;
jStats["N"] = oStats.uiNumSamples;
jStats["units"] = oStats.sUnits;
jStats["min"] = oStats.dMin;
jStats["max"] = oStats.dMax;
jStats["mean"] = oStats.dMean;
jStats["stddev"] = oStats.dStdDev;
return jStats;
}
void ITABase::Utils::JSON::Export(const ITABase::CStatistics& oStats, const std::string& sTargetFilePath)
{
std::ofstream fsOut(sTargetFilePath);
fsOut << StatsToJSON(oStats).write_formatted();
fsOut.close();
std::ofstream fsOut ( sTargetFilePath );
fsOut << StatsToJSON ( oStats ).dump ( JSON_INDENT );
fsOut.close ( );
}
void ITABase::Utils::JSON::Export(const std::vector<ITABase::CStatistics>& voStats, const std::string& sTargetFilePath)
{
JSONNode jnRoot;
jnRoot.set_name("statistics");
for (auto& oStats : voStats)
jnRoot.push_back(StatsToJSON(oStats));
json jRoot;
for ( auto& oStats : voStats )
jRoot.push_back ( StatsToJSON ( oStats ) );
std::ofstream fsOut(sTargetFilePath);
fsOut << jnRoot.write_formatted();
fsOut.close();
std::ofstream fsOut ( sTargetFilePath );
fsOut << jRoot.dump ( JSON_INDENT );
fsOut.close ( );
}
......@@ -85,4 +85,17 @@ set_property (TARGET SplineTest PROPERTY FOLDER "Tests/ITABase")
install (TARGETS SplineTest RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
# ######################################################################################################################
if (ITA_BASE_WITH_JSON_SUPPORT)
add_executable (ITABaseUtilsJSONTest ITABaseUtilsJSONTest.cpp)
target_link_libraries (ITABaseUtilsJSONTest PUBLIC ITABase::ITABase)
set_property (TARGET ITABaseUtilsJSONTest PROPERTY FOLDER "Tests/ITABase")
install (TARGETS ITABaseUtilsJSONTest RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
# ######################################################################################################################
add_subdirectory (VistaTests)
#include <ITABase/UtilsJSON.h>
#include<ITAThirdOctaveMagnitudeSpectrum.h>
using namespace std;
int main( int nArgsIn, char** pccArgs )
{
ITABase::CThirdOctaveGainMagnitudeSpectrum oSpectrum;
oSpectrum.SetName ( "test_spectrum" );
for ( int idx = 0; idx < oSpectrum.GetNumBands ( ); idx++ )
oSpectrum.SetMagnitude ( idx, idx );
ITABase::Utils::JSON::Export ( &oSpectrum, "test_spectrum.json" );
ITABase::CStatistics statsTime ( "test_stats_time", "s" );
statsTime.dMax = 1;
statsTime.dMean = 1;
statsTime.dMin = 1;
statsTime.dStdDev = 1;
ITABase::CStatistics statsPressure ( "test_stats_pressure", "Pa" );
statsPressure.dMax = 2;
statsPressure.dMean = 2;
statsPressure.dMin = 2;
statsPressure.dStdDev = 2;
std::vector<ITABase::CStatistics> statsVector { statsTime, statsPressure };
ITABase::Utils::JSON::Export ( statsTime, "test_single_stats.json" );
ITABase::Utils::JSON::Export ( statsVector, "test_multiple_stats.json" );
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment