Skip to content

Commit 1490a37

Browse files
Griger5slayoo
andauthored
Replace nlohmann::json with nlohmann::ordered_json to maintain order in input/output. Bumps nanobind_json submodule. Closes #213 (#461)
Co-authored-by: Sylwester Arabas <sylwester.arabas@agh.edu.pl>
1 parent 6322c6d commit 1490a37

19 files changed

+62
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
examples/tmp*.pdf
22
examples/tmp*.svg
33
examples/tmp*.gif
4+
**/__pycache__/

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@
5656
shallow = true
5757
[submodule "nanobind_json"]
5858
path = gitmodules/nanobind_json
59-
url = https://github.com/Griger5/nanobind_json
59+
url = https://github.com/Griger5/nanobind_json.git
6060
shallow = true

src/aero_data.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct AeroData {
4848
f_aero_data_from_camp(this->ptr.f_arg(), camp_core.ptr.f_arg());
4949
}
5050

51-
AeroData(const nlohmann::json &json) :
51+
AeroData(const nlohmann::ordered_json &json) :
5252
ptr(f_aero_data_ctor, f_aero_data_dtor)
5353
{
5454
if (!InputJSONResource::unique_keys(json))

src/aero_dist.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct AeroDist {
4444

4545
AeroDist(
4646
std::shared_ptr<AeroData> aero_data,
47-
const nlohmann::json &json
47+
const nlohmann::ordered_json &json
4848
):
4949
ptr(f_aero_dist_ctor, f_aero_dist_dtor),
5050
aero_data(aero_data)

src/aero_mode.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct AeroMode {
138138
ptr(f_aero_mode_ctor, f_aero_mode_dtor)
139139
{}
140140

141-
AeroMode(AeroData &aero_data, const nlohmann::json &json) :
141+
AeroMode(AeroData &aero_data, const nlohmann::ordered_json &json) :
142142
ptr(f_aero_mode_ctor, f_aero_mode_dtor)
143143
{
144144
if (json.size() != 1 || !json.is_object() || !json.begin().value().is_object())
@@ -149,7 +149,7 @@ struct AeroMode {
149149
guard.check_parameters();
150150
}
151151

152-
static void check_mode_json(const nlohmann::json &mode) {
152+
static void check_mode_json(const nlohmann::ordered_json &mode) {
153153
for (auto key : std::set<std::string>({"mass_frac", "mode_type"})) // TODO #320: more...
154154
if (mode.find(key) == mode.end())
155155
throw std::runtime_error("mode parameters dict must include key '" + key + "'");

src/env_state.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern "C" void f_env_state_air_dens(const void *ptr, double *air_density) noexc
2929
struct EnvState {
3030
PMCResource ptr;
3131

32-
EnvState(const nlohmann::json &json) :
32+
EnvState(const nlohmann::ordered_json &json) :
3333
ptr(f_env_state_ctor, f_env_state_dtor)
3434
{
3535
JSONResourceGuard<InputJSONResource> guard(json);

src/gas_data.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "gas_data_parameters.hpp"
1212
#include "camp_core.hpp"
1313
#include "nanobind/nanobind.h"
14-
#include "nanobind_json/nanobind_json.hpp"
14+
#include "nanobind_json/nanobind_json.h"
1515

1616
extern "C" void f_gas_data_ctor(void *ptr) noexcept;
1717
extern "C" void f_gas_data_dtor(void *ptr) noexcept;
@@ -26,7 +26,7 @@ extern "C" void f_gas_data_spec_name_by_index(const void *ptr, const int *i_spec
2626

2727
struct GasData {
2828
PMCResource ptr;
29-
const nlohmann::json json;
29+
const nlohmann::ordered_json json;
3030

3131
GasData(const CampCore &CampCore) :
3232
ptr(f_gas_data_ctor, f_gas_data_dtor)
@@ -38,11 +38,11 @@ struct GasData {
3838
ptr(f_gas_data_ctor, f_gas_data_dtor),
3939
json(tpl)
4040
{
41-
auto json_array = nlohmann::json::array();
41+
auto json_array = nlohmann::ordered_json::array();
4242
for (const auto item : tpl)
43-
json_array.push_back(nlohmann::json::object({{
43+
json_array.push_back(nlohmann::ordered_json::object({{
4444
nanobind::cast<std::string>(item),
45-
nlohmann::json::array()
45+
nlohmann::ordered_json::array()
4646
}}));
4747

4848
JSONResourceGuard<InputJSONResource> guard(json_array);

src/gas_state.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct GasState {
115115
return data;
116116
}
117117

118-
static void set_mix_rats(const GasState &self, const nlohmann::json &json) {
118+
static void set_mix_rats(const GasState &self, const nlohmann::ordered_json &json) {
119119
if (json.size() == 0)
120120
throw std::runtime_error("Non-empty sequence of mixing ratios expected");
121121

src/input_guard.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
struct InputGuard {
88
public:
9-
InputGuard(const nlohmann::json &j) {
9+
InputGuard(const nlohmann::ordered_json &j) {
1010
process_json(j);
1111

1212
this->dict_key_present = false;
@@ -71,8 +71,8 @@ struct InputGuard {
7171

7272
bool dict_key_present;
7373

74-
void process_json(const nlohmann::json &j) {
75-
nlohmann::json flat = j.flatten();
74+
void process_json(const nlohmann::ordered_json &j) {
75+
nlohmann::ordered_json flat = j.flatten();
7676

7777
// JSON Pointer, as in a string syntax for identifying a specific value in JSON
7878
std::vector<std::string> json_pointers;

0 commit comments

Comments
 (0)