Skip to content

Commit be499e3

Browse files
committed
More lenient string conversion
1 parent d5fee20 commit be499e3

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

include/openPMD/auxiliary/JSON.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,9 @@ namespace json
181181
#endif
182182

183183
nlohmann::json & lowerCase( nlohmann::json & );
184+
185+
std::string asStringDynamic( nlohmann::json const & );
186+
187+
std::string asLowerCaseStringDynamic( nlohmann::json const & );
184188
} // namespace json
185189
} // namespace openPMD

src/IO/ADIOS/ADIOS2IOHandler.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,8 @@ ADIOS2IOHandlerImpl::init( nlohmann::json cfg )
140140
if( !engineTypeConfig.is_null() )
141141
{
142142
// convert to string
143-
m_engineType = engineTypeConfig;
144-
std::transform(
145-
m_engineType.begin(),
146-
m_engineType.end(),
147-
m_engineType.begin(),
148-
[]( unsigned char c ) { return std::tolower( c ); } );
143+
m_engineType =
144+
json::asLowerCaseStringDynamic( engineTypeConfig );
149145
}
150146
}
151147
auto operators = getOperators();
@@ -189,7 +185,7 @@ ADIOS2IOHandlerImpl::getOperators( json::TracingJSON cfg )
189185
++paramIterator )
190186
{
191187
adiosParams[ paramIterator.key() ] =
192-
paramIterator.value().get< std::string >();
188+
json::asStringDynamic( paramIterator.value() );
193189
}
194190
}
195191
auxiliary::Option< adios2::Operator > adiosOperator =
@@ -2318,7 +2314,8 @@ namespace detail
23182314
for( auto it = params.json().begin(); it != params.json().end();
23192315
it++ )
23202316
{
2321-
m_IO.SetParameter( it.key(), it.value() );
2317+
m_IO.SetParameter(
2318+
it.key(), json::asStringDynamic( it.value() ) );
23222319
alreadyConfigured.emplace( it.key() );
23232320
}
23242321
}

src/IO/HDF5/HDF5IOHandler.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ HDF5IOHandlerImpl::HDF5IOHandlerImpl(
9797
auto datasetConfig = m_config[ "dataset" ];
9898
if( datasetConfig.json().contains( "chunks" ) )
9999
{
100-
m_chunks = datasetConfig[ "chunks" ].json().get< std::string >();
100+
m_chunks = json::asLowerCaseStringDynamic(
101+
datasetConfig[ "chunks" ].json() );
101102
}
102103
}
103104
if( m_chunks != "auto" && m_chunks != "none" )

src/auxiliary/JSON.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,31 @@ namespace json
231231
}
232232
return json;
233233
}
234+
235+
std::string asStringDynamic( nlohmann::json const & value )
236+
{
237+
if( value.is_string() )
238+
{
239+
return value.get< std::string >();
240+
}
241+
else if( value.is_number_integer() )
242+
{
243+
return std::to_string( value.get< long long >() );
244+
}
245+
else if( value.is_number_float() )
246+
{
247+
return std::to_string( value.get< long double >() );
248+
}
249+
else if( value.is_boolean() )
250+
{
251+
return value.get< bool >() ? "1" : "0";
252+
}
253+
throw std::runtime_error( "JSON: Cannot convert value to string." );
254+
}
255+
256+
std::string asLowerCaseStringDynamic( nlohmann::json const & value )
257+
{
258+
return auxiliary::lowerCase( asStringDynamic( value ) );
259+
}
234260
} // namespace json
235261
} // namespace openPMD

test/SerialIOTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3134,7 +3134,7 @@ TEST_CASE( "serial_adios2_json_config", "[serial][adios2]" )
31343134
"type": "bp4",
31353135
"unused": "parameter",
31363136
"parameters": {
3137-
"BufferGrowthFactor": "2.0",
3137+
"BufferGrowthFactor": 2.0,
31383138
"Profile": "On"
31393139
}
31403140
},
@@ -3144,7 +3144,7 @@ TEST_CASE( "serial_adios2_json_config", "[serial][adios2]" )
31443144
{
31453145
"type": "blosc",
31463146
"parameters": {
3147-
"clevel": "1",
3147+
"clevel": 1,
31483148
"doshuffle": "BLOSC_BITSHUFFLE"
31493149
}
31503150
}

0 commit comments

Comments
 (0)