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
21#include "core/sim_id.h"
22
23namespace timing
24{
25 class PrototypeTiming;
26}
27namespace antenna
28{
29 class Antenna;
30}
31namespace fers_signal
32{
33 class RadarSignal;
34}
35namespace radar
36{
37 class Target;
38 class Receiver;
39 class Transmitter;
40 class Platform;
41}
42namespace core
43{
44 class World;
45}
46
47namespace serial
48{
49 /**
50 * @brief Serializes the entire simulation world into a nlohmann::json object.
51 *
52 * This function traverses the `core::World` object model and constructs a JSON
53 * representation. It is designed to produce a format that is convenient for the
54 * frontend to consume. This involves translating internal data formats (e.g.,
55 * angles in radians) to a more UI-friendly format (e.g., compass degrees) and
56 * restructuring complex object relationships (like monostatic radars) into simpler
57 * representations.
58 *
59 * @param world The world object to serialize.
60 * @return A nlohmann::json object representing the world.
61 */
62 nlohmann::json world_to_json(const core::World& world);
63
64 /**
65 * @brief Updates a platform's motion and rotation paths from JSON.
66 */
67 void update_platform_paths_from_json(const nlohmann::json& j, radar::Platform* plat);
68
69 /**
70 * @brief Parses an Antenna from JSON.
71 */
72 std::unique_ptr<antenna::Antenna> parse_antenna_from_json(const nlohmann::json& j);
73
74 /**
75 * @brief Parses a Waveform from JSON.
76 */
77 std::unique_ptr<fers_signal::RadarSignal> parse_waveform_from_json(const nlohmann::json& j);
78
79 /**
80 * @brief Parses a timing prototype from JSON.
81 */
82 std::unique_ptr<timing::PrototypeTiming> parse_timing_from_json(const nlohmann::json& j, SimId id);
83
84 /**
85 * @brief Updates global simulation parameters from JSON.
86 */
87 void update_parameters_from_json(const nlohmann::json& j, std::mt19937& masterSeeder);
88
89 /**
90 * @brief Updates an antenna from JSON without full context recreation.
91 */
92 void update_antenna_from_json(const nlohmann::json& j, antenna::Antenna* ant, core::World& world);
93
94 /**
95 * @brief Updates a transmitter from JSON without full context recreation.
96 */
97 void update_transmitter_from_json(const nlohmann::json& j, radar::Transmitter* tx, core::World& world,
98 std::mt19937& masterSeeder);
99
100 /**
101 * @brief Updates a receiver from JSON without full context recreation.
102 */
103 void update_receiver_from_json(const nlohmann::json& j, radar::Receiver* rx, core::World& world,
104 std::mt19937& masterSeeder);
105
106 /**
107 * @brief Updates a target from JSON without full context recreation.
108 */
109 void update_target_from_json(const nlohmann::json& j, radar::Target* tgt, core::World& world,
110 std::mt19937& masterSeeder);
111
112 /**
113 * @brief Updates a monostatic radar from JSON without full context recreation.
114 */
115 void update_monostatic_from_json(const nlohmann::json& j, radar::Transmitter* tx, radar::Receiver* rx,
116 core::World& world, std::mt19937& masterSeeder);
117
118 /**
119 * @brief Updates a timing source from JSON without full context recreation.
120 */
121 void update_timing_from_json(const nlohmann::json& j, core::World& world, SimId id);
122
123 /**
124 * @brief Deserializes a nlohmann::json object and reconstructs the simulation world.
125 *
126 * This function is the counterpart to `world_to_json`. It performs a full state
127 * replacement by clearing the existing world and rebuilding it from the provided
128 * JSON. This "replace" strategy simplifies state management, guaranteeing that the
129 * C++ core is always perfectly synchronized with the state provided by the UI without
130 * requiring complex diffing or patching logic. It also handles re-seeding the master
131 * random number generator to ensure that loading a state also restores its
132 * deterministic behavior.
133 *
134 * @param j The json object to deserialize.
135 * @param world The world object to populate.
136 * @param masterSeeder A reference to the master random number generator, which will be re-seeded.
137 */
138 void json_to_world(const nlohmann::json& j, core::World& world, std::mt19937& masterSeeder);
139}
Abstract base class representing an antenna.
The World class manages the simulator environment.
Definition world.h:39
Represents a simulation platform with motion and rotation paths.
Definition platform.h:32
Manages radar signal reception and response processing.
Definition receiver.h:37
Base class for radar targets.
Definition target.h:118
Represents a radar transmitter system.
Definition transmitter.h:33
void update_platform_paths_from_json(const nlohmann::json &j, radar::Platform *plat)
Updates a platform's motion and rotation paths from JSON.
void update_parameters_from_json(const nlohmann::json &j, std::mt19937 &masterSeeder)
Updates global simulation parameters from JSON.
void json_to_world(const nlohmann::json &j, core::World &world, std::mt19937 &masterSeeder)
Deserializes a nlohmann::json object and reconstructs the simulation world.
void update_receiver_from_json(const nlohmann::json &j, radar::Receiver *rx, core::World &world, std::mt19937 &)
Updates a receiver from JSON without full context recreation.
void update_timing_from_json(const nlohmann::json &j, core::World &world, const SimId id)
Updates a timing source from JSON without full context recreation.
void update_monostatic_from_json(const nlohmann::json &j, radar::Transmitter *tx, radar::Receiver *rx, core::World &world, std::mt19937 &masterSeeder)
Updates a monostatic radar from JSON without full context recreation.
void update_transmitter_from_json(const nlohmann::json &j, radar::Transmitter *tx, core::World &world, std::mt19937 &)
Updates a transmitter from JSON without full context recreation.
void update_antenna_from_json(const nlohmann::json &j, antenna::Antenna *ant, core::World &world)
Updates an antenna from JSON without full context recreation.
std::unique_ptr< antenna::Antenna > parse_antenna_from_json(const nlohmann::json &j)
Parses an Antenna from JSON.
void update_target_from_json(const nlohmann::json &j, radar::Target *existing_tgt, core::World &world, std::mt19937 &)
Updates a target from JSON without full context recreation.
nlohmann::json world_to_json(const core::World &world)
Serializes the entire simulation world into a nlohmann::json object.
std::unique_ptr< timing::PrototypeTiming > parse_timing_from_json(const nlohmann::json &j, const SimId id)
Parses a timing prototype from JSON.
std::unique_ptr< fers_signal::RadarSignal > parse_waveform_from_json(const nlohmann::json &j)
Parses a Waveform from JSON.
uint64_t SimId
64-bit Unique Simulation ID.
Definition sim_id.h:18