Adding export utils using JSON file i/o

parent 715db06b
......@@ -12,6 +12,7 @@ vista_use_package( samplerate )
vista_use_package( IPP QUIET )
vista_use_package( SimpleIni QUIET )
vista_use_package( SPLINE REQUIRED )
vista_find_package( libjson OPTIONAL QUIET )
if ( NOT WIN32 )
vista_use_package ( Threads REQUIRED )
......@@ -55,6 +56,11 @@ if( ITA_BASE_WITH_REGULAR_EXPRESSIONS )
vista_use_package( PCRE QUIET )
endif( )
if( NOT DEFINED ITA_BASE_WITH_JSON_SUPPORT )
set( ITA_BASE_WITH_JSON_SUPPORT ${LIBJSON_FOUND} CACHE BOOL "Build ITA Base with JSON support" )
endif( )
# includes
include_directories( "include" )
......@@ -133,6 +139,14 @@ set( ITABaseSources
)
if( ITA_BASE_WITH_JSON_SUPPORT )
vista_use_package( libjson REQUIRED )
add_definitions( -DWITH_JSON_SUPPORT )
list( APPEND ITABaseHeader "include/ITABase/UtilsJSON.h" )
list( APPEND ITABaseSources "src/ITABase/UtilsJSON.cpp" )
endif( )
# Win32 implementations
if( WIN32 )
list( APPEND ITABaseHeader "include/ITAHPT.h" "include/ITAMutex.h" "include/ITATimer.h" )
......
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2019
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef IW_ITA_BASE_UTILS_JSON
#define IW_ITA_BASE_UTILS_JSON
#include <ITABaseDefinitions.h>
#include <ITASpectrum.h>
#include <string>
namespace ITABase
{
namespace Utils
{
namespace JSON
{
//! Export a spectrum to JSON fromatted text file
/**
* @param[in] oSpectrum Spectrum
* @param[in] sTargetFilePath Target file path
*/
ITA_BASE_API void Export( const CITASpectrum* pSpectrum, const std::string& sTargetFilePath );
}
}
}
#endif // IW_ITA_BASE_UTILS_JSON
#include <ITABase/UtilsJSON.h>
#include <libjson.h>
#include <fstream>
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() ) );
JSONNode jnBins;
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 ] ) );
jnBins.push_back( jnBin );
}
std::ofstream fsOut( sTargetFilePath );
fsOut << jnRoot.write_formatted();
fsOut.close();
}
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