OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
domain_to_json.hpp
1#pragma once
2#include <ossia/network/domain/domain.hpp>
3#include <ossia/network/oscquery/detail/value_to_json.hpp>
4
5namespace ossia::oscquery::detail
6{
7inline void write_json(ossia::json_writer& writer, int v)
8{
9 writer.Int(v);
10}
11inline void write_json(ossia::json_writer& writer, double v)
12{
13 writer.Double(v);
14}
15inline void write_json(ossia::json_writer& writer, float v)
16{
17 writer.Double(v);
18}
19inline void write_json(ossia::json_writer& writer, bool v)
20{
21 writer.Bool(v);
22}
23inline void write_json(ossia::json_writer& writer, const std::string& v)
24{
25 writer.String(v);
26}
27inline void write_json(ossia::json_writer& writer, const ossia::value& v)
28{
29 v.apply(value_to_json{writer, {}});
30}
31
34{
35 ossia::json_writer& writer;
36 void operator()() { writer.Null(); }
37
38 void operator()(const ossia::domain_base<impulse>& dom) { writer.Null(); }
39
40 void operator()(const ossia::domain_base<bool>& dom)
41 {
42 writer.StartArray();
43 writer.StartObject();
44 writer.Key("VALS");
45 writer.StartArray();
46 write_json(writer, false);
47 write_json(writer, true);
48 writer.EndArray();
49 writer.EndObject();
50 writer.EndArray();
51 }
52
53 template <typename T>
54 void operator()(const ossia::domain_base<T>& dom)
55 {
56 bool has_min = bool(dom.min);
57 bool has_max = bool(dom.max);
58 bool has_values = !dom.values.empty();
59 if(has_min || has_max || has_values)
60 {
61 writer.StartArray();
62 writer.StartObject();
63 if(has_min)
64 {
65 writer.Key("MIN");
66 write_json(writer, *dom.min);
67 }
68 if(has_max)
69 {
70 writer.Key("MAX");
71 write_json(writer, *dom.max);
72 }
73 if(has_values)
74 {
75 writer.Key("VALS");
76 writer.StartArray();
77 for(auto val : dom.values)
78 write_json(writer, val);
79 writer.EndArray();
80 }
81 writer.EndObject();
82 writer.EndArray();
83 }
84 else
85 {
86 writer.Null();
87 }
88 }
89
90 void operator()(const ossia::domain_base<std::string>& dom)
91 {
92 if(!dom.values.empty())
93 {
94 writer.StartArray();
95 writer.StartObject();
96 writer.Key("VALS");
97 writer.StartArray();
98 for(auto val : dom.values)
99 write_json(writer, val);
100 writer.EndArray();
101 writer.EndObject();
102 writer.EndArray();
103 }
104 else
105 {
106 writer.Null();
107 }
108 }
109
110 void operator()(const ossia::vector_domain& dom)
111 {
112 const auto min_count = dom.min.size();
113 const auto max_count = dom.max.size();
114 const auto values_count = dom.values.size();
115 const auto N = std::max(std::max(min_count, max_count), values_count);
116 if(N > 0)
117 {
118 writer.StartArray();
119 for(std::size_t i = 0; i < N; i++)
120 {
121 if(values_count > i && !dom.values[i].empty())
122 {
123 writer.StartObject();
124
125 writer.Key("VALS");
126 writer.StartArray();
127
128 for(const auto& val : dom.values[i])
129 write_json(writer, val);
130
131 writer.EndArray();
132 writer.EndObject();
133 }
134 else if(
135 (min_count > i && dom.min[i].valid())
136 || (max_count > i && dom.max[i].valid()))
137 {
138 writer.StartObject();
139 if(dom.min[i].valid())
140 {
141 writer.Key("MIN");
142 write_json(writer, dom.min[i]);
143 }
144
145 if(dom.max[i].valid())
146 {
147 writer.Key("MAX");
148 write_json(writer, dom.max[i]);
149 }
150 writer.EndObject();
151 }
152 else
153 {
154 writer.Null();
155 }
156 }
157 writer.EndArray();
158 }
159 else
160 {
161 writer.Null();
162 }
163 }
164
165 template <std::size_t N>
166 void operator()(const ossia::vecf_domain<N>& dom)
167 {
168 // TODO handle rgba case
169 writer.StartArray();
170 for(std::size_t i = 0; i < N; i++)
171 {
172 if(!dom.values[i].empty())
173 {
174 writer.StartObject();
175
176 writer.Key("VALS");
177 writer.StartArray();
178
179 for(const auto val : dom.values[i])
180 writer.Double(val);
181
182 writer.EndArray();
183 writer.EndObject();
184 }
185 else if(dom.min[i] || dom.max[i])
186 {
187 writer.StartObject();
188 if(dom.min[i])
189 {
190 writer.Key("MIN");
191 write_json(writer, *dom.min[i]);
192 }
193
194 if(dom.max[i])
195 {
196 writer.Key("MAX");
197 write_json(writer, *dom.max[i]);
198 }
199 writer.EndObject();
200 }
201 else
202 {
203 writer.Null();
204 }
205 }
206 writer.EndArray();
207 }
208
209 void operator()(const ossia::domain_base<ossia::value>& dom)
210 {
211 /* TODO this code is not correct
212 if (dom.min)
213 {
214 dom.min->apply(value_to_json{writer});
215 }
216 else
217 {
218 writer.Null();
219 }
220
221 if (dom.max)
222 {
223 dom.max->apply(value_to_json{writer});
224 }
225 else
226 {
227 writer.Null();
228 }
229
230 if (!dom.values.empty())
231 {
232 writer.StartArray();
233 for (const auto& val : dom.values)
234 {
235 val.apply(value_to_json{writer});
236 }
237 writer.EndArray();
238 }
239 else
240 {
241 writer.Null(); // TODO why not just nothing ?
242 }
243 */
244 }
245};
246}
The value class.
Definition value.hpp:173
Write a domain to json.
Definition domain_to_json.hpp:34