Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafJsonDataType.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 3D-Radar 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 <nlohmann/json.hpp>
23 
24 #include <concepts>
25 #include <string>
26 #include <typeinfo>
27 #include <utility>
28 #include <vector>
29 
30 namespace caffa
31 {
35 template <typename DataType>
37 {
38  static nlohmann::json type()
39  {
40  auto object = nlohmann::json::object();
41  object["type"] = "object";
42 
43  return object;
44  }
45 };
46 
47 template <>
48 struct JsonDataType<void>
49 {
50  static nlohmann::json type() { return nlohmann::json::object(); }
51 };
52 
53 template <typename DataType>
54 struct JsonDataType<std::vector<DataType>>
55 {
56  static nlohmann::json type()
57  {
58  auto object = nlohmann::json::object();
59  object["type"] = "array";
60  object["items"] = JsonDataType<DataType>::type();
61 
62  return object;
63  }
64 };
65 
66 template <std::floating_point DataType>
67 struct JsonDataType<DataType>
68 {
69  static nlohmann::json type()
70  {
71  auto object = nlohmann::json::object();
72  object["type"] = "number";
73  return object;
74  }
75 };
76 
77 template <std::integral DataType>
78 struct JsonDataType<DataType>
79 {
80  static nlohmann::json type()
81  {
82  auto object = nlohmann::json::object();
83  object["type"] = "integer";
84  return object;
85  }
86 };
87 
88 template <>
89 struct JsonDataType<bool>
90 {
91  static nlohmann::json type()
92  {
93  auto object = nlohmann::json::object();
94  object["type"] = "boolean";
95  return object;
96  }
97 };
98 
99 template <>
100 struct JsonDataType<std::string>
101 {
102  static nlohmann::json type()
103  {
104  auto object = nlohmann::json::object();
105  object["type"] = "string";
106  return object;
107  }
108 };
109 
110 } // namespace caffa
Main Caffa namespace.
Definition: __init__.py:1
Definition: cafJsonDataType.h:37