0.2.0
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
13
14#pragma once
15
16#include <nlohmann/json.hpp>
17using json = nlohmann::json;
18
19#include <imgui.h>
20#include <implot.h>
21#include <stdexcept>
22#include <variant>
23
27void to_json(json& j, const ImColor& color);
31void from_json(const json& j, ImColor& color);
32
36void to_json(json& j, const ImVec2& vec2);
40void from_json(const json& j, ImVec2& vec2);
41
45void to_json(json& j, const ImVec4& vec4);
49void from_json(const json& j, ImVec4& vec4);
50
54void to_json(json& j, const ImPlotStyle& style);
58void from_json(const json& j, ImPlotStyle& style);
59
60namespace detail
61{
62
64template<std::size_t N>
66{
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
86template<>
88{
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
114template<typename... Args>
115struct adl_serializer<std::variant<Args...>>
116{
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 }
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
Definition FlowManager.hpp:21
void from_json(const json &j, ImColor &color)
Converts the provided json object into a color.
void to_json(json &j, const ImColor &color)
Converts the provided color into a json object.
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