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 <typename Arg, std::size_t N>
621 static void readFrom(JSONObject::Serializer& s, const std::array<Arg, N>& vec)
622 {
623 s.stream.StartArray();
624 for(const auto& elt : vec)
625 s.readFrom(elt);
626 s.stream.EndArray();
627 }
628
629 template <typename Arg, std::size_t N>
630 static void writeTo(JSONObject::Deserializer& s, std::array<Arg, N>& vec)
631 {
632 const auto& array = s.base.GetArray();
633 SCORE_ASSERT(N >= array.Size());
634
635 auto it = vec.begin();
636 for(const auto& elt : array)
637 {
639 des.writeTo(*it);
640 ++it;
641 }
642 }
643
644 template <std::size_t N>
645 static void readFrom(JSONObject::Serializer& s, const std::array<float, N>& vec)
646 {
647 s.stream.StartArray();
648 for(auto elt : vec)
649 s.stream.Double(elt);
650 s.stream.EndArray();
651 }
652
653 template <std::size_t N>
654 static void writeTo(JSONObject::Deserializer& s, std::array<float, N>& vec)
655 {
656 const auto& array = s.base.GetArray();
657 SCORE_ASSERT(N >= array.Size());
658
659 auto it = vec.begin();
660 for(const auto& elt : array)
661 {
662 *it = elt.GetFloat();
663 ++it;
664 }
665 }
666
667 template <typename T>
668 static void readFrom(JSONObject::Serializer& s, const std::list<T>& vec)
669 {
670 s.stream.StartArray();
671 for(const auto& elt : vec)
672 s.readFrom(elt);
673 s.stream.EndArray();
674 }
675
676 template <typename T>
677 static void writeTo(JSONObject::Deserializer& s, std::list<T>& vec)
678 {
679 vec.clear();
680
681 const auto& array = s.base.GetArray();
682 for(const auto& elt : array)
683 {
684 T v;
686 des.writeTo(v);
687 vec.push_back(std::move(v));
688 }
689 }
690
691 // REMOVEME
692 template <typename T>
693 static void readFrom(JSONObject::Serializer& s, const QList<T>& vec)
694 {
695 s.stream.StartArray();
696 for(const auto& elt : vec)
697 s.readFrom(elt);
698 s.stream.EndArray();
699 }
700
701 template <typename T>
702 static void writeTo(JSONObject::Deserializer& s, QList<T>& vec)
703 {
704 const auto& array = s.base.GetArray();
705 vec.clear();
706 vec.reserve(array.Size());
707
708 for(const auto& elt : array)
709 {
710 T v;
712 des.writeTo(v);
713 vec.push_back(std::move(v));
714 }
715 }
716
717 template <template <typename... Args> typename T, typename... Args>
718 static void readFrom(JSONObject::Serializer& s, const T<std::string, Args...>& vec)
719 {
720 s.stream.StartArray();
721 for(const auto& elt : vec)
722 s.stream.String(elt.data(), elt.size());
723 s.stream.EndArray();
724 }
725
726 template <template <typename... Args> typename T, typename... Args>
727 static void writeTo(JSONObject::Deserializer& s, T<std::string, Args...>& vec)
728 {
729 const auto& array = s.base.GetArray();
730 vec.clear();
731 vec.reserve(array.Size());
732 for(const auto& elt : array)
733 {
734 vec.push_back(std::string{elt.GetString(), elt.GetStringLength()});
735 }
736 }
737
738 template <template <typename... Args> typename T, typename... Args>
739 static void readFrom(JSONObject::Serializer& s, const T<QString, Args...>& vec)
740 {
741 s.stream.StartArray();
742 for(const auto& elt : vec)
743 {
744 const QByteArray& b = elt.toUtf8();
745 s.stream.String(b.data(), b.size());
746 }
747 s.stream.EndArray();
748 }
749
750 template <template <typename... Args> typename T, typename... Args>
751 static void writeTo(JSONObject::Deserializer& s, T<QString, Args...>& vec)
752 {
753 const auto& array = s.base.GetArray();
754 vec.clear();
755 vec.reserve(array.Size());
756 for(const auto& elt : array)
757 {
758 vec.push_back(QString::fromUtf8(elt.GetString(), elt.GetStringLength()));
759 }
760 }
761
762 template <template <typename... Args> typename T, typename... Args>
763 static void readFrom(JSONObject::Serializer& s, const T<int, Args...>& vec)
764 {
765 s.stream.StartArray();
766 for(auto elt : vec)
767 s.stream.Int(elt);
768 s.stream.EndArray();
769 }
770
771 template <template <typename... Args> typename T, typename... Args>
772 static void writeTo(JSONObject::Deserializer& s, T<int, Args...>& vec)
773 {
774 const auto& array = s.base.GetArray();
775 vec.clear();
776 vec.resize(array.Size());
777 auto it = vec.begin();
778 for(const auto& elt : array)
779 {
780 *it = elt.GetInt();
781 ++it;
782 }
783 }
784
785 template <template <typename... Args> typename T, typename... Args>
786 static void readFrom(JSONObject::Serializer& s, const T<char, Args...>& vec)
787 {
788 s.stream.StartArray();
789 for(char elt : vec)
790 s.stream.String(&elt, 1);
791 s.stream.EndArray();
792 }
793
794 template <template <typename... Args> typename T, typename... Args>
795 static void writeTo(JSONObject::Deserializer& s, T<char, Args...>& vec)
796 {
797 const auto& array = s.base.GetArray();
798 vec.clear();
799 vec.resize(array.Size());
800 auto it = vec.begin();
801 for(const auto& elt : array)
802 {
803 *it = elt.GetString()[0];
804 ++it;
805 }
806 }
807
808 template <template <typename... Args> typename T, typename... Args>
809 static void readFrom(JSONObject::Serializer& s, const T<int64_t, Args...>& vec)
810 {
811 s.stream.StartArray();
812 for(auto elt : vec)
813 s.stream.Int64(elt);
814 s.stream.EndArray();
815 }
816
817 template <template <typename... Args> typename T, typename... Args>
818 static void writeTo(JSONObject::Deserializer& s, T<int64_t, Args...>& vec)
819 {
820 const auto& array = s.base.GetArray();
821 vec.clear();
822 vec.resize(array.Size());
823 auto it = vec.begin();
824 for(const auto& elt : array)
825 {
826 *it = elt.GetInt64();
827 ++it;
828 }
829 }
830
831 template <template <typename... Args> typename T, typename... Args>
832 static void readFrom(JSONObject::Serializer& s, const T<float, Args...>& vec)
833 {
834 s.stream.StartArray();
835 for(auto elt : vec)
836 s.stream.Double(elt);
837 s.stream.EndArray();
838 }
839
840 template <template <typename... Args> typename T, typename... Args>
841 static void writeTo(JSONObject::Deserializer& s, T<float, Args...>& vec)
842 {
843 const auto& array = s.base.GetArray();
844 vec.clear();
845 vec.resize(array.Size());
846 auto it = vec.begin();
847 for(const auto& elt : array)
848 {
849 *it = elt.GetFloat();
850 ++it;
851 }
852 }
853
854 template <template <typename... Args> typename T, typename... Args>
855 static void readFrom(JSONObject::Serializer& s, const T<double, Args...>& vec)
856 {
857 s.stream.StartArray();
858 for(auto elt : vec)
859 s.stream.Double(elt);
860 s.stream.EndArray();
861 }
862
863 template <template <typename... Args> typename T, typename... Args>
864 static void writeTo(JSONObject::Deserializer& s, T<double, Args...>& vec)
865 {
866 const auto& array = s.base.GetArray();
867 vec.clear();
868 vec.resize(array.Size());
869 auto it = vec.begin();
870 for(const auto& elt : array)
871 {
872 *it = elt.GetDouble();
873 ++it;
874 }
875 }
876
877 // Overloads for supporting static/small vectors... remove when we have
878 // concepts
879 template <
880 template <typename, std::size_t, typename...> typename T, std::size_t N,
881 typename... Args>
882 static void readFrom(JSONObject::Serializer& s, const T<int, N, Args...>& vec)
883 {
884 s.stream.StartArray();
885 for(auto elt : vec)
886 s.stream.Int(elt);
887 s.stream.EndArray();
888 }
889
890 template <
891 template <typename, std::size_t, typename...> typename T, std::size_t N,
892 typename... Args>
893 static void writeTo(JSONObject::Deserializer& s, T<int, N, Args...>& vec)
894 {
895 const auto& array = s.base.GetArray();
896 vec.clear();
897 vec.resize(array.Size());
898 auto it = vec.begin();
899 for(const auto& elt : array)
900 {
901 *it = elt.GetInt();
902 ++it;
903 }
904 }
905
906 template <
907 template <typename, std::size_t, typename...> typename T, std::size_t N,
908 typename... Args>
909 static void readFrom(JSONObject::Serializer& s, const T<char, N, Args...>& vec)
910 {
911 s.stream.StartArray();
912 for(char elt : vec)
913 s.stream.String(&elt, 1);
914 s.stream.EndArray();
915 }
916
917 template <
918 template <typename, std::size_t, typename...> typename T, std::size_t N,
919 typename... Args>
920 static void writeTo(JSONObject::Deserializer& s, T<char, N, Args...>& vec)
921 {
922 const auto& array = s.base.GetArray();
923 vec.clear();
924 vec.resize(array.Size());
925 auto it = vec.begin();
926 for(const auto& elt : array)
927 {
928 *it = elt.GetString()[0];
929 ++it;
930 }
931 }
932
933 template <
934 template <typename, std::size_t, typename...> typename T, std::size_t N,
935 typename... Args>
936 static void readFrom(JSONObject::Serializer& s, const T<int64_t, N, Args...>& vec)
937 {
938 s.stream.StartArray();
939 for(auto elt : vec)
940 s.stream.Int64(elt);
941 s.stream.EndArray();
942 }
943
944 template <
945 template <typename, std::size_t, typename...> typename T, std::size_t N,
946 typename... Args>
947 static void writeTo(JSONObject::Deserializer& s, T<int64_t, N, Args...>& vec)
948 {
949 const auto& array = s.base.GetArray();
950 vec.clear();
951 vec.resize(array.Size());
952 auto it = vec.begin();
953 for(const auto& elt : array)
954 {
955 *it = elt.GetInt64();
956 ++it;
957 }
958 }
959
960 template <
961 template <typename, std::size_t, typename...> typename T, std::size_t N,
962 typename... Args>
963 static void readFrom(JSONObject::Serializer& s, const T<float, N, Args...>& vec)
964 {
965 s.stream.StartArray();
966 for(auto elt : vec)
967 s.stream.Double(elt);
968 s.stream.EndArray();
969 }
970
971 template <
972 template <typename, std::size_t, typename...> typename T, std::size_t N,
973 typename... Args>
974 static void writeTo(JSONObject::Deserializer& s, T<float, N, Args...>& vec)
975 {
976 const auto& array = s.base.GetArray();
977 vec.clear();
978 vec.resize(array.Size());
979 auto it = vec.begin();
980 for(const auto& elt : array)
981 {
982 *it = elt.GetFloat();
983 ++it;
984 }
985 }
986
987 template <
988 template <typename, std::size_t, typename...> typename T, std::size_t N,
989 typename... Args>
990 static void readFrom(JSONObject::Serializer& s, const T<double, N, Args...>& vec)
991 {
992 s.stream.StartArray();
993 for(auto elt : vec)
994 s.stream.Double(elt);
995 s.stream.EndArray();
996 }
997
998 template <
999 template <typename, std::size_t, typename...> typename T, std::size_t N,
1000 typename... Args>
1001 static void writeTo(JSONObject::Deserializer& s, T<double, N, Args...>& vec)
1002 {
1003 const auto& array = s.base.GetArray();
1004 vec.clear();
1005 vec.resize(array.Size());
1006 auto it = vec.begin();
1007 for(const auto& elt : array)
1008 {
1009 *it = elt.GetDouble();
1010 ++it;
1011 }
1012 }
1013};
1014
1015template <typename... Args>
1016struct TSerializer<JSONObject, boost::container::vector<Args...>> : ArraySerializer
1017{
1018};
1019
1020template <typename... Args>
1021struct TSerializer<JSONObject, std::vector<Args...>> : ArraySerializer
1022{
1023};
1024
1025template <typename... Args>
1027{
1028};
1029
1030template <typename... Args>
1031struct TSerializer<JSONObject, QList<Args...>> : ArraySerializer
1032{
1033};
1034
1035template <typename T, std::size_t N, typename Alloc>
1036struct TSerializer<JSONObject, boost::container::small_vector<T, N, Alloc>>
1038{
1039};
1040
1041template <typename T, std::size_t N>
1042struct TSerializer<JSONObject, boost::container::static_vector<T, N>> : ArraySerializer
1043{
1044};
1045
1046template <typename T, std::size_t N>
1048{
1049};
1050
1051template <std::size_t N>
1052struct TSerializer<JSONObject, std::array<float, N>> : ArraySerializer
1053{
1054};
1055
1056template <typename T, typename U, bool O>
1058{
1059};
1060
1061template <typename T>
1062struct TSerializer<JSONObject, std::optional<T>>
1063{
1064 static void readFrom(JSONObject::Serializer& s, const std::optional<T>& obj)
1065 {
1066 if(obj)
1067 s.readFrom(*obj);
1068 else
1069 s.stream.Null();
1070 }
1071
1072 static void writeTo(JSONObject::Deserializer& s, std::optional<T>& obj)
1073 {
1074 if(s.base.IsNull())
1075 {
1076 obj = std::nullopt;
1077 }
1078 else
1079 {
1080 T t;
1081 t <<= JsonValue{s.base};
1082 obj = std::move(t);
1083 }
1084 }
1085};
1086
1087template <typename T, typename U>
1088struct TSerializer<JSONObject, std::pair<T, U>>
1089{
1090 using type = std::pair<T, U>;
1091 static void readFrom(JSONObject::Serializer& s, const type& obj)
1092 {
1093 s.stream.StartArray();
1094 s.readFrom(obj.first);
1095 s.readFrom(obj.second);
1096 s.stream.EndArray();
1097 }
1098
1099 static void writeTo(JSONObject::Deserializer& s, type& obj)
1100 {
1101 const auto& arr = s.base.GetArray();
1102 obj.first <<= JsonValue{arr[0]};
1103 obj.second <<= JsonValue{arr[1]};
1104 }
1105};
1106
1107template <>
1108struct TSerializer<JSONObject, QVariantMap>
1109{
1110 using type = QVariantMap;
1111 static void readFrom(JSONObject::Serializer& s, const type& obj) = delete;
1112 // {
1113 // SCORE_ABORT;
1114 // /*
1115 // QJsonArray arr;
1116 // arr.append(toJsonValue(obj.first));
1117 // arr.append(toJsonValue(obj.second));
1118 // s.val = std::move(arr);
1119 // */
1120 // }
1121
1122 static void writeTo(JSONObject::Deserializer& s, type& obj) = delete;
1123 // {
1124 // SCORE_ABORT;
1125 // /*
1126 // const auto arr = s.val.toArray();
1127 // obj.first = fromJsonValue<T>(arr[0]);
1128 // obj.second = fromJsonValue<U>(arr[1]);
1129 // */
1130 // }
1131};
1132
1133template <typename T>
1134struct TSerializer<JSONObject, ossia::flat_set<T>>
1135{
1136 using type = ossia::flat_set<T>;
1137 static void readFrom(JSONObject::Serializer& s, const type& obj)
1138 {
1139 ArraySerializer::readFrom(s, obj.tree().get_sequence_cref());
1140 }
1141
1142 static void writeTo(JSONObject::Deserializer& s, type& obj)
1143 {
1144 ArraySerializer::writeTo(s, obj.tree().get_sequence_ref());
1145 }
1146};
1147
1148template <>
1150{
1151 static void readFrom(JSONObject::Serializer& s, QColor c)
1152 {
1153 const auto col = c.rgba64();
1154 s.stream.StartArray();
1155 s.stream.Int(col.red());
1156 s.stream.Int(col.green());
1157 s.stream.Int(col.blue());
1158 s.stream.Int(col.alpha());
1159 s.stream.EndArray();
1160 }
1161
1162 static void writeTo(JSONObject::Deserializer& s, QColor& c)
1163 {
1164 const auto& array = s.base.GetArray();
1165 QRgba64 col;
1166 col.setRed(array[0].GetInt());
1167 col.setGreen(array[1].GetInt());
1168 col.setBlue(array[2].GetInt());
1169 col.setAlpha(array[3].GetInt());
1170 c = col;
1171 }
1172};
1173
1174template <>
1176{
1177 static void readFrom(JSONObject::Serializer& s, QPoint c)
1178 {
1179 s.stream.StartArray();
1180 s.stream.Int(c.x());
1181 s.stream.Int(c.y());
1182 s.stream.EndArray();
1183 }
1184
1185 static void writeTo(JSONObject::Deserializer& s, QPoint& c)
1186 {
1187 const auto& array = s.base.GetArray();
1188 c.setX(array[0].GetInt());
1189 c.setY(array[1].GetInt());
1190 }
1191};
1192
1193template <>
1194struct TSerializer<JSONObject, QPointF>
1195{
1196 static void readFrom(JSONObject::Serializer& s, QPointF c)
1197 {
1198 s.stream.StartArray();
1199 s.stream.Double(c.x());
1200 s.stream.Double(c.y());
1201 s.stream.EndArray();
1202 }
1203
1204 static void writeTo(JSONObject::Deserializer& s, QPointF& c)
1205 {
1206 const auto& array = s.base.GetArray();
1207 c.setX(array[0].GetDouble());
1208 c.setY(array[1].GetDouble());
1209 }
1210};
1211
1212template <>
1214{
1215 static void readFrom(JSONObject::Serializer& s, QSize c)
1216 {
1217 s.stream.StartArray();
1218 s.stream.Int(c.width());
1219 s.stream.Int(c.height());
1220 s.stream.EndArray();
1221 }
1222
1223 static void writeTo(JSONObject::Deserializer& s, QSize& c)
1224 {
1225 const auto& array = s.base.GetArray();
1226 c.setWidth(array[0].GetInt());
1227 c.setHeight(array[1].GetInt());
1228 }
1229};
1230
1231template <>
1233{
1234 static void readFrom(JSONObject::Serializer& s, QSizeF c)
1235 {
1236 s.stream.StartArray();
1237 s.stream.Double(c.width());
1238 s.stream.Double(c.height());
1239 s.stream.EndArray();
1240 }
1241
1242 static void writeTo(JSONObject::Deserializer& s, QSizeF& c)
1243 {
1244 const auto& array = s.base.GetArray();
1245 c.setWidth(array[0].GetDouble());
1246 c.setHeight(array[1].GetDouble());
1247 }
1248};
1249
1250template <>
1252{
1253 static void readFrom(JSONObject::Serializer& s, QRect c)
1254 {
1255 s.stream.StartArray();
1256 s.stream.Int(c.x());
1257 s.stream.Int(c.y());
1258 s.stream.Int(c.width());
1259 s.stream.Int(c.height());
1260 s.stream.EndArray();
1261 }
1262
1263 static void writeTo(JSONObject::Deserializer& s, QRect& c)
1264 {
1265 const auto& array = s.base.GetArray();
1266 c.setX(array[0].GetInt());
1267 c.setY(array[1].GetInt());
1268 c.setWidth(array[2].GetInt());
1269 c.setHeight(array[3].GetInt());
1270 }
1271};
1272
1273template <>
1275{
1276 static void readFrom(JSONObject::Serializer& s, QRectF c)
1277 {
1278 s.stream.StartArray();
1279 s.stream.Double(c.x());
1280 s.stream.Double(c.y());
1281 s.stream.Double(c.width());
1282 s.stream.Double(c.height());
1283 s.stream.EndArray();
1284 }
1285
1286 static void writeTo(JSONObject::Deserializer& s, QRectF& c)
1287 {
1288 const auto& array = s.base.GetArray();
1289 c.setX(array[0].GetDouble());
1290 c.setY(array[1].GetDouble());
1291 c.setWidth(array[2].GetDouble());
1292 c.setHeight(array[3].GetDouble());
1293 }
1294};
1295template <typename T>
1297{
1298 using type = Id<T>;
1299 static void readFrom(JSONObject::Serializer& s, const type& obj)
1300 {
1301 s.stream.Int64(obj.val());
1302 }
1303
1304 static void writeTo(JSONObject::Deserializer& s, type& obj)
1305 {
1306 obj.setVal(s.base.GetInt64());
1307 }
1308};
1309
1310template <>
1311struct SCORE_LIB_BASE_EXPORT TSerializer<DataStream, rapidjson::Document>
1312{
1313 static void readFrom(DataStream::Serializer& s, const rapidjson::Document& obj);
1314 static void writeTo(DataStream::Deserializer& s, rapidjson::Document& obj);
1315};
1316template <>
1317struct SCORE_LIB_BASE_EXPORT TSerializer<DataStream, rapidjson::Value>
1318{
1319 static void readFrom(DataStream::Serializer& s, rapidjson::Value& obj) = delete;
1320 static void writeTo(DataStream::Deserializer& s, rapidjson::Value& obj) = delete;
1321};
1322
1323namespace Process
1324{
1325class Inlet;
1326}
1327template <>
1328void JSONReader::read<Process::Inlet*>(Process::Inlet* const&) = delete;
1329
1330SCORE_LIB_BASE_EXPORT
1331rapidjson::Document clone(const rapidjson::Value& val) noexcept;
1332
1333SCORE_LIB_BASE_EXPORT
1334rapidjson::Document readJson(const QByteArray& arr);
1335
1336inline QByteArray jsonToByteArray(const rapidjson::Value& arr) noexcept
1337{
1338 rapidjson::StringBuffer buf;
1339 buf.Reserve(8192);
1340 JsonWriter w{buf};
1341 arr.Accept(w);
1342 return QByteArray(buf.GetString(), buf.GetSize());
1343}
1344
1345SCORE_LIB_BASE_EXPORT
1346rapidjson::Document toValue(const JSONReader&) noexcept;
1347
1348template <typename T>
1349T fromJson(const QByteArray& rawData)
1350{
1351 const rapidjson::Document doc = readJson(rawData);
1352 JSONWriter wr{doc};
1353 T t;
1354 wr.writeTo(t);
1355 return t;
1356}
1357
1358template <typename T>
1359QByteArray toJson(const T& t)
1360{
1361 JSONReader reader;
1362 reader.readFrom(t);
1363 return reader.toByteArray();
1364}
1365
1366namespace score
1367{
1368
1369template <typename Object>
1370auto unmarshall(const JSONReader& obj)
1371{
1372 Object data;
1373 const auto doc = toValue(obj);
1374 JSONWriter wrt{doc};
1375 wrt.writeTo(data);
1376 return data;
1377}
1378
1379}
1380/*
1381template<typename T>
1382struct optional_assigner {
1383 T& lhs;
1384 bool set_default = false;
1385 template<typename R>
1386 void operator||(R&& rhs) const noexcept
1387 {
1388 if(set_default)
1389 lhs = std::forward<R>(rhs);
1390 }
1391};
1392template <typename T>
1393inline auto operator<<=(T& t, const std::optional<JsonValue>& self)
1394{
1395 if(self)
1396 {
1397 JSONWriter w{self->obj};
1398 w.writeTo(t);
1399 return optional_assigner<T>{t, false};
1400 }
1401 else
1402 {
1403 return optional_assigner<T>{t, true};
1404 }
1405}
1406*/
1407
1408#define assign_with_default(member, optional, alt_value) \
1409 do \
1410 { \
1411 if(auto it = optional) \
1412 member <<= *it; \
1413 else \
1414 member = alt_value; \
1415 } 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:1324
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