1+ #pragma once
2+
3+ #include < string>
4+ #include < variant>
5+
6+ namespace ConnectedSystemsAPI {
7+ namespace DataModels {
8+ namespace Data {
9+ struct DataValue {
10+ using ValueType = std::variant<
11+ bool ,
12+ int64_t ,
13+ uint64_t ,
14+ double ,
15+ std::string
16+ >;
17+ ValueType value;
18+
19+ DataValue () = default ;
20+ DataValue (const DataValue&) = default ;
21+ DataValue (DataValue&&) noexcept = default ;
22+ DataValue& operator =(const DataValue&) = default ;
23+ DataValue& operator =(DataValue&&) noexcept = default ;
24+ ~DataValue () = default ;
25+
26+ DataValue (bool v) : value(v) {}
27+ DataValue (int64_t v) : value(v) {}
28+ DataValue (uint64_t v) : value(v) {}
29+ DataValue (double v) : value(v) {}
30+ DataValue (const std::string& v) : value(v) {}
31+ DataValue (std::string&& v) : value(std::move(v)) {}
32+ DataValue (const char * s) : value(std::string(s)) {}
33+
34+ std::string toString () const {
35+ if (std::holds_alternative<bool >(value)) {
36+ return std::holds_alternative<bool >(value) && std::get<bool >(value) ? " true" : " false" ;
37+ }
38+ else if (std::holds_alternative<int64_t >(value)) {
39+ return std::to_string (std::get<int64_t >(value));
40+ }
41+ else if (std::holds_alternative<uint64_t >(value)) {
42+ return std::to_string (std::get<uint64_t >(value));
43+ }
44+ else if (std::holds_alternative<double >(value)) {
45+ return std::to_string (std::get<double >(value));
46+ }
47+ else if (std::holds_alternative<std::string>(value)) {
48+ return std::get<std::string>(value);
49+ }
50+ else {
51+ return " " ;
52+ }
53+ }
54+
55+ friend void from_json (const nlohmann::json& j, DataValue& v);
56+ friend void to_json (nlohmann::ordered_json& j, const DataValue& v);
57+ };
58+
59+ inline void from_json (const nlohmann::json& j, DataValue& v) {
60+ if (j.is_boolean ()) {
61+ v.value = j.get <bool >();
62+ }
63+ else if (j.is_number_integer ()) {
64+ v.value = j.get <int64_t >();
65+ }
66+ else if (j.is_number_unsigned ()) {
67+ v.value = j.get <uint64_t >();
68+ }
69+ else if (j.is_number_float ()) {
70+ v.value = j.get <double >();
71+ }
72+ else if (j.is_string ()) {
73+ v.value = j.get <std::string>();
74+ }
75+ }
76+
77+ inline void to_json (nlohmann::ordered_json& j, const DataValue& v) {
78+ if (std::holds_alternative<bool >(v.value )) {
79+ j = std::get<bool >(v.value );
80+ }
81+ else if (std::holds_alternative<int64_t >(v.value )) {
82+ j = std::get<int64_t >(v.value );
83+ }
84+ else if (std::holds_alternative<uint64_t >(v.value )) {
85+ j = std::get<uint64_t >(v.value );
86+ }
87+ else if (std::holds_alternative<double >(v.value )) {
88+ j = std::get<double >(v.value );
89+ }
90+ else if (std::holds_alternative<std::string>(v.value )) {
91+ j = std::get<std::string>(v.value );
92+ }
93+ }
94+ }
95+ }
96+ }
0 commit comments