Skip to content

Commit 06c9299

Browse files
committed
fix: fix json serialization by using correct Initializers, iif the init wasn't the correct one for an object, it would use an array, so now it always uses an object
1 parent 2276523 commit 06c9299

File tree

7 files changed

+89
-93
lines changed

7 files changed

+89
-93
lines changed

src/input/controller_input.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,21 @@ namespace nlohmann {
149149
}
150150

151151
static void to_json(json& obj, const input::ControllerSettings& settings) {
152-
obj = nlohmann::json{
153-
{ "rotate_left", settings.rotate_left.to_string() },
154-
{ "rotate_right", settings.rotate_right.to_string() },
155-
{ "move_left", settings.move_left.to_string() },
156-
{ "move_right", settings.move_right.to_string() },
157-
{ "move_down", settings.move_down.to_string() },
158-
{ "drop", settings.drop.to_string() },
159-
{ "hold", settings.hold.to_string() },
160-
{
161-
"menu", nlohmann::json{
162-
{ "pause", settings.pause.to_string() },
163-
{ "open_settings", settings.open_settings.to_string() },
164-
}, }
165-
};
152+
obj = nlohmann::json::object({
153+
{ "rotate_left", settings.rotate_left.to_string() },
154+
{ "rotate_right", settings.rotate_right.to_string() },
155+
{ "move_left", settings.move_left.to_string() },
156+
{ "move_right", settings.move_right.to_string() },
157+
{ "move_down", settings.move_down.to_string() },
158+
{ "drop", settings.drop.to_string() },
159+
{ "hold", settings.hold.to_string() },
160+
{
161+
"menu", nlohmann::json::object({
162+
{ "pause", settings.pause.to_string() },
163+
{ "open_settings", settings.open_settings.to_string() },
164+
}),
165+
}
166+
});
166167
}
167168
};
168169
} // namespace nlohmann

src/input/joystick_input.hpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,10 @@ namespace nlohmann {
377377
}
378378

379379
static void to_json(json& obj, const input::JoystickIdentification& identification) {
380-
obj = nlohmann::json{
381-
{ "guid", identification.guid.to_string(), { "name", identification.name } },
382-
};
380+
obj = nlohmann::json::object({
381+
{ "guid", identification.guid.to_string() },
382+
{ "name", identification.name }
383+
});
383384
}
384385
};
385386

@@ -431,24 +432,21 @@ namespace nlohmann {
431432

432433
static void to_json(json& obj, const input::JoystickSettings& settings) {
433434

434-
auto identification = nlohmann::json{};
435+
auto identification = nlohmann::json::object();
435436
adl_serializer<input::JoystickIdentification>::to_json(identification, settings.identification);
436437

437-
obj = nlohmann::json{
438-
{ "identification", identification },
439-
{ "rotate_left", settings.rotate_left },
440-
{ "rotate_right", settings.rotate_right },
441-
{ "move_left", settings.move_left },
442-
{ "move_right", settings.move_right },
443-
{ "move_down", settings.move_down },
444-
{ "drop", settings.drop },
445-
{ "hold", settings.hold },
446-
{
447-
"menu", nlohmann::json{
448-
{ "pause", settings.pause },
449-
{ "open_settings", settings.open_settings },
450-
}, }
451-
};
438+
obj = nlohmann::json::object({
439+
{ "identification", identification },
440+
{ "rotate_left", settings.rotate_left },
441+
{ "rotate_right", settings.rotate_right },
442+
{ "move_left", settings.move_left },
443+
{ "move_right", settings.move_right },
444+
{ "move_down", settings.move_down },
445+
{ "drop", settings.drop },
446+
{ "hold", settings.hold },
447+
{ "menu", nlohmann::json::object({ { "pause", settings.pause },
448+
{ "open_settings", settings.open_settings } }) }
449+
});
452450
}
453451
};
454452
} // namespace nlohmann

src/input/keyboard_input.hpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,17 @@ namespace nlohmann {
147147
}
148148

149149
static void to_json(json& obj, const input::KeyboardSettings& settings) {
150-
obj = nlohmann::json{
151-
{ "rotate_left", settings.rotate_left.to_string() },
152-
{ "rotate_right", settings.rotate_right.to_string() },
153-
{ "move_left", settings.move_left.to_string() },
154-
{ "move_right", settings.move_right.to_string() },
155-
{ "move_down", settings.move_down.to_string() },
156-
{ "drop", settings.drop.to_string() },
157-
{ "hold", settings.hold.to_string() },
158-
{
159-
"menu", nlohmann::json{
160-
{ "pause", settings.pause.to_string() },
161-
{ "open_settings", settings.open_settings.to_string() },
162-
}, }
163-
};
150+
obj = nlohmann::json::object({
151+
{ "rotate_left", settings.rotate_left.to_string() },
152+
{ "rotate_right", settings.rotate_right.to_string() },
153+
{ "move_left", settings.move_left.to_string() },
154+
{ "move_right", settings.move_right.to_string() },
155+
{ "move_down", settings.move_down.to_string() },
156+
{ "drop", settings.drop.to_string() },
157+
{ "hold", settings.hold.to_string() },
158+
{ "menu", nlohmann::json::object({ { "pause", settings.pause.to_string() },
159+
{ "open_settings", settings.open_settings.to_string() } }) }
160+
});
164161
}
165162
};
166163
} // namespace nlohmann

