Skip to content

Commit da5ab03

Browse files
committed
Gracefully handle missing JSON
JSON is still optional, so we should gracefully throw if a user tries to pass options that we cannot parse without it. https://travis-ci.org/openPMD/openPMD-api/builds/599388433
1 parent 2765f82 commit da5ab03

File tree

3 files changed

+116
-33
lines changed

3 files changed

+116
-33
lines changed

include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
# include <mpi.h>
3939
#endif
4040

41-
#include <nlohmann/json.hpp>
41+
#if openPMD_HAVE_JSON
42+
# include <nlohmann/json.hpp>
43+
#endif
4244

4345
#include <array>
4446
#include <future>
@@ -93,13 +95,24 @@ class ADIOS2IOHandlerImpl
9395

9496
#if openPMD_HAVE_MPI
9597

96-
ADIOS2IOHandlerImpl( AbstractIOHandler *, MPI_Comm, nlohmann::json config );
98+
ADIOS2IOHandlerImpl(
99+
AbstractIOHandler *,
100+
MPI_Comm
101+
# if openPMD_HAVE_JSON
102+
, nlohmann::json config
103+
# endif
104+
);
97105

98106
MPI_Comm m_comm;
99107

100-
# endif // openPMD_HAVE_MPI
108+
#endif // openPMD_HAVE_MPI
101109

102-
explicit ADIOS2IOHandlerImpl( AbstractIOHandler *, nlohmann::json config );
110+
explicit ADIOS2IOHandlerImpl(
111+
AbstractIOHandler *
112+
# if openPMD_HAVE_JSON
113+
, nlohmann::json config
114+
# endif
115+
);
103116

104117

105118
~ADIOS2IOHandlerImpl() override;
@@ -172,6 +185,8 @@ class ADIOS2IOHandlerImpl
172185

