Commit 67d79140 authored by Pascal Palenda's avatar Pascal Palenda
Browse files

Move json utils to seperate file and add varaibles

parent a24f25fb
......@@ -70,7 +70,7 @@ int main ( )
std::ofstream myfile;
myfile.open ( "CompleteTest_Master_Test_Config.json" );
myfile << utils::DataTypeUtils::writeVistaPropertyListToJSON ( oConfig.store ( ) ).write_formatted ( );
myfile << utils::JSONConfigUtils::writeVistaPropertyListToJSON ( oConfig.store ( ) ).write_formatted ( );
myfile.close ( );
auto masterController = std::make_unique<CMasterSimulationController> ( oConfig );
......
......@@ -10,6 +10,7 @@ set( DirFiles
fft_utils.h
enumerate_utils.h
calculation_utils.h
json_config_utils.h
#_SourceFiles.cmake
)
......
......@@ -172,173 +172,6 @@ namespace ITA
return retVal;
}
static JSONNode writeVistaPropertyToJSON ( const VistaProperty& oProperty )
{
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_STRING )
{
std::string sValue;
VistaConversion::FromString ( oProperty.GetValue ( ), sValue );
return JSONNode ( oProperty.GetNameForNameable ( ), sValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_INT )
{
int iValue;
VistaConversion::FromString ( oProperty.GetValue ( ), iValue );
return JSONNode ( oProperty.GetNameForNameable ( ), iValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_BOOL )
{
bool bValue;
VistaConversion::FromString ( oProperty.GetValue ( ), bValue );
return JSONNode ( oProperty.GetNameForNameable ( ), bValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_DOUBLE )
{
double dValue;
VistaConversion::FromString ( oProperty.GetValue ( ), dValue );
return JSONNode ( oProperty.GetNameForNameable ( ), dValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_LIST )
{
std::vector<float> vfValue;
VistaConversion::FromString ( oProperty.GetValue ( ), vfValue );
JSONNode jnValues ( JSON_ARRAY );
jnValues.set_name ( oProperty.GetNameForNameable ( ) );
for ( const auto& val : vfValue )
{
jnValues.push_back ( JSONNode ( "", val ) );
}
return jnValues;
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_PROPERTYLIST )
{
JSONNode jnRoot;
jnRoot.set_name ( oProperty.GetNameForNameable ( ) );
for ( const auto& oProp : oProperty.GetPropertyListConstRef ( ) )
{
jnRoot.push_back ( writeVistaPropertyToJSON ( oProp.second ) );
}
return jnRoot;
}
else
{
ITA_EXCEPT_NOT_IMPLEMENTED;
}
}
static VistaProperty readVistaPropertyFromJSON ( const JSONNode& oJsonNode )
{
VistaProperty oProp;
if ( oJsonNode.type ( ) == JSON_BOOL )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_BOOL );
std::string sValue;
VistaConversion::ToString ( oJsonNode.as_bool ( ), sValue );
oProp.SetValue ( sValue );
}
else if ( oJsonNode.type ( ) == JSON_STRING )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_STRING );
oProp.SetValue ( oJsonNode.as_string() );
}
else if ( oJsonNode.type ( ) == JSON_NUMBER )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
std::string sValue;
const auto value = oJsonNode.as_float ( );
if ( value != static_cast< int >( value ) ) // double
{
oProp.SetPropertyType ( VistaProperty::PROPT_DOUBLE );
VistaConversion::ToString ( value , sValue );
}
else // int
{
oProp.SetPropertyType ( VistaProperty::PROPT_INT );
VistaConversion::ToString ( static_cast< int >( value ), sValue );
}
oProp.SetValue ( sValue );
}
else if ( oJsonNode.type ( ) == JSON_ARRAY )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_LIST );
std::vector<float> values;
for ( const auto& val : oJsonNode )
{
values.push_back ( val.as_float ( ) );
}
std::string sValue;
VistaConversion::ToString ( values, sValue );
oProp.SetValue ( sValue );
}
else if ( oJsonNode.type ( ) == JSON_NODE )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_PROPERTYLIST );
VistaPropertyList oPropertyList;
for ( const auto& subNode : oJsonNode )
{
oPropertyList.SetProperty ( readVistaPropertyFromJSON ( subNode ) );
}
oProp.SetPropertyListValue ( oPropertyList );
}
else
ITA_EXCEPT_NOT_IMPLEMENTED;
return oProp;
}
static JSONNode writeVistaPropertyListToJSON ( const VistaPropertyList& oProperties )
{
JSONNode jnRoot;
for ( const auto& oProp : oProperties )
{
jnRoot.push_back ( writeVistaPropertyToJSON ( oProp.second ) );
}
return jnRoot;
}
static VistaPropertyList readVistaPropertyListFromJSON ( const JSONNode& oJsonNode )
{
return readVistaPropertyFromJSON ( oJsonNode ).GetPropertyListConstRef();
}
static VistaPropertyList loadJSONConfig( const std::string& sFileName )
{
std::ifstream fConfigFile ( sFileName );
const std::string sContent ( ( std::istreambuf_iterator<char> ( fConfigFile ) ),
( std::istreambuf_iterator<char> ( ) ) );
fConfigFile.close ( );
const auto jnRoot = libjson::parse ( sContent );
return readVistaPropertyListFromJSON ( jnRoot );
}
template<class T>
static std::array<VistaType::byte, sizeof(T)> convertToByteArray( T value )
{
......
#ifndef INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS_JSON_CONFIG_UTILS
#define INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS_JSON_CONFIG_UTILS
// std includes
#include <valarray>
#include <complex>
#include <fstream>
#include <array>
// API includes
#include <ITA/simulation_scheduler/definitions.h>
#include <VistaAspects/VistaPropertyList.h>
#include <VistaTools/VistaBase64.h>
#include <ITAException.h>
#include <libjson.h>
namespace ITA
{
namespace simulation_scheduler
{
namespace utils
{
struct ITA_SIMULATION_SCHEDULER_API JSONConfigUtils
{
static JSONNode writeVistaPropertyToJSON ( const VistaProperty& oProperty )
{
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_STRING )
{
std::string sValue;
VistaConversion::FromString ( oProperty.GetValue ( ), sValue );
return JSONNode ( oProperty.GetNameForNameable ( ), sValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_INT )
{
int iValue;
VistaConversion::FromString ( oProperty.GetValue ( ), iValue );
return JSONNode ( oProperty.GetNameForNameable ( ), iValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_BOOL )
{
bool bValue;
VistaConversion::FromString ( oProperty.GetValue ( ), bValue );
return JSONNode ( oProperty.GetNameForNameable ( ), bValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_DOUBLE )
{
double dValue;
VistaConversion::FromString ( oProperty.GetValue ( ), dValue );
return JSONNode ( oProperty.GetNameForNameable ( ), dValue );
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_LIST )
{
std::vector<float> vfValue;
VistaConversion::FromString ( oProperty.GetValue ( ), vfValue );
JSONNode jnValues ( JSON_ARRAY );
jnValues.set_name ( oProperty.GetNameForNameable ( ) );
for ( const auto& val : vfValue )
{
jnValues.push_back ( JSONNode ( "", val ) );
}
return jnValues;
}
if ( oProperty.GetPropertyType ( ) == VistaProperty::PROPT_PROPERTYLIST )
{
JSONNode jnRoot;
jnRoot.set_name ( oProperty.GetNameForNameable ( ) );
for ( const auto& oProp : oProperty.GetPropertyListConstRef ( ) )
{
jnRoot.push_back ( writeVistaPropertyToJSON ( oProp.second ) );
}
return jnRoot;
}
else
{
ITA_EXCEPT_NOT_IMPLEMENTED;
}
}
static VistaProperty readVistaPropertyFromJSON ( const JSONNode& oJsonNode )
{
VistaProperty oProp;
if ( oJsonNode.type ( ) == JSON_BOOL )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_BOOL );
std::string sValue;
VistaConversion::ToString ( oJsonNode.as_bool ( ), sValue );
oProp.SetValue ( sValue );
}
else if ( oJsonNode.type ( ) == JSON_STRING )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_STRING );
oProp.SetValue ( oJsonNode.as_string ( ) );
}
else if ( oJsonNode.type ( ) == JSON_NUMBER )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
std::string sValue;
const auto value = oJsonNode.as_float ( );
if ( value != static_cast< int >( value ) ) // double
{
oProp.SetPropertyType ( VistaProperty::PROPT_DOUBLE );
VistaConversion::ToString ( value, sValue );
}
else // int
{
oProp.SetPropertyType ( VistaProperty::PROPT_INT );
VistaConversion::ToString ( static_cast< int >( value ), sValue );
}
oProp.SetValue ( sValue );
}
else if ( oJsonNode.type ( ) == JSON_ARRAY )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_LIST );
std::vector<float> values;
for ( const auto& val : oJsonNode )
{
values.push_back ( val.as_float ( ) );
}
std::string sValue;
VistaConversion::ToString ( values, sValue );
oProp.SetValue ( sValue );
}
else if ( oJsonNode.type ( ) == JSON_NODE )
{
oProp.SetNameForNameable ( oJsonNode.name ( ) );
oProp.SetPropertyType ( VistaProperty::PROPT_PROPERTYLIST );
VistaPropertyList oPropertyList;
for ( const auto& subNode : oJsonNode )
{
oPropertyList.SetProperty ( readVistaPropertyFromJSON ( subNode ) );
}
oProp.SetPropertyListValue ( oPropertyList );
}
else
ITA_EXCEPT_NOT_IMPLEMENTED;
return oProp;
}
static JSONNode writeVistaPropertyListToJSON ( const VistaPropertyList& oProperties )
{
JSONNode jnRoot;
for ( const auto& oProp : oProperties )
{
jnRoot.push_back ( writeVistaPropertyToJSON ( oProp.second ) );
}
return jnRoot;
}
static VistaPropertyList readVistaPropertyListFromJSON ( const JSONNode& oJsonNode )
{
return readVistaPropertyFromJSON ( oJsonNode ).GetPropertyListConstRef ( );
}
static VistaPropertyList loadJSONConfig ( const std::string& sFileName, const std::string& sVariableSectionName = "Variables" )
{
std::ifstream fConfigFile ( sFileName );
const std::string sContent ( ( std::istreambuf_iterator<char> ( fConfigFile ) ),
( std::istreambuf_iterator<char> ( ) ) );
fConfigFile.close ( );
auto jnRoot = libjson::parse ( sContent );
replaceVariables ( jnRoot, sVariableSectionName );
return readVistaPropertyListFromJSON ( jnRoot );
}
static void replaceVariables ( JSONNode& jnRoot, const std::string& sVariableSectionName = "Variables" )
{
const auto jnVariableIterator = jnRoot.find ( sVariableSectionName );
if ( jnVariableIterator != jnRoot.end ( ) )
{
std::map<std::string, JSONNode> mVariables;
for ( const auto jnNode : *jnVariableIterator )
{
mVariables [jnNode.name ( )] = jnNode;
}
replaceVariablesHelper ( jnRoot, mVariables );
jnRoot.erase ( jnVariableIterator );
}
}
private:
static void replaceVariablesHelper ( JSONNode& jnRoot, const std::map<std::string, JSONNode>& mVariables )
{
for ( auto& jnNode : jnRoot )
{
if ( jnNode.type() == JSON_NODE )
{
replaceVariablesHelper ( jnNode, mVariables );
}
if ( jnNode.type() == JSON_STRING )
{
auto sString = jnNode.as_string ( );
const auto nStart = sString.find ( "${" );
if ( nStart == std::string::npos )
continue;
const auto nEnd = sString.find ('}', nStart );
if ( nEnd == std::string::npos )
continue;
std::string sVariableName = sString.substr ( nStart + 2, nEnd - nStart - 2 );
auto replacementIterator = mVariables.find ( sVariableName );
if ( replacementIterator != mVariables.end() )
{
auto sName = jnNode.name ( );
jnNode = replacementIterator->second;
jnNode.set_name ( sName );
}
}
}
}
};
} // namespace statistics
} // namespace simulation_scheduler
} // namespace ITA
#endif // INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS_JSON_CONFIG_UTILS
\ No newline at end of file
......@@ -6,5 +6,6 @@
#include "statistics_utils.h"
#include "enumerate_utils.h"
#include "calculation_utils.h"
#include "json_config_utils.h"
#endif // INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_UTILS_UTILS
\ No newline at end of file
......@@ -72,7 +72,7 @@ struct CompleteTest : public testing::Test, public ITATimerEventHandler
ofstream myfile;
myfile.open ( "CompleteTest_Master_Test_Config.json" );
myfile << utils::DataTypeUtils::writeVistaPropertyListToJSON ( oConfig.store ( ) ).write_formatted ( );
myfile << utils::JSONConfigUtils::writeVistaPropertyListToJSON ( oConfig.store ( ) ).write_formatted ( );
myfile.close ( );
masterController = std::make_unique<CMasterSimulationController> ( oConfig );
......
......@@ -606,11 +606,11 @@ TEST( DataTypeUtils, VistaPropertyListJSONParser )
oPropSub.SetValue ( "double", 3.14 );
oProp.SetValue ( "PropList", oPropSub );
const auto jnProps = DataTypeUtils::writeVistaPropertyListToJSON ( oProp );
const auto jnProps = JSONConfigUtils::writeVistaPropertyListToJSON ( oProp );
const auto res = libjson::parse ( jnProps.write_formatted ( ) );
const auto resProp = DataTypeUtils::readVistaPropertyListFromJSON ( res );
const auto resProp = JSONConfigUtils::readVistaPropertyListFromJSON ( res );
EXPECT_EQ ( oProp, resProp );
}
......@@ -676,3 +676,41 @@ TEST ( DataTypeUtils, convertBase64 )
EXPECT_EQ ( vecOfRandomIntegers, iRetVal );
}
TEST (JSONConfigUtils, replaceVariables)
{
JSONNode jnRoot;
JSONNode jnVariables;
jnVariables.set_name ( "WeirdName" );
jnVariables.push_back ( JSONNode ( "VariableName", 5 ) );
JSONNode jnData;
jnData.set_name ( "Data" );
jnData.push_back ( JSONNode ( "One", "One" ) );
jnVariables.push_back ( jnData );
JSONNode jnConf;
jnConf.set_name ( "Conf" );
jnConf.push_back ( JSONNode ( "Some Name", "${Data}" ) );
jnConf.push_back ( JSONNode ( "Some Var Name", "${VariableName}" ) );
jnRoot.push_back ( jnVariables );
jnRoot.push_back ( jnConf );
JSONConfigUtils::replaceVariables ( jnRoot, "WeirdName" );
JSONNode jnResultRoot;
JSONNode jnResultConf;
jnResultConf.set_name ( "Conf" );
JSONNode jnResultData;
jnResultData.set_name ( "Some Name" );
jnResultData.push_back ( JSONNode ( "One", "One" ) );
jnResultConf.push_back ( jnResultData );
jnResultConf.push_back ( JSONNode ( "Some Var Name", 5 ) );
jnResultRoot.push_back ( jnResultConf );
EXPECT_EQ ( jnRoot, jnResultRoot );
}
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