FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
json_serializer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2025-present FERS Contributors (see AUTHORS.md).
3
4/**
5 * @file json_serializer.h
6 * @brief Provides functions to serialize and deserialize the simulation world to/from JSON.
7 *
8 * This module is the primary data interchange layer between the C++ core engine and
9 * the user interface. JSON was chosen as the format for its native support in web
10 * technologies (like the React/TypeScript frontend), its human-readability, and its
11 * lightweight nature. This serializer defines the contract for how C++ simulation
12 * objects are represented in JSON, enabling the UI to read, modify, and write
13 * back the entire simulation state.
14 */
15
16#pragma once
17
18#include <nlohmann/json.hpp>
19#include <random>
20
21namespace core
22{
23 class World;
24}
25
26namespace serial
27{
28 /**
29 * @brief Serializes the entire simulation world into a nlohmann::json object.
30 *
31 * This function traverses the `core::World` object model and constructs a JSON
32 * representation. It is designed to produce a format that is convenient for the
33 * frontend to consume. This involves translating internal data formats (e.g.,
34 * angles in radians) to a more UI-friendly format (e.g., compass degrees) and
35 * restructuring complex object relationships (like monostatic radars) into simpler
36 * representations.
37 *
38 * @param world The world object to serialize.
39 * @return A nlohmann::json object representing the world.
40 */
41 nlohmann::json world_to_json(const core::World& world);
42
43 /**
44 * @brief Deserializes a nlohmann::json object and reconstructs the simulation world.
45 *
46 * This function is the counterpart to `world_to_json`. It performs a full state
47 * replacement by clearing the existing world and rebuilding it from the provided
48 * JSON. This "replace" strategy simplifies state management, guaranteeing that the
49 * C++ core is always perfectly synchronized with the state provided by the UI without
50 * requiring complex diffing or patching logic. It also handles re-seeding the master
51 * random number generator to ensure that loading a state also restores its
52 * deterministic behavior.
53 *
54 * @param j The json object to deserialize.
55 * @param world The world object to populate.
56 * @param masterSeeder A reference to the master random number generator, which will be re-seeded.
57 */
58 void json_to_world(const nlohmann::json& j, core::World& world, std::mt19937& masterSeeder);
59}
The World class manages the simulator environment.
Definition world.h:38
void json_to_world(const nlohmann::json &j, core::World &world, std::mt19937 &masterSeeder)
Deserializes a nlohmann::json object and reconstructs the simulation world.
nlohmann::json world_to_json(const core::World &world)
Serializes the entire simulation world into a nlohmann::json object.