173186
private:
174187
adios2::ADIOS m_ADIOS;
188+
189+
# if openPMD_HAVE_JSON
175190
// data is held by m_handler
176191
nlohmann::json m_config;
177192
static nlohmann::json nullvalue;
@@ -199,7 +214,7 @@ class ADIOS2IOHandlerImpl
199214
{
200215
return config< Key >( std::forward< Key >( key ), m_config );
201216
}
202-
217+
# endif // openPMD_HAVE_JSON
203218
/*
204219
* We need to give names to IO objects. These names are irrelevant
205220
* within this application, since:
@@ -639,13 +654,21 @@ friend class ADIOS2IOHandlerImpl;
639654
ADIOS2IOHandler(
640655
std::string path,
641656
AccessType,
642-
MPI_Comm,
643-
nlohmann::json options );
657+
MPI_Comm
658+
# if openPMD_HAVE_JSON
659+
, nlohmann::json options
660+
# endif
661+
);
644662

645663
#endif
646664

647-
ADIOS2IOHandler( std::string path, AccessType,
648-
nlohmann::json options);
665+
ADIOS2IOHandler(
666+
std::string path,
667+
AccessType
668+
#if openPMD_HAVE_JSON
669+
, nlohmann::json options
670+
#endif
671+
);
649672

650673
std::future< void > flush( ) override;
651674
}; // ADIOS2IOHandler

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,36 @@ namespace openPMD
6464

6565
ADIOS2IOHandlerImpl::ADIOS2IOHandlerImpl(
6666
AbstractIOHandler * handler,
67-
MPI_Comm communicator,
68-
nlohmann::json cfg )
67+
MPI_Comm communicator
68+
# if openPMD_HAVE_JSON
69+
, nlohmann::json cfg
70+
# endif // openPMD_HAVE_JSON
71+
)
6972
: AbstractIOHandlerImplCommon( handler )
7073
, m_comm{ communicator }
7174
, m_ADIOS{ communicator, ADIOS2_DEBUG_MODE }
7275
{
76+
# if openPMD_HAVE_JSON
7377
init( std::move( cfg ) );
78+
# endif // openPMD_HAVE_JSON
7479
}
7580

7681
# endif // openPMD_HAVE_MPI
7782

7883
ADIOS2IOHandlerImpl::ADIOS2IOHandlerImpl(
79-
AbstractIOHandler * handler,
80-
nlohmann::json cfg )
84+
AbstractIOHandler * handler
85+
# if openPMD_HAVE_JSON
86+
, nlohmann::json cfg
87+
# endif // openPMD_HAVE_JSON
88+
)
8189
: AbstractIOHandlerImplCommon( handler ), m_ADIOS{ ADIOS2_DEBUG_MODE }
8290
{
91+
# if openPMD_HAVE_JSON
8392
init( std::move( cfg ) );
93+
# endif // openPMD_HAVE_JSON
8494
}
8595

86-
96+
# if openPMD_HAVE_JSON
8797
void
8898
ADIOS2IOHandlerImpl::init( nlohmann::json cfg )
8999
{
@@ -92,6 +102,7 @@ ADIOS2IOHandlerImpl::init( nlohmann::json cfg )
92102
m_config = std::move( cfg[ "adios2" ] );
93103
}
94104
}
105+
# endif // openPMD_HAVE_JSON
95106

96107
ADIOS2IOHandlerImpl::~ADIOS2IOHandlerImpl( )
97108
{
@@ -536,7 +547,9 @@ adios2::Mode ADIOS2IOHandlerImpl::adios2Accesstype( )
536547
}
537548
}
538549

550+
# if openPMD_HAVE_JSON
539551
nlohmann::json ADIOS2IOHandlerImpl::nullvalue = nlohmann::json();
552+
# endif // openPMD_HAVE_JSON
540553

541554
std::string ADIOS2IOHandlerImpl::filePositionToString(
542555
std::shared_ptr< ADIOS2FilePosition > filepos )
@@ -1152,7 +1165,9 @@ namespace detail
11521165
void
11531166
BufferedActions::configure_IO( ADIOS2IOHandlerImpl & impl )
11541167
{
1168+
(void)impl;
11551169
std::set< std::string > alreadyConfigured;
1170+
#if openPMD_HAVE_JSON
11561171
auto & engine = impl.config( detail::str_engine );
11571172
if( !engine.is_null() )
11581173
{
@@ -1167,7 +1182,7 @@ namespace detail
11671182
}
11681183
}
11691184
}
1170-
1185+
#endif // openPMD_HAVE_JSON
11711186
auto notYetConfigured =
11721187
[&alreadyConfigured]( std::string const & param ) {
11731188
auto it = alreadyConfigured.find( param );
@@ -1278,11 +1293,18 @@ namespace detail
12781293
ADIOS2IOHandler::ADIOS2IOHandler(
12791294
std::string path,
12801295
openPMD::AccessType at,
1281-
MPI_Comm comm,
1282-
nlohmann::json options )
1296+
MPI_Comm comm
1297+
# if openPMD_HAVE_JSON
1298+
, nlohmann::json options
1299+
# endif
1300+
)
12831301
: AbstractIOHandler( std::move( path ), at, comm )
1284-
, m_impl{ this, comm, std::move( options )
1285-
1302+
, m_impl{
1303+
this,
1304+
comm
1305+
# if openPMD_HAVE_JSON
1306+
, std::move( options )
1307+
# endif
12861308
}
12871309
{
12881310
}
@@ -1291,10 +1313,18 @@ ADIOS2IOHandler::ADIOS2IOHandler(
12911313

12921314
ADIOS2IOHandler::ADIOS2IOHandler(
12931315
std::string path,
1294-
AccessType at,
1295-
nlohmann::json options )
1316+
AccessType at
1317+
# if openPMD_HAVE_JSON
1318+
, nlohmann::json options
1319+
# endif
1320+
)
12961321
: AbstractIOHandler( std::move( path ), at )
1297-
, m_impl{ this, std::move( options ) }
1322+
, m_impl{
1323+
this
1324+
# if openPMD_HAVE_JSON
1325+
, std::move( options )
1326+
# endif
1327+
}
12981328
{
12991329
}
13001330

@@ -1315,18 +1345,24 @@ std::future< void > ADIOS2IOHandler::flush( )
13151345
ADIOS2IOHandler::ADIOS2IOHandler(
13161346
std::string path,
13171347
AccessType at,
1318-
MPI_Comm comm,
1319-
nlohmann::json )
1348+
MPI_Comm comm
1349+
# if openPMD_HAVE_JSON
1350+
, nlohmann::json
1351+
# endif
1352+
)
13201353
: AbstractIOHandler( std::move( path ), at, comm )
13211354
{
13221355
}
13231356

1324-
# endif
1357+
#endif // openPMD_HAVE_MPI
13251358

13261359
ADIOS2IOHandler::ADIOS2IOHandler(
13271360
std::string path,
1328-
AccessType at,
1329-
nlohmann::json )
1361+
AccessType at
1362+
#if openPMD_HAVE_JSON
1363+
, nlohmann::json
1364+
#endif
1365+
)
13301366
: AbstractIOHandler( std::move( path ), at )
13311367
{
13321368
}

src/IO/AbstractIOHandlerHelper.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#include "openPMD/IO/HDF5/ParallelHDF5IOHandler.hpp"
2929
#include "openPMD/IO/JSON/JSONIOHandler.hpp"
3030

31-
#include <nlohmann/json.hpp>
31+
#if openPMD_HAVE_JSON
32+
# include <nlohmann/json.hpp>
33+
#endif
3234

3335

3436
namespace openPMD
@@ -42,7 +44,12 @@ namespace openPMD
4244
MPI_Comm comm,
4345
std::string const & options )
4446
{
47+
# if openPMD_HAVE_JSON
4548
nlohmann::json optionsJson = nlohmann::json::parse( options );
49+
# else
50+
if( options.size() > 0u && options != "{}" )
51+
throw std::runtime_error("openPMD-api built without JSON support which is required for runtime options!");
52+
# endif
4653
switch( format )
4754
{
4855
case Format::HDF5:
@@ -55,7 +62,14 @@ namespace openPMD
5562
return std::make_shared< DummyIOHandler >(path, accessTypeBackend);
5663
# endif
5764
case Format::ADIOS2:
58-
return std::make_shared<ADIOS2IOHandler>(path, accessTypeBackend, comm, std::move(optionsJson));
65+
return std::make_shared<ADIOS2IOHandler>(
66+
path,
67+
accessTypeBackend,
68+
comm
69+
# if openPMD_HAVE_JSON
70+
, std::move(optionsJson)
71+
# endif
72+
);
5973
default:
6074
return std::make_shared< DummyIOHandler >(path, accessTypeBackend);
6175
}
@@ -68,24 +82,34 @@ namespace openPMD
6882
Format format,
6983
std::string const & options )
7084
{
85+
#if openPMD_HAVE_JSON
7186
nlohmann::json optionsJson = nlohmann::json::parse( options );
87+
#else
88+
if( options.size() > 0u && options != "{}" )
89+
throw std::runtime_error("openPMD-api built without JSON support which is required for runtime options!");
90+
#endif
7291
switch( format )
7392
{
7493
case Format::HDF5:
7594
return std::make_shared< HDF5IOHandler >( path, accessType );
7695
case Format::ADIOS1:
77-
# if openPMD_HAVE_ADIOS1
96+
#if openPMD_HAVE_ADIOS1
7897
return std::make_shared< ADIOS1IOHandler >(path, accessType);
7998
#else
8099
throw std::runtime_error(
81100
"openPMD-api built without ADIOS1 support" );
82101
return std::make_shared< DummyIOHandler >( path, accessType );
83-
#endif
102+
#endif // openPMD_HAVE_ADIOS1
84103
#if openPMD_HAVE_ADIOS2
85104
case Format::ADIOS2:
86105
return std::make_shared< ADIOS2IOHandler >(
87-
path, accessType, std::move( optionsJson ) );
88-
#endif
106+
path,
107+
accessType
108+
# if openPMD_HAVE_JSON
109+
, std::move( optionsJson )
110+
# endif // openPMD_HAVE_JSON
111+
);
112+
#endif // openPMD_HAVE_ADIOS2
89113
case Format::JSON:
90114
return std::make_shared< JSONIOHandler >(path, accessType);
91115
default:

0 commit comments

Comments
 (0)