src/input/touch_input.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ namespace nlohmann {
214214

215215
static void to_json(json& obj, const input::TouchSettings& settings) {
216216

217-
obj = nlohmann::json{
218-
{ "move_x_threshold", settings.move_x_threshold },
219-
{ "move_y_threshold", settings.move_y_threshold },
220-
{ "rotation_duration_threshold", settings.rotation_duration_threshold },
221-
{ "drop_duration_threshold", settings.drop_duration_threshold },
222-
};
217+
obj = nlohmann::json::object({
218+
{ "move_x_threshold", settings.move_x_threshold },
219+
{ "move_y_threshold", settings.move_y_threshold },
220+
{ "rotation_duration_threshold", settings.rotation_duration_threshold },
221+
{ "drop_duration_threshold", settings.drop_duration_threshold }
222+
});
223223
}
224224
};
225225
} // namespace nlohmann

src/libs/core/helper/parse_json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ namespace json {
137137
const auto result = json::try_json_to_string<T>(value, pretty);
138138

139139
if (not result.has_value()) {
140-
return fmt::format("unable to convert settings to json {}", result.error());
140+
return fmt::format("unable to convert settings to json: {}", result.error());
141141
}
142142

143143
std::ofstream file_stream{ file };

src/libs/recordings/utility/recording_json_wrapper.hpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ namespace nlohmann {
6464
}
6565

6666
static void to_json(json& obj, const recorder::TetrionHeader& tetrion_header) {
67-
obj = nlohmann::json{
68-
{ "seed", tetrion_header.seed },
69-
{ "starting_level", tetrion_header.starting_level }
70-
};
67+
obj = nlohmann::json::object({
68+
{ "seed", tetrion_header.seed },
69+
{ "starting_level", tetrion_header.starting_level }
70+
});
7171
}
7272
};
7373

@@ -93,11 +93,11 @@ namespace nlohmann {
9393

9494
static void to_json(json& obj, const recorder::Record& record) {
9595

96-
obj = nlohmann::json{
97-
{ "tetrion_index", record.tetrion_index },
98-
{ "simulation_step_index", record.simulation_step_index },
99-
{ "event", record.event }
100-
};
96+
obj = nlohmann::json::object({
97+
{ "tetrion_index", record.tetrion_index },
98+
{ "simulation_step_index", record.simulation_step_index },
99+
{ "event", record.event }
100+
});
101101
}
102102
};
103103

@@ -110,10 +110,10 @@ namespace nlohmann {
110110
}
111111

112112
static void to_json(json& obj, const shapes::AbstractPoint<T>& point) {
113-
obj = nlohmann::json{
114-
{ "x", point.x },
115-
{ "y", point.y }
116-
};
113+
obj = nlohmann::json::object({
114+
{ "x", point.x },
115+
{ "y", point.y }
116+
});
117117
}
118118
};
119119

@@ -139,10 +139,10 @@ namespace nlohmann {
139139
}
140140

141141
static void to_json(json& obj, const Mino& mino) {
142-
obj = nlohmann::json{
143-
{ "position", mino.position() },
144-
{ "type", mino.type() }
145-
};
142+
obj = nlohmann::json::object({
143+
{ "position", mino.position() },
144+
{ "type", mino.type() }
145+
});
146146
}
147147
};
148148

@@ -160,14 +160,14 @@ namespace nlohmann {
160160
nlohmann::adl_serializer<std::vector<Mino>>::to_json(mino_stack_json, snapshot.mino_stack().minos());
161161

162162

163-
obj = nlohmann::json{
164-
{ "tetrion_index", snapshot.tetrion_index() },
165-
{ "level", snapshot.level() },
166-
{ "score", snapshot.score() },
167-
{ "lines_cleared", snapshot.lines_cleared() },
168-
{ "simulation_step_index", snapshot.simulation_step_index() },
169-
{ "mino_stack", mino_stack_json }
170-
};
163+
obj = nlohmann::json::object({
164+
{ "tetrion_index", snapshot.tetrion_index() },
165+
{ "level", snapshot.level() },
166+
{ "score", snapshot.score() },
167+
{ "lines_cleared", snapshot.lines_cleared() },
168+
{ "simulation_step_index", snapshot.simulation_step_index() },
169+
{ "mino_stack", mino_stack_json }
170+
});
171171
}
172172
};
173173

@@ -199,13 +199,13 @@ namespace nlohmann {
199199
snapshots_json, recording_reader.snapshots()
200200
);
201201

202-
obj = nlohmann::json{
203-
{ "version", recorder::Recording::current_supported_version_number },
204-
{ "information", information_json },
205-
{ "tetrion_headers", tetrion_headers_json },
206-
{ "records", records_json },
207-
{ "snapshots", snapshots_json },
208-
};
202+
obj = nlohmann::json::object({
203+
{ "version", recorder::Recording::current_supported_version_number },
204+
{ "information", information_json },
205+
{ "tetrion_headers", tetrion_headers_json },
206+
{ "records", records_json },
207+
{ "snapshots", snapshots_json },
208+
});
209209
}
210210
};
211211
} // namespace nlohmann

src/manager/settings.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99

1010
void settings::to_json(nlohmann::json& obj, const settings::Settings& settings) {
11-
obj = nlohmann::json{
12-
{ "controls",
13-
nlohmann::json{ { "inputs", settings.controls }, { "selected", settings.selected } },
14-
{ "volume", settings.volume },
15-
{ "discord", settings.discord },
16-
{ "api_url", settings.api_url } }
17-
};
11+
obj = nlohmann::json::object({
12+
{ "controls",
13+
nlohmann::json::object({ { "inputs", settings.controls }, { "selected", settings.selected } }) },
14+
{ "volume", settings.volume },
15+
{ "discord", settings.discord },
16+
{ "api_url", settings.api_url }
17+
});
1818
}
1919

2020
void settings::from_json(const nlohmann::json& obj, settings::Settings& settings) {

0 commit comments

Comments
 (0)