0.4.1
Loading...
Searching...
No Matches
Json.hpp
Go to the documentation of this file.
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>
17using 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
27void 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
31void 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
36void 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
40void 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
45void 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
49void 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
54void 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
58void from_json(const json& j, ImPlotStyle& style);
59
60namespace detail
61{
62
63/// @brief Variant serialize implementation
64template<std::size_t N>
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 void operator()(int index, const json& value, Variant& v) const
73 {
74 if (index == N)
75 {
76 v = value.get<std::variant_alternative_t<N, Variant>>();
77 }
78 else
79 {
80 variant_switch<N - 1>{}(index, value, v);
81 }
82 }
83};
84
85/// @brief Variant serialize implementation specialization for a single variant
86template<>
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 void operator()(int index, const json& value, Variant& v) const
95 {
96 if (index == 0)
97 {
98 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 }
106};
107
108} // namespace detail
109
110namespace nlohmann
111{
112
113/// @brief ADL serializer for JSON
114template<typename... Args>
115struct 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 static void from_json(const json& j, std::variant<Args...>& v)
132 {
133 auto const index = j.at("index").get<int>();
134 ::detail::variant_switch<sizeof...(Args) - 1>{}(index, j.at("value"), v);
135 }
136};
137
138} // namespace nlohmann
nlohmann::json json
json namespace
void from_json(const json &j, ImColor &color)
Converts the provided json object into a color.
Definition Json.cpp:26
void to_json(json &j, const ImColor &color)
Converts the provided color into a json object.
Definition Json.cpp:17
void operator()(int index, const json &value, Variant &v) const
Access operator into the variant.
Definition Json.hpp:94
Variant serialize implementation.
Definition Json.hpp:66
void operator()(int index, const json &value, Variant &v) const
Access operator into the variant.
Definition Json.hpp:72
static void to_json(json &j, const std::variant< Args... > &v)
Write info to a json object.
Definition Json.hpp:120
static void from_json(const json &j, std::variant< Args... > &v)
Read info from a json object.
Definition Json.hpp:131