Skip to content

Commit 8be39c7

Browse files
ax3lfranzpoeschel
authored andcommitted
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 ac6cf2f commit 8be39c7

3 files changed

Lines changed: 113 additions & 30 deletions

File tree

include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp

Lines changed: 31 additions & 8 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

100108
#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 = default;
@@ -172,6 +185,8 @@ class ADIOS2IOHandlerImpl
172185

173186
private:
174187
adios2::ADIOS m_ADIOS;
188+
189+
# if openPMD_HAVE_JSON
175190
nlohmann::json m_config;
176191
static nlohmann::json nullvalue;
177192

@@ -198,7 +213,7 @@ class ADIOS2IOHandlerImpl
198213
{
199214
return config< Key >( std::forward< Key >( key ), m_config );
200215
}
201-
216+
# endif // openPMD_HAVE_JSON
202217
/*
203218
* We need to give names to IO objects. These names are irrelevant
204219
* within this application, since:
@@ -690,13 +705,21 @@ friend class ADIOS2IOHandlerImpl;
690705
ADIOS2IOHandler(
691706
std::string path,
692707
AccessType,
693-
MPI_Comm,
694-
nlohmann::json options );
708+
MPI_Comm
709+
# if openPMD_HAVE_JSON
710+
, nlohmann::json options
711+
# endif
712+
);
695713

696714
#endif
697715

698-
ADIOS2IOHandler( std::string path, AccessType,
699-
nlohmann::json options);
716+
ADIOS2IOHandler(
717+
std::string path,
718+
AccessType
719+
#if openPMD_HAVE_JSON
720+
, nlohmann::json options
721+
#endif
722+
);
700723

701724
std::future< void > flush( ) override;
702725
}; // 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
std::future< void > ADIOS2IOHandlerImpl::flush( )
97108
{
@@ -531,7 +542,9 @@ adios2::Mode ADIOS2IOHandlerImpl::adios2Accesstype( )
531542
}
532543
}
533544

545+
# if openPMD_HAVE_JSON
534546
nlohmann::json ADIOS2IOHandlerImpl::nullvalue = nlohmann::json();
547+
# endif // openPMD_HAVE_JSON
535548

536549
std::string ADIOS2IOHandlerImpl::filePositionToString(
537550
std::shared_ptr< ADIOS2FilePosition > filepos )
@@ -1150,7 +1163,9 @@ namespace detail
11501163
void
11511164
BufferedActions::configure_IO( ADIOS2IOHandlerImpl & impl )
11521165
{
1166+
(void)impl;
11531167
std::set< std::string > alreadyConfigured;
1168+
#if openPMD_HAVE_JSON
11541169
auto & engine = impl.config( detail::str_engine );
11551170
if( !engine.is_null() )
11561171
{
@@ -1165,7 +1180,7 @@ namespace detail
11651180
}
11661181
}
11671182
}
1168-
1183+
#endif // openPMD_HAVE_JSON
11691184
auto notYetConfigured =
11701185
[&alreadyConfigured]( std::string const & param ) {
11711186
auto it = alreadyConfigured.find( param );
@@ -1366,11 +1381,18 @@ namespace detail
13661381
ADIOS2IOHandler::ADIOS2IOHandler(
13671382
std::string path,
13681383
openPMD::AccessType at,
1369-
MPI_Comm comm,
1370-
nlohmann::json options )
1384+
MPI_Comm comm
1385+
# if openPMD_HAVE_JSON
1386+
, nlohmann::json options
1387+
# endif
1388+
)
13711389
: AbstractIOHandler( std::move( path ), at, comm )
1372-
, m_impl{ this, comm, std::move( options )
1373-
1390+
, m_impl{
1391+
this,
1392+
comm
1393+
# if openPMD_HAVE_JSON
1394+
, std::move( options )
1395+
# endif
13741396
}
13751397
{
13761398
}
@@ -1379,10 +1401,18 @@ ADIOS2IOHandler::ADIOS2IOHandler(
13791401

13801402
ADIOS2IOHandler::ADIOS2IOHandler(
13811403
std::string path,
1382-
AccessType at,
1383-
nlohmann::json options )
1404+
AccessType at
1405+
# if openPMD_HAVE_JSON
1406+
, nlohmann::json options
1407+
# endif
1408+
)
13841409
: AbstractIOHandler( std::move( path ), at )
1385-
, m_impl{ this, std::move( options ) }
1410+
, m_impl{
1411+
this
1412+
# if openPMD_HAVE_JSON
1413+
, std::move( options )
1414+
# endif
1415+
}
13861416
{
13871417
}
13881418

@@ -1397,18 +1427,24 @@ std::future< void > ADIOS2IOHandler::flush( )
13971427
ADIOS2IOHandler::ADIOS2IOHandler(
13981428
std::string path,
13991429
AccessType at,
1400-
MPI_Comm comm,
1401-
nlohmann::json )
1430+
MPI_Comm comm
1431+
# if openPMD_HAVE_JSON
1432+
, nlohmann::json
1433+
# endif
1434+
)
14021435
: AbstractIOHandler( std::move( path ), at, comm )
14031436
{
14041437
}
14051438

1406-
# endif
1439+
#endif // openPMD_HAVE_MPI
14071440

14081441
ADIOS2IOHandler::ADIOS2IOHandler(
14091442
std::string path,
1410-
AccessType at,
1411-
nlohmann::json )
1443+
AccessType at
1444+
#if openPMD_HAVE_JSON
1445+
, nlohmann::json
1446+
#endif
1447+
)
14121448
: AbstractIOHandler( std::move( path ), at )
14131449
{
14141450
}

src/IO/AbstractIOHandlerHelper.cpp

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

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

3234
namespace openPMD
3335
{
@@ -40,7 +42,12 @@ namespace openPMD
4042
MPI_Comm comm,
4143
std::string const & options )
4244
{
45+
# if openPMD_HAVE_JSON
4346
nlohmann::json optionsJson = nlohmann::json::parse( options );
47+
# else
48+
if( options.size() > 0u && options != "{}" )
49+
throw std::runtime_error("openPMD-api built without JSON support which is required for runtime options!");
50+
# endif
4451
switch( format )
4552
{
4653
case Format::HDF5:
@@ -53,7 +60,14 @@ namespace openPMD
5360
return std::make_shared< DummyIOHandler >(path, accessTypeBackend);
5461
# endif
5562
case Format::ADIOS2:
56-
return std::make_shared<ADIOS2IOHandler>(path, accessTypeBackend, comm, std::move(optionsJson));
63+
return std::make_shared<ADIOS2IOHandler>(
64+
path,
65+
accessTypeBackend,
66+
comm
67+
# if openPMD_HAVE_JSON
68+
, std::move(optionsJson)
69+
# endif
70+
);
5771
default:
5872
throw std::runtime_error("Unknown file format! Did you specify a file ending?" );
5973
return std::make_shared< DummyIOHandler >(path, accessTypeBackend);
@@ -67,7 +81,12 @@ namespace openPMD
6781
Format format,
6882
std::string const & options )
6983
{
84+
#if openPMD_HAVE_JSON
7085
nlohmann::json optionsJson = nlohmann::json::parse( options );
86+
#else
87+
if( options.size() > 0u && options != "{}" )
88+
throw std::runtime_error("openPMD-api built without JSON support which is required for runtime options!");
89+
#endif
7190
switch( format )
7291
{
7392
case Format::HDF5:
@@ -82,8 +101,13 @@ namespace openPMD
82101
#if openPMD_HAVE_ADIOS2
83102
case Format::ADIOS2:
84103
return std::make_shared< ADIOS2IOHandler >(
85-
path, accessType, std::move( optionsJson ) );
86-
#endif
104+
path,
105+
accessType
106+
# if openPMD_HAVE_JSON
107+
, std::move( optionsJson )
108+
# endif // openPMD_HAVE_JSON
109+
);
110+
#endif // openPMD_HAVE_ADIOS2
87111
case Format::JSON:
88112
return std::make_shared< JSONIOHandler >(path, accessType);
89113
default:

0 commit comments

Comments
 (0)