Loading...
Searching...
No Matches
JSONVisitor.hpp
1#pragma once
2#include <score/model/Identifier.hpp>
3#include <score/serialization/CommonTypes.hpp>
4#include <score/serialization/StringConstants.hpp>
5#include <score/serialization/VisitorInterface.hpp>
6#include <score/serialization/VisitorTags.hpp>
7#include <score/tools/Debug.hpp>
8#include <score/tools/ForEach.hpp>
9
10#include <ossia/detail/flat_set.hpp>
11#include <ossia/detail/json.hpp>
12#include <ossia/detail/small_vector.hpp>
13
14#include <boost/container/small_vector.hpp>
15#include <boost/container/static_vector.hpp>
16
17#include <QDebug>
18
19#include <verdigris>
20
21template <typename T>
23
28namespace score
29{
30template <typename T>
31class Entity;
32
33class ApplicationComponents;
34}
35using JsonWriter = ossia::json_writer;
37{
38 template <typename T>
39 friend bool operator==(std::optional<T>& lhs, const OptionalSentinel& rhs)
40 {
41 return !bool(lhs);
42 }
43
44 template <typename T>
45 friend bool operator!=(std::optional<T>& lhs, const OptionalSentinel& rhs)
46 {
47 return bool(lhs);
48 }
49};
50
51class SCORE_LIB_BASE_EXPORT JSONReader : public AbstractVisitor
52{
53public:
54 using type = JSONObject;
55 using is_visitor_tag = std::integral_constant<bool, true>;
56
57 JSONReader();
58 JSONReader(const JSONReader&) = delete;
59 JSONReader& operator=(const JSONReader&) = delete;
60 JSONReader(JSONReader&&) = default;
61 JSONReader& operator=(JSONReader&&) = delete;
62
63 VisitorVariant toVariant() { return {*this, JSONObject::type()}; }
64
65 template <typename T>
66 static auto marshall(const T& t)
67 {
68 JSONReader reader;
69 reader.readFrom(t);
70 return reader;
71 }
72
73 bool empty() const noexcept { return this->buffer.GetLength() == 0; }
74
75 void read(const QString&) const noexcept = delete;
76 void read(const float&) const noexcept = delete;
77 void read(const char&) const noexcept = delete;
78 void read(const int&) const noexcept = delete;
79 void read(const bool&) const noexcept = delete;
80 void read(const std::string&) const noexcept = delete;
81 void read(const unsigned int&) const noexcept = delete;
82 void read(const unsigned char&) const noexcept = delete;
83
86 template <typename T>
87 void readFrom(const score::Entity<T>& obj);
88
89 template <typename T>
90 void readFrom(const IdentifiedObject<T>& obj);
91
92 void readFrom(const QString& obj) noexcept { readFrom(obj.toUtf8()); }
93 void readFrom(const QByteArray& t) noexcept { stream.String(t.data(), t.size()); }
94 void readFrom(const std::string& t) noexcept { stream.String(t.data(), t.size()); }
95 void readFrom(int64_t t) noexcept { stream.Int64(t); }
96 void readFrom(int32_t t) noexcept { stream.Int(t); }
97 void readFrom(uint64_t t) noexcept { stream.Uint64(t); }
98 void readFrom(uint32_t t) noexcept { stream.Uint(t); }
99 void readFrom(float t) noexcept { stream.Double(t); }
100 void readFrom(double t) noexcept { stream.Double(t); }
101 void readFrom(bool t) noexcept { stream.Bool(t); }
102 void readFrom(char t) noexcept { stream.String(&t, 1); }
103 void readFrom(const void*) noexcept = delete;
104
105 template <typename T>
106 void readFrom(const T& obj)
107 {
108 static constexpr bool has_base = base_kind<T>::value;
109
110 if constexpr(has_base)
111 {
112 readFrom((const typename T::score_base_type&)obj);
113 }
114 else if constexpr(std::is_enum_v<T>)
115 {
117 Q_UNUSED(_);
118 stream.Int(static_cast<int32_t>(obj));
119 }
120 else
121 {
122 if constexpr(
123 (is_template<T>::value && !abstract_base<T> && !identified_object<T>)
124 || is_custom_serialized<T>::value)
125 {
127 }
128 else if constexpr(
130 && !is_custom_serialized<T>::value)
131 {
132 stream.StartObject();
134
135 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
137 else
138 read(obj);
139
140 stream.EndObject();
141 }
142 else if constexpr(
143 identified_entity<T> && !abstract_base<T> && !is_custom_serialized<T>::value)
144 {
145 stream.StartObject();
147
148 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
150 else
151 read(obj);
152 stream.EndObject();
153 }
154 else if constexpr(
155 !identified_object<T> && abstract_base<T> && !is_custom_serialized<T>::value)
156 {
157 stream.StartObject();
158 readFromAbstract(obj, [](JSONReader& sub, const T& obj) {
159 // Read the implementation of the base object
160 sub.read(obj);
161 });
162 stream.EndObject();
163 }
164 else if constexpr(
166 && !is_custom_serialized<T>::value)
167 {
168 stream.StartObject();
169 readFromAbstract(obj, [](JSONReader& sub, const T& obj) {
171
172 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
174 else
175 sub.read(obj);
176 });
177 stream.EndObject();
178 }
179 else if constexpr(
180 identified_entity<T> && abstract_base<T> && !is_custom_serialized<T>::value)
181 {
182 stream.StartObject();
183 readFromAbstract(obj, [](JSONReader& sub, const T& obj) {
184 TSerializer<JSONObject, score::Entity<T>>::readFrom(sub, obj);
185
186 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
188 else
189 sub.read(obj);
190 });
191 stream.EndObject();
192 }
193 else
194 {
197 read(obj);
198 }
199 }
200 }
201
202 rapidjson::StringBuffer buffer;
203 JsonWriter stream{buffer};
204 struct assigner;
205 struct fake_obj
206 {
207 JSONReader& self;
208 assigner operator[](std::string_view str) const noexcept;
209 template <std::size_t N>
210 assigner operator[](const char (&str)[N]) const noexcept;
211 assigner operator[](const QString& str) const noexcept;
212 } obj;
213
214 const score::ApplicationComponents& components;
215 const score::StringConstants& strings;
216
217 QByteArray toByteArray() const
218 {
219 SCORE_ASSERT(stream.IsComplete());
220 return QByteArray{buffer.GetString(), (int)buffer.GetLength()};
221 }
222 std::string toStdString() const
223 {
224 SCORE_ASSERT(stream.IsComplete());
225 return std::string{buffer.GetString(), buffer.GetLength()};
226 }
227 QString toString() const
228 {
229 SCORE_ASSERT(stream.IsComplete());
230 return QString::fromUtf8(buffer.GetString(), buffer.GetLength());
231 }
232
235 template <typename T>
236 void read(const T&);
237
238private:
239 template <typename T, typename Fun>
240 void readFromAbstract(const T& in, Fun f);
241};
242
244{
245 JSONReader& self;
246
247 void operator=(int64_t t) const noexcept { self.stream.Int64(t); }
248 void operator=(int32_t t) const noexcept { self.stream.Int(t); }
249 void operator=(uint64_t t) const noexcept { self.stream.Uint64(t); }
250 void operator=(uint32_t t) const noexcept { self.stream.Uint(t); }
251 void operator=(float t) const noexcept { self.stream.Double(t); }
252 void operator=(double t) const noexcept { self.stream.Double(t); }
253 void operator=(bool t) const noexcept { self.stream.Bool(t); }
254 void operator=(char t) const noexcept { self.stream.String(&t, 1); }
255 void operator=(QPoint t) const noexcept
256 {
257 self.stream.StartArray();
258 self.stream.Int(t.x());
259 self.stream.Int(t.y());
260 self.stream.EndArray();
261 }
262 void operator=(QPointF t) const noexcept
263 {
264 self.stream.StartArray();
265 self.stream.Double(t.x());
266 self.stream.Double(t.y());
267 self.stream.EndArray();
268 }
269 void operator=(QSize t) const noexcept
270 {
271 self.stream.StartArray();
272 self.stream.Int(t.width());
273 self.stream.Int(t.height());
274 self.stream.EndArray();
275 }
276 void operator=(QSizeF t) const noexcept
277 {
278 self.stream.StartArray();
279 self.stream.Double(t.width());
280 self.stream.Double(t.height());
281 self.stream.EndArray();
282 }
283 void operator=(QRect t) const noexcept
284 {
285 self.stream.StartArray();
286 self.stream.Int(t.x());
287 self.stream.Int(t.y());
288 self.stream.Int(t.width());
289 self.stream.Int(t.height());
290 self.stream.EndArray();
291 }
292 void operator=(QRectF t) const noexcept
293 {
294 self.stream.StartArray();
295 self.stream.Double(t.x());
296 self.stream.Double(t.y());
297 self.stream.Double(t.width());
298 self.stream.Double(t.height());
299 self.stream.EndArray();
300 }
301 void operator=(const QString& t) const noexcept { *this = t.toUtf8(); }
302 void operator=(const QStringList& t) const noexcept
303 {
304 self.stream.StartArray();
305 for(const auto& str : t)
306 *this = str.toUtf8();
307 self.stream.EndArray();
308 }
309 void operator=(const QLatin1String& t) const noexcept
310 {
311 self.stream.String(t.data(), t.size());
312 }
313 void operator=(const std::string& t) const noexcept
314 {
315 self.stream.String(t.data(), t.length());
316 }
317 void operator=(const std::string_view& t) const noexcept
318 {
319 self.stream.String(t.data(), t.length());
320 }
321 void operator=(const QByteArray& t) const noexcept
322 {
323 self.stream.String(t.data(), t.length());
324 }
325 void operator=(const QVariantMap& t) const noexcept { SCORE_ABORT; }
326
327 template <typename T>
328 void operator=(const T& t) const noexcept
329 {
330 if constexpr(std::is_enum_v<T>)
331 {
333 Q_UNUSED(_);
334 self.stream.Int(static_cast<int32_t>(t));
335 }
336 else
337 {
338 self.readFrom(t);
339 }
340 }
341};
342
344JSONReader::fake_obj::operator[](std::string_view str) const noexcept
345{
346 self.stream.Key(str.data(), str.length());
347 return assigner{self};
348}
350JSONReader::fake_obj::operator[](const QString& str) const noexcept
351{
352 const std::string& s = str.toStdString();
353 self.stream.Key(s.data(), s.length());
354 return assigner{self};
355}
356template <std::size_t N>
358JSONReader::fake_obj::operator[](const char (&str)[N]) const noexcept
359{
360 return (*this)[std::string_view(str, N - 1)];
361}
362
363template <typename T, typename Fun>
364void JSONReader::readFromAbstract(const T& in, Fun f)
365{
366 obj[strings.uuid] = in.concreteKey().impl();
367 f(*this, in);
368 in.serialize_impl(this->toVariant());
369}
370
372{
373 const rapidjson::Value& obj;
374 QString toString() const noexcept
375 {
376 return QString::fromUtf8(obj.GetString(), obj.GetStringLength());
377 }
378 std::string toStdString() const noexcept
379 {
380 return std::string(obj.GetString(), obj.GetStringLength());
381 }
382 QByteArray toByteArray() const noexcept
383 {
384 return QByteArray(obj.GetString(), obj.GetStringLength());
385 }
386
387 int32_t toInt() const noexcept { return obj.GetInt(); }
388 bool toBool() const noexcept { return obj.GetBool(); }
389 double toDouble() const noexcept { return obj.GetDouble(); }
390 int64_t toInt64() const noexcept { return obj.GetInt64(); }
391 uint64_t toUInt64() const noexcept { return obj.GetUint64(); }
392 bool isDouble() const noexcept { return obj.IsDouble(); }
393 auto toArray() const noexcept { return obj.GetArray(); }
394 auto toObject() const noexcept { return obj.GetObject(); }
395 bool isString() const noexcept { return obj.IsString(); }
396
397 template <std::size_t N>
398 JsonValue operator[](const char (&str)[N]) const noexcept
399 {
400 return JsonValue{obj[str]};
401 }
402 JsonValue operator[](const std::string& str) const noexcept
403 {
404 return JsonValue{obj[str]};
405 }
406 JsonValue operator[](const QString& str) const noexcept
407 {
408 return (*this)[str.toStdString()];
409 }
410 template <typename T>
411 friend void operator<<=(T& t, const JsonValue& self);
412
413 template <typename T>
414 T to() const noexcept
415 {
416 T t;
417 t <<= *this;
418 return t;
419 }
420};
421
422class SCORE_LIB_BASE_EXPORT JSONWriter : public AbstractVisitor
423{
424public:
425 using type = JSONObject;
426 using is_visitor_tag = std::integral_constant<bool, true>;
427 using is_deserializer_tag = std::integral_constant<bool, true>;
428
429 VisitorVariant toVariant() { return {*this, JSONObject::type()}; }
430
431 JSONWriter() = delete;
432 JSONWriter(const JSONWriter&) = delete;
433 JSONWriter& operator=(const JSONWriter&) = delete;
434
435 explicit JSONWriter(const rapidjson::Value& obj);
436 explicit JSONWriter(const JsonValue& obj);
437
438 template <typename T>
439 static auto unmarshall(const rapidjson::Value& obj)
440 {
441 T data;
442 JSONWriter wrt{obj};
443 wrt.writeTo(data);
444 return data;
445 }
446
447 template <typename T>
448 void write(T&);
449
450 void write(QString&) = delete;
451 void write(float&) = delete;
452 void write(char&) = delete;
453 void write(int&) = delete;
454 void write(bool&) = delete;
455 void write(std::string&) = delete;
456
457 template <typename T>
458 void writeTo(T& obj)
459 {
460 if constexpr(
461 (is_template<T>::value && !abstract_base<T> && !identified_object<T>)
462 || is_custom_serialized<T>::value)
463 {
465 }
466 else if constexpr(std::is_enum<T>::value)
467 {
468 obj = static_cast<T>(base.GetInt());
469 }
470 else
471 {
472 write(obj);
473 }
474 }
475
476 const rapidjson::Value& base;
477
478 struct wrapper
479 {
480 const rapidjson::Value& ref;
481 template <std::size_t N>
482 JsonValue operator[](const char (&str)[N]) const noexcept
483 {
484 return JsonValue{ref[str]};
485 }
486 JsonValue operator[](const std::string& str) const noexcept
487 {
488 return JsonValue{ref[str]};
489 }
490 JsonValue operator[](const QString& str) const noexcept
491 {
492 return (*this)[str.toStdString()];
493 }
494 template <std::size_t N>
495 std::optional<JsonValue> tryGet(const char (&str)[N]) const noexcept
496 {
497 if(auto it = ref.FindMember(str); it != ref.MemberEnd())
498 return JsonValue{it->value};
499 return std::nullopt;
500 }
501 std::optional<JsonValue> tryGet(const std::string& str) const noexcept
502 {
503 if(auto it = ref.FindMember(str); it != ref.MemberEnd())
504 return JsonValue{it->value};
505 return std::nullopt;
506 }
507 std::optional<JsonValue> tryGet(const QString& str) const noexcept
508 {
509 return tryGet(str.toStdString());
510 }
511 std::optional<JsonValue> constFind(const std::string& str) const noexcept
512 {
513 return tryGet(str);
514 }
515 std::optional<JsonValue> constFind(const QString& str) const noexcept
516 {
517 return tryGet(str.toStdString());
518 }
519 auto constEnd() const noexcept { return OptionalSentinel{}; }
520 } obj{base};
521
522 const score::ApplicationComponents& components;
523 const score::StringConstants& strings;
524};
525
526template <typename T>
527inline void operator<<=(T& t, const JsonValue& self)
528{
529 JSONWriter w{self.obj};
530 w.writeTo(t);
531}
532
533inline void operator<<=(QString& t, const JsonValue& self)
534{
535 t = self.toString();
536}
537inline void operator<<=(float& t, const JsonValue& self)
538{
539 t = self.obj.GetFloat();
540}
541inline void operator<<=(double& t, const JsonValue& self)
542{
543 t = self.obj.GetDouble();
544}
545inline void operator<<=(int& t, const JsonValue& self)
546{
547 t = self.obj.GetInt();
548}
549inline void operator<<=(int64_t& t, const JsonValue& self)
550{
551 t = self.obj.GetInt();
552}
553inline void operator<<=(std::string& t, const JsonValue& self)
554{
555 t = self.toStdString();
556}
557inline void operator<<=(QByteArray& t, const JsonValue& self)
558{
559 t = self.toByteArray();
560}
561inline void operator<<=(bool& t, const JsonValue& self)
562{
563 t = self.toBool();
564}
565inline void operator<<=(char& t, const JsonValue& self)
566{
567 t = self.obj.GetStringLength() > 0 ? self.obj.GetString()[0] : '\0';
568}
569
570template <typename T>
572{
573 template <typename U>
574 static void readFrom(JSONObject::Serializer& s, const IdentifiedObject<U>& obj)
575 {
576 s.obj[s.strings.ObjectName] = obj.objectName();
577 s.obj[s.strings.id] = obj.id().val();
578 }
579
580 template <typename U>
581 static void writeTo(JSONObject::Deserializer& s, IdentifiedObject<U>& obj)
582 {
583 obj.setObjectName(s.obj[s.strings.ObjectName].toString());
584 obj.setId(Id<T>{s.obj[s.strings.id].toInt()});
585 }
586};
587
588Q_DECLARE_METATYPE(JSONReader*)
589Q_DECLARE_METATYPE(JSONWriter*)
590W_REGISTER_ARGTYPE(JSONReader*)
591W_REGISTER_ARGTYPE(JSONWriter*)
592
594{
595 template <typename T>
596 static void readFrom(JSONObject::Serializer& s, const T& vec)
597 {
598 s.stream.StartArray();
599 for(const auto& elt : vec)
600 s.readFrom(elt);
601 s.stream.EndArray();
602 }
603
604 template <typename T>
605 static void writeTo(JSONObject::Deserializer& s, T& vec)
606 {
607 const auto& array = s.base.GetArray();
608
609 vec.clear();
610 vec.reserve(array.Size());
611 for(const auto& elt : array)
612 {
613 typename T::value_type v;
615 des.writeTo(v);
616 vec.push_back(std::move(v));
617 }
618 }
619
620 template <template <typename, typename...> typename T, typename Arg, typename... Args>
621 static void readFrom(JSONObject::Serializer& s, const T<Arg, Args...>& vec)
622 {
623 using arg_type = std::remove_cvref_t<Arg>;
624 s.stream.StartArray();
625 for(const auto& elt : vec)
626 {
627 if constexpr(std::is_floating_point_v<arg_type>)
628 s.stream.Double(elt);
629 else if constexpr(std::is_same_v<arg_type, char>)
630 s.stream.String(&elt, 1);
631 else if constexpr(std::is_integral_v<arg_type>)
632 {
633 if constexpr(sizeof(arg_type) > 4)
634 s.stream.Int64(elt);
635 else
636 s.stream.Int(elt);
637 }
638 else if constexpr(std::is_same_v<arg_type, std::string>)
639 s.stream.String(elt.data(), elt.size());
640 else if constexpr(std::is_same_v<arg_type, QString>)
641 {
642 const QByteArray& b = elt.toUtf8();
643 s.stream.String(b.data(), b.size());
644 }
645 else
646 s.readFrom(elt);
647 }
648 s.stream.EndArray();
649 }
650
651 template <
652 template <typename, std::size_t, typename...> typename T, typename Arg,
653 std::size_t N, typename... Args>
654 static void writeTo(JSONObject::Deserializer& s, T<Arg, N, Args...>& vec)
655 {
656 using type = T<Arg, N, Args...>;
657 using arg_type = std::remove_cvref_t<Arg>;
658 const auto& array = s.base.GetArray();
659 if constexpr(std::is_aggregate_v<type>)
660 {
661 SCORE_ASSERT(N >= array.Size());
662 }
663 else
664 {
665 vec.clear();
666 vec.resize(array.Size());
667 }
668
669 auto it = vec.begin();
670 for(const auto& elt : array)
671 {
672 if constexpr(std::is_floating_point_v<arg_type>)
673 *it = elt.GetDouble();
674 else if constexpr(std::is_same_v<arg_type, char>)
675 {
676 SCORE_ASSERT(elt.IsString());
677 if(elt.GetStringLength() == 1)
678 *it = elt.GetString()[0];
679 else
680 *it = 0;
681 }
682 else if constexpr(std::is_integral_v<arg_type>)
683 {
684 if constexpr(sizeof(arg_type) > 4)
685 {
686 if(elt.IsInt64())
687 *it = elt.GetInt64();
688 else
689 *it = elt.GetInt();
690 }
691 else
692 *it = elt.GetInt();
693 }
694 else if constexpr(std::is_same_v<arg_type, std::string>)
695 *it = std::string{elt.GetString(), elt.GetStringLength()};
696 else if constexpr(std::is_same_v<arg_type, QString>)
697 {
698 *it = QString::fromUtf8(elt.GetString(), elt.GetStringLength());
699 }
700 else
701 {
703 des.writeTo(*it);
704 }
705 ++it;
706 }
707 }
708
709 template <
710 template <typename, typename, typename...> typename T, typename Arg, typename Arg2,
711 typename... Args>
712 static void writeTo(JSONObject::Deserializer& s, T<Arg, Arg2, Args...>& vec)
713 {
714 using type = T<Arg, Arg2, Args...>;
715 using arg_type = std::remove_cvref_t<Arg>;
716 const auto& array = s.base.GetArray();
717
718 vec.clear();
719 vec.resize(array.Size());
720
721 auto it = vec.begin();
722 for(const auto& elt : array)
723 {
724 if constexpr(std::is_floating_point_v<arg_type>)
725 *it = elt.GetDouble();
726 else if constexpr(std::is_same_v<arg_type, char>)
727 {
728 SCORE_ASSERT(elt.IsString());
729 if(elt.GetStringLength() == 1)
730 *it = elt.GetString()[0];
731 else
732 *it = 0;
733 }
734 else if constexpr(std::is_integral_v<arg_type>)
735 {
736 if constexpr(sizeof(arg_type) > 4)
737 *it = elt.GetInt64();
738 else
739 *it = elt.GetInt();
740 }
741 else if constexpr(std::is_same_v<arg_type, std::string>)
742 *it = std::string{elt.GetString(), elt.GetStringLength()};
743 else if constexpr(std::is_same_v<arg_type, QString>)
744 {
745 *it = QString::fromUtf8(elt.GetString(), elt.GetStringLength());
746 }
747 else
748 {
750 des.writeTo(*it);
751 }
752 ++it;
753 }
754 }
755
756 template <typename T>
757 static void readFrom(JSONObject::Serializer& s, const std::list<T>& vec)
758 {
759 s.stream.StartArray();
760 for(const auto& elt : vec)
761 s.readFrom(elt);
762 s.stream.EndArray();
763 }
764
765 template <typename T>
766 static void writeTo(JSONObject::Deserializer& s, std::list<T>& vec)
767 {
768 vec.clear();
769
770 const auto& array = s.base.GetArray();
771 for(const auto& elt : array)
772 {
773 T v;
775 des.writeTo(v);
776 vec.push_back(std::move(v));
777 }
778 }
779
780 // REMOVEME
781 template <typename T>
782 static void readFrom(JSONObject::Serializer& s, const QList<T>& vec)
783 {
784 s.stream.StartArray();
785 for(const auto& elt : vec)
786 s.readFrom(elt);
787 s.stream.EndArray();
788 }
789
790 template <typename T>
791 static void writeTo(JSONObject::Deserializer& s, QList<T>& vec)
792 {
793 const auto& array = s.base.GetArray();
794 vec.clear();
795 vec.reserve(array.Size());
796
797 for(const auto& elt : array)
798 {
799 T v;
801 des.writeTo(v);
802 vec.push_back(std::move(v));
803 }
804 }
805};
806
807template <typename... Args>
808struct TSerializer<JSONObject, boost::container::vector<Args...>> : ArraySerializer
809{
810};
811
812template <typename... Args>
813struct TSerializer<JSONObject, std::vector<Args...>> : ArraySerializer
814{
815};
816
817template <typename... Args>
818struct TSerializer<JSONObject, std::list<Args...>> : ArraySerializer
819{
820};
821
822template <typename... Args>
823struct TSerializer<JSONObject, QList<Args...>> : ArraySerializer
824{
825};
826
827template <typename T, std::size_t N, typename Alloc>
828struct TSerializer<JSONObject, boost::container::small_vector<T, N, Alloc>>
830{
831};
832
833template <typename T, std::size_t N>
834struct TSerializer<JSONObject, boost::container::static_vector<T, N>> : ArraySerializer
835{
836};
837
838template <typename T, std::size_t N>
840{
841};
842
843template <std::size_t N>
844struct TSerializer<JSONObject, std::array<float, N>> : ArraySerializer
845{
846};
847
848template <typename T, typename U, bool O>
850{
851};
852
853template <typename T>
854struct TSerializer<JSONObject, std::optional<T>>
855{
856 static void readFrom(JSONObject::Serializer& s, const std::optional<T>& obj)
857 {
858 if(obj)
859 s.readFrom(*obj);
860 else
861 s.stream.Null();
862 }
863
864 static void writeTo(JSONObject::Deserializer& s, std::optional<T>& obj)
865 {
866 if(s.base.IsNull())
867 {
868 obj = std::nullopt;
869 }
870 else
871 {
872 T t;
873 t <<= JsonValue{s.base};
874 obj = std::move(t);
875 }
876 }
877};
878
879template <typename T, typename U>
880struct TSerializer<JSONObject, std::pair<T, U>>
881{
882 using type = std::pair<T, U>;
883 static void readFrom(JSONObject::Serializer& s, const type& obj)
884 {
885 s.stream.StartArray();
886 s.readFrom(obj.first);
887 s.readFrom(obj.second);
888 s.stream.EndArray();
889 }
890
891 static void writeTo(JSONObject::Deserializer& s, type& obj)
892 {
893 const auto& arr = s.base.GetArray();
894 obj.first <<= JsonValue{arr[0]};
895 obj.second <<= JsonValue{arr[1]};
896 }
897};
898
899template <>
900struct TSerializer<JSONObject, QVariantMap>
901{
902 using type = QVariantMap;
903 static void readFrom(JSONObject::Serializer& s, const type& obj) = delete;
904 // {
905 // SCORE_ABORT;
906 // /*
907 // QJsonArray arr;
908 // arr.append(toJsonValue(obj.first));
909 // arr.append(toJsonValue(obj.second));
910 // s.val = std::move(arr);
911 // */
912 // }
913
914 static void writeTo(JSONObject::Deserializer& s, type& obj) = delete;
915 // {
916 // SCORE_ABORT;
917 // /*
918 // const auto arr = s.val.toArray();
919 // obj.first = fromJsonValue<T>(arr[0]);
920 // obj.second = fromJsonValue<U>(arr[1]);
921 // */
922 // }
923};
924
925template <typename T>
926struct TSerializer<JSONObject, ossia::flat_set<T>>
927{
928 using type = ossia::flat_set<T>;
929 static void readFrom(JSONObject::Serializer& s, const type& obj)
930 {
931 ArraySerializer::readFrom(s, obj.tree().get_sequence_cref());
932 }
933
934 static void writeTo(JSONObject::Deserializer& s, type& obj)
935 {
936 ArraySerializer::writeTo(s, obj.tree().get_sequence_ref());
937 }
938};
939
940template <>
941struct TSerializer<JSONObject, QColor>
942{
943 static void readFrom(JSONObject::Serializer& s, QColor c)
944 {
945 const auto col = c.rgba64();
946 s.stream.StartArray();
947 s.stream.Int(col.red());
948 s.stream.Int(col.green());
949 s.stream.Int(col.blue());
950 s.stream.Int(col.alpha());
951 s.stream.EndArray();
952 }
953
954 static void writeTo(JSONObject::Deserializer& s, QColor& c)
955 {
956 const auto& array = s.base.GetArray();
957 QRgba64 col;
958 col.setRed(array[0].GetInt());
959 col.setGreen(array[1].GetInt());
960 col.setBlue(array[2].GetInt());
961 col.setAlpha(array[3].GetInt());
962 c = col;
963 }
964};
965
966template <>
967struct TSerializer<JSONObject, QPoint>
968{
969 static void readFrom(JSONObject::Serializer& s, QPoint c)
970 {
971 s.stream.StartArray();
972 s.stream.Int(c.x());
973 s.stream.Int(c.y());
974 s.stream.EndArray();
975 }
976
977 static void writeTo(JSONObject::Deserializer& s, QPoint& c)
978 {
979 const auto& array = s.base.GetArray();
980 c.setX(array[0].GetInt());
981 c.setY(array[1].GetInt());
982 }
983};
984
985template <>
986struct TSerializer<JSONObject, QPointF>
987{
988 static void readFrom(JSONObject::Serializer& s, QPointF c)
989 {
990 s.stream.StartArray();
991 s.stream.Double(c.x());
992 s.stream.Double(c.y());
993 s.stream.EndArray();
994 }
995
996 static void writeTo(JSONObject::Deserializer& s, QPointF& c)
997 {
998 const auto& array = s.base.GetArray();
999 c.setX(array[0].GetDouble());
1000 c.setY(array[1].GetDouble());
1001 }
1002};
1003
1004template <>
1006{
1007 static void readFrom(JSONObject::Serializer& s, QSize c)
1008 {
1009 s.stream.StartArray();
1010 s.stream.Int(c.width());
1011 s.stream.Int(c.height());
1012 s.stream.EndArray();
1013 }
1014
1015 static void writeTo(JSONObject::Deserializer& s, QSize& c)
1016 {
1017 const auto& array = s.base.GetArray();
1018 c.setWidth(array[0].GetInt());
1019 c.setHeight(array[1].GetInt());
1020 }
1021};
1022
1023template <>
1025{
1026 static void readFrom(JSONObject::Serializer& s, QSizeF c)
1027 {
1028 s.stream.StartArray();
1029 s.stream.Double(c.width());
1030 s.stream.Double(c.height());
1031 s.stream.EndArray();
1032 }
1033
1034 static void writeTo(JSONObject::Deserializer& s, QSizeF& c)
1035 {
1036 const auto& array = s.base.GetArray();
1037 c.setWidth(array[0].GetDouble());
1038 c.setHeight(array[1].GetDouble());
1039 }
1040};
1041
1042template <>
1044{
1045 static void readFrom(JSONObject::Serializer& s, QRect c)
1046 {
1047 s.stream.StartArray();
1048 s.stream.Int(c.x());
1049 s.stream.Int(c.y());
1050 s.stream.Int(c.width());
1051 s.stream.Int(c.height());
1052 s.stream.EndArray();
1053 }
1054
1055 static void writeTo(JSONObject::Deserializer& s, QRect& c)
1056 {
1057 const auto& array = s.base.GetArray();
1058 c.setX(array[0].GetInt());
1059 c.setY(array[1].GetInt());
1060 c.setWidth(array[2].GetInt());
1061 c.setHeight(array[3].GetInt());
1062 }
1063};
1064
1065template <>
1067{
1068 static void readFrom(JSONObject::Serializer& s, QRectF c)
1069 {
1070 s.stream.StartArray();
1071 s.stream.Double(c.x());
1072 s.stream.Double(c.y());
1073 s.stream.Double(c.width());
1074 s.stream.Double(c.height());
1075 s.stream.EndArray();
1076 }
1077
1078 static void writeTo(JSONObject::Deserializer& s, QRectF& c)
1079 {
1080 const auto& array = s.base.GetArray();
1081 c.setX(array[0].GetDouble());
1082 c.setY(array[1].GetDouble());
1083 c.setWidth(array[2].GetDouble());
1084 c.setHeight(array[3].GetDouble());
1085 }
1086};
1087template <typename T>
1089{
1090 using type = Id<T>;
1091 static void readFrom(JSONObject::Serializer& s, const type& obj)
1092 {
1093 s.stream.Int64(obj.val());
1094 }
1095
1096 static void writeTo(JSONObject::Deserializer& s, type& obj)
1097 {
1098 obj.setVal(s.base.GetInt64());
1099 }
1100};
1101
1102template <>
1103struct SCORE_LIB_BASE_EXPORT TSerializer<DataStream, rapidjson::Document>
1104{
1105 static void readFrom(DataStream::Serializer& s, const rapidjson::Document& obj);
1106 static void writeTo(DataStream::Deserializer& s, rapidjson::Document& obj);
1107};
1108template <>
1109struct SCORE_LIB_BASE_EXPORT TSerializer<DataStream, rapidjson::Value>
1110{
1111 static void readFrom(DataStream::Serializer& s, rapidjson::Value& obj) = delete;
1112 static void writeTo(DataStream::Deserializer& s, rapidjson::Value& obj) = delete;
1113};
1114
1115namespace Process
1116{
1117class Inlet;
1118}
1119template <>
1120void JSONReader::read<Process::Inlet*>(Process::Inlet* const&) = delete;
1121
1122SCORE_LIB_BASE_EXPORT
1123rapidjson::Document clone(const rapidjson::Value& val) noexcept;
1124
1125SCORE_LIB_BASE_EXPORT
1126rapidjson::Document readJson(const QByteArray& arr);
1127
1128inline QByteArray jsonToByteArray(const rapidjson::Value& arr) noexcept
1129{
1130 rapidjson::StringBuffer buf;
1131 buf.Reserve(8192);
1132 JsonWriter w{buf};
1133 arr.Accept(w);
1134 return QByteArray(buf.GetString(), buf.GetSize());
1135}
1136
1137SCORE_LIB_BASE_EXPORT
1138rapidjson::Document toValue(const JSONReader&) noexcept;
1139
1140template <typename T>
1141T fromJson(const QByteArray& rawData)
1142{
1143 const rapidjson::Document doc = readJson(rawData);
1144 JSONWriter wr{doc};
1145 T t;
1146 wr.writeTo(t);
1147 return t;
1148}
1149
1150template <typename T>
1151QByteArray toJson(const T& t)
1152{
1153 JSONReader reader;
1154 reader.readFrom(t);
1155 return reader.toByteArray();
1156}
1157
1158namespace score
1159{
1160
1161template <typename Object>
1162auto unmarshall(const JSONReader& obj)
1163{
1164 Object data;
1165 const auto doc = toValue(obj);
1166 JSONWriter wrt{doc};
1167 wrt.writeTo(data);
1168 return data;
1169}
1170
1171}
1172/*
1173template<typename T>
1174struct optional_assigner {
1175 T& lhs;
1176 bool set_default = false;
1177 template<typename R>
1178 void operator||(R&& rhs) const noexcept
1179 {
1180 if(set_default)
1181 lhs = std::forward<R>(rhs);
1182 }
1183};
1184template <typename T>
1185inline auto operator<<=(T& t, const std::optional<JsonValue>& self)
1186{
1187 if(self)
1188 {
1189 JSONWriter w{self->obj};
1190 w.writeTo(t);
1191 return optional_assigner<T>{t, false};
1192 }
1193 else
1194 {
1195 return optional_assigner<T>{t, true};
1196 }
1197}
1198*/
1199
1200#define assign_with_default(member, optional, alt_value) \
1201 do \
1202 { \
1203 if(auto it = optional) \
1204 member <<= *it; \
1205 else \
1206 member = alt_value; \
1207 } while(0)
Definition VisitorInterface.hpp:10
Definition VisitorInterface.hpp:53
Definition DataStreamVisitor.hpp:27
Definition DataStreamVisitor.hpp:202
A map to access child objects through their id.
Definition IdentifiedObjectMap.hpp:16
The IdentifiedObject class.
Definition IdentifiedObject.hpp:19
Definition VisitorInterface.hpp:61
Definition JSONVisitor.hpp:52
void readFrom(const T &obj)
Definition JSONVisitor.hpp:106
void readFrom(const score::Entity< T > &obj)
Definition EntitySerialization.hpp:7
void read(const T &)
Definition JSONVisitor.hpp:423
Definition Port.hpp:177
The id_base_t class.
Definition Identifier.hpp:57
Definition ApplicationComponents.hpp:68
Base for complex model objects.
Definition EntityBase.hpp:24
Definition VisitorTags.hpp:10
Definition VisitorTags.hpp:18
Definition VisitorTags.hpp:14
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1116
Base toolkit upon which the software is built.
Definition Application.cpp:90
STL namespace.
Definition JSONVisitor.hpp:594
Definition JSONVisitor.hpp:244
Definition JSONVisitor.hpp:206
Definition JSONVisitor.hpp:479
Definition JSONVisitor.hpp:372
Definition JSONVisitor.hpp:37
Definition VisitorInterface.hpp:13
The VisitorVariant struct.
Definition VisitorInterface.hpp:26
Definition VisitorTags.hpp:29
Definition VisitorTags.hpp:152
Definition VisitorTags.hpp:24
Definition StringConstants.hpp:10