Celeritas  0.5.0-56+6b053cd
Macros | Functions
JsonUtils.json.hh File Reference
#include <string_view>
#include <nlohmann/json.hpp>
This graph shows which files directly or indirectly include this file:

Macros

#define CELER_JSON_LOAD_REQUIRED(OBJ, STRUCT, NAME)    OBJ.at(#NAME).get_to(STRUCT.NAME)
 Load a required field into a struct.
 
#define CELER_JSON_LOAD_OPTION(OBJ, STRUCT, NAME)
 Load an optional field. More...
 
#define CELER_JSON_LOAD_DEPRECATED(OBJ, STRUCT, OLD, NEW)
 Load an optional field. More...
 
#define CELER_JSON_SAVE(OBJ, STRUCT, NAME)   OBJ[#NAME] = STRUCT.NAME
 Save a field to a JSON object.
 
#define CELER_JSON_SAVE_WHEN(OBJ, STRUCT, NAME, COND)
 Save a field if the condition is met. More...
 
#define CELER_JSON_PAIR(STRUCT, NAME)   {#NAME, STRUCT.NAME}
 Construct a key/value pair for a JSON object.
 

Functions

void celeritas::warn_deprecated_json_option (char const *old_name, char const *new_name)
 Print a warning about a deprecated input option.
 
void celeritas::save_format (nlohmann::json &j, std::string const &format)
 Save a format and version marker. More...
 
void celeritas::save_units (nlohmann::json &j)
 Save units for provenance/reproducibility.
 
void celeritas::check_format (nlohmann::json const &j, std::string_view format)
 Load and check for a format and compatible version marker.
 
void celeritas::check_units (nlohmann::json const &j, std::string_view format)
 Check units for consistency.
 

Macro Definition Documentation

◆ CELER_JSON_LOAD_DEPRECATED

#define CELER_JSON_LOAD_DEPRECATED (   OBJ,
  STRUCT,
  OLD,
  NEW 
)
Value:
do \
{ \
if (auto iter = OBJ.find(#OLD); iter != OBJ.end()) \
{ \
::celeritas::warn_deprecated_json_option(#OLD, #NEW); \
iter->get_to(STRUCT.NEW); \
} \
} while (0)

Load an optional field.

If the field is missing or null, it is omitted.

◆ CELER_JSON_LOAD_OPTION

#define CELER_JSON_LOAD_OPTION (   OBJ,
  STRUCT,
  NAME 
)
Value:
do \
{ \
if (auto iter = OBJ.find(#NAME); \
iter != OBJ.end() && !iter->is_null()) \
{ \
iter->get_to(STRUCT.NAME); \
} \
} while (0)

Load an optional field.

If the field is missing or null, it is omitted.

◆ CELER_JSON_SAVE_WHEN

#define CELER_JSON_SAVE_WHEN (   OBJ,
  STRUCT,
  NAME,
  COND 
)
Value:
do \
{ \
if ((COND)) \
{ \
CELER_JSON_SAVE(OBJ, STRUCT, NAME); \
} \
else \
{ \
OBJ[#NAME] = nullptr; \
} \
} while (0)

Save a field if the condition is met.

If not met, a "null" placeholder is saved.

Function Documentation

◆ save_format()

void celeritas::save_format ( nlohmann::json &  j,
std::string const &  format 
)

Save a format and version marker.

This should be only used for JSON structs intended for input in addition to output. Format strings should be all lowercase with hyphens for consistency.