INSTINCT Code Coverage Report


Directory: src/
File: util/Json.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 12 19 63.2%
Functions: 3 6 50.0%
Branches: 6 28 21.4%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 /// @file Json.hpp
10 /// @brief Defines how to save certain datatypes to json
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2021-08-10
13
14 #pragma once
15
16 #include <nlohmann/json.hpp>
17 using json = nlohmann::json; ///< json namespace
18
19 #include <imgui.h>
20 #include <implot.h>
21 #include <stdexcept>
22 #include <variant>
23
24 /// @brief Converts the provided color into a json object
25 /// @param[out] j Return Json object
26 /// @param[in] color Color to convert
27 void to_json(json& j, const ImColor& color);
28 /// @brief Converts the provided json object into a color
29 /// @param[in] j Json object with the color values
30 /// @param[out] color Color to return
31 void from_json(const json& j, ImColor& color);
32
33 /// @brief Converts the provided vector into a json object
34 /// @param[out] j Return Json object
35 /// @param[in] vec2 Vector to convert
36 void to_json(json& j, const ImVec2& vec2);
37 /// @brief Converts the provided json object into a vector
38 /// @param[in] j Json object with the vector values
39 /// @param[out] vec2 Vector to return
40 void from_json(const json& j, ImVec2& vec2);
41
42 /// @brief Converts the provided vector into a json object
43 /// @param[out] j Return Json object
44 /// @param[in] vec4 Vector to convert
45 void to_json(json& j, const ImVec4& vec4);
46 /// @brief Converts the provided json object into a vector
47 /// @param[in] j Json object with the vector values
48 /// @param[out] vec4 Vector to return
49 void from_json(const json& j, ImVec4& vec4);
50
51 /// @brief Converts the provided vector into a json object
52 /// @param[out] j Return Json object
53 /// @param[in] style Style to convert
54 void to_json(json& j, const ImPlotStyle& style);
55 /// @brief Converts the provided json object into a vector
56 /// @param[in] j Json object with the vector values
57 /// @param[out] style Style to return
58 void from_json(const json& j, ImPlotStyle& style);
59
60 namespace detail
61 {
62
63 /// @brief Variant serialize implementation
64 template<std::size_t N>
65 struct variant_switch
66 {
67 /// @brief Access operator into the variant
68 /// @param index Index of the variant
69 /// @param value Json value of the variant
70 /// @param v Variant to return
71 template<typename Variant>
72 12 void operator()(int index, const json& value, Variant& v) const
73 {
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (index == N)
75 {
76 v = value.get<std::variant_alternative_t<N, Variant>>();
77 }
78 else
79 {
80
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 variant_switch<N - 1>{}(index, value, v);
81 }
82 12 }
83 };
84
85 /// @brief Variant serialize implementation specialization for a single variant
86 template<>
87 struct variant_switch<0>
88 {
89 /// @brief Access operator into the variant
90 /// @param index Index of the variant
91 /// @param value Json value of the variant
92 /// @param v Variant to return
93 template<typename Variant>
94 12 void operator()(int index, const json& value, Variant& v) const
95 {
96
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (index == 0)
97 {
98
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 v = value.get<std::variant_alternative_t<0, Variant>>();
99 }
100 else
101 {
102 throw std::runtime_error(
103 "while converting json to variant: invalid index");
104 }
105 12 }
106 };
107
108 } // namespace detail
109
110 namespace nlohmann
111 {
112
113 /// @brief ADL serializer for JSON
114 template<typename... Args>
115 struct adl_serializer<std::variant<Args...>>
116 {
117 /// @brief Write info to a json object
118 /// @param[out] j Json output
119 /// @param[in] v Object to read info from
120 static void to_json(json& j, const std::variant<Args...>& v)
121 {
122 std::visit([&](auto&& value) {
123 j["index"] = v.index();
124 j["value"] = std::forward<decltype(value)>(value);
125 },
126 v);
127 }
128 /// @brief Read info from a json object
129 /// @param[in] j Json variable to read info from
130 /// @param[out] v Output object
131 12 static void from_json(const json& j, std::variant<Args...>& v)
132 {
133 12 auto const index = j.at("index").get<int>();
134
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 ::detail::variant_switch<sizeof...(Args) - 1>{}(index, j.at("value"), v);
135 12 }
136 };
137
138 } // namespace nlohmann
139