Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafJsonDataType.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) Kontur AS
5 //
6 // GNU Lesser General Public License Usage
7 // This library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation; either version 2.1 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
17 // for more details.
18 //
19 
20 #pragma once
21 
22 #include "cafAppEnum.h"
23 #include "cafObjectHandlePortableDataType.h"
24 #include "cafPortableDataType.h"
25 
26 #include <nlohmann/json.hpp>
27 
28 namespace caffa
29 {
30 template <class _Tp>
31 struct is_chrono_integral : std::false_type
32 {
33 };
34 
35 template <class Rep, class Period>
36 struct is_chrono_integral<std::chrono::duration<Rep, Period>> : std::true_type
37 {
38 };
39 
40 template <class Clock, class Duration>
41 struct is_chrono_integral<std::chrono::time_point<Clock, Duration>> : std::true_type
42 {
43 };
44 
45 template <typename _Tp>
46 concept chrono_integral = is_chrono_integral<_Tp>::value;
47 
48 template <typename _Tp>
49 concept supported_integral = ( std::integral<_Tp> || chrono_integral<_Tp> ) && !std::is_same<_Tp, bool>::value;
50 
54 template <typename DataType>
56 {
57  static nlohmann::json jsonType()
58  {
59  auto object = nlohmann::json::object();
60  object["type"] = PortableDataType<DataType>::name();
61 
62  return object;
63  }
64 };
65 
66 template <>
67 struct JsonDataType<void>
68 {
69  static nlohmann::json jsonType() { return nlohmann::json::object(); }
70 };
71 
72 template <typename DataType>
73 struct JsonDataType<std::vector<DataType>>
74 {
75  static nlohmann::json jsonType()
76  {
77  auto object = nlohmann::json::object();
78  object["type"] = "array";
79  object["items"] = JsonDataType<DataType>::jsonType();
80 
81  return object;
82  }
83 };
84 
85 template <supported_integral DataType>
86 struct JsonDataType<DataType>
87 {
88  static nlohmann::json jsonType()
89  {
90  auto object = nlohmann::json::object();
91  object["type"] = "integer";
92  object["format"] = PortableDataType<DataType>::name();
93  return object;
94  }
95 };
96 
97 template <std::floating_point DataType>
98 struct JsonDataType<DataType>
99 {
100  static nlohmann::json jsonType()
101  {
102  auto object = nlohmann::json::object();
103  object["type"] = "number";
104  object["format"] = PortableDataType<DataType>::name();
105  return object;
106  }
107 };
108 
112 template <DerivesFromObjectHandle DataType>
113 struct JsonDataType<DataType>
114 {
115  static nlohmann::json jsonType()
116  {
117  auto object = nlohmann::json::object();
118  object["$ref"] = std::string( "#/components/object_schemas/" ) + DataType::classKeywordStatic();
119  return object;
120  }
121 };
122 
126 template <IsSharedPtr DataType>
127 struct JsonDataType<DataType>
128 {
129  static nlohmann::json jsonType()
130  {
131  auto object = nlohmann::json::object();
132  object["$ref"] = std::string( "#/components/object_schemas/" ) + DataType::element_type::classKeywordStatic();
133  return object;
134  }
135 };
136 
137 template <typename EnumType>
138 struct JsonDataType<AppEnum<EnumType>>
139 {
140  static nlohmann::json jsonType()
141  {
142  auto values = nlohmann::json::array();
143  for ( auto entry : AppEnum<EnumType>::validLabels() )
144  {
145  values.push_back( entry );
146  }
147  auto object = nlohmann::json::object();
148  object["enum"] = values;
149  return object;
150  }
151 };
152 
153 template <typename Enum>
154 void to_json( nlohmann::json& jsonValue, const AppEnum<Enum>& appEnum )
155 {
156  std::stringstream stream;
157  stream << appEnum;
158  jsonValue = stream.str();
159 }
160 
161 template <typename Enum>
162 void from_json( const nlohmann::json& jsonValue, AppEnum<Enum>& appEnum )
163 {
164  std::stringstream stream( jsonValue.get<std::string>() );
165  stream >> appEnum;
166 }
167 
168 } // namespace caffa
Definition: cafJsonDataType.h:31
Definition: cafJsonDataType.h:55
Definition: cafPortableDataType.h:35
Definition: cafAppEnum.h:65
Main Caffa namespace.
Definition: cafApplication.h:30