forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonUtils.json.hh
88 lines (78 loc) · 3.4 KB
/
JsonUtils.json.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/io/detail/JsonUtils.json.hh
//---------------------------------------------------------------------------//
#pragma once
#include <nlohmann/json.hpp>
//---------------------------------------------------------------------------//
// MACROS
//---------------------------------------------------------------------------//
/*!
* Load a required field into a struct.
*/
#define CELER_JSON_LOAD_REQUIRED(OBJ, STRUCT, NAME) \
OBJ.at(#NAME).get_to(STRUCT.NAME)
/*!
* Load an optional field.
*
* If the field is missing or null, it is omitted.
*/
#define CELER_JSON_LOAD_OPTION(OBJ, STRUCT, NAME) \
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.
*/
#define CELER_JSON_LOAD_DEPRECATED(OBJ, STRUCT, OLD, NEW) \
do \
{ \
if (auto iter = OBJ.find(#OLD); iter != OBJ.end()) \
{ \
::celeritas::warn_deprecated_json_option(#OLD, #NEW); \
iter->get_to(STRUCT.NEW); \
} \
} while (0)
/*!
* Save a field to a JSON object.
*/
#define CELER_JSON_SAVE(OBJ, STRUCT, NAME) OBJ[#NAME] = STRUCT.NAME
/*!
* Save a field if the condition is met.
*
* If not met, a "null" placeholder is saved.
*/
#define CELER_JSON_SAVE_WHEN(OBJ, STRUCT, NAME, COND) \
do \
{ \
if ((COND)) \
{ \
CELER_JSON_SAVE(OBJ, STRUCT, NAME); \
} \
else \
{ \
OBJ[#NAME] = nullptr; \
} \
} while (0)
//---------------------------------------------------------------------------//
namespace celeritas
{
//---------------------------------------------------------------------------//
// Print a warning about a deprecated input option
void warn_deprecated_json_option(char const* old_name, char const* new_name);
// Save a format and version marker
void save_format(nlohmann::json& j, std::string const& format);
// Load and check for a format and compatible version marker
void check_format(nlohmann::json const& j, std::string const& format);
//---------------------------------------------------------------------------//
} // namespace celeritas