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>
10#include <ossia/detail/flat_set.hpp>
11#include <ossia/detail/json.hpp>
12#include <ossia/detail/small_vector.hpp>
14#include <boost/container/small_vector.hpp>
15#include <boost/container/static_vector.hpp>
33class ApplicationComponents;
35using JsonWriter = ossia::json_writer;
55 using is_visitor_tag = std::integral_constant<bool, true>;
66 static auto marshall(
const T& t)
73 bool empty()
const noexcept {
return this->buffer.GetLength() == 0; }
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;
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;
105 template <
typename T>
112 readFrom((
const typename T::score_base_type&)obj);
114 else if constexpr(std::is_enum_v<T>)
118 stream.Int(
static_cast<int32_t
>(obj));
124 || is_custom_serialized<T>::value)
130 && !is_custom_serialized<T>::value)
132 stream.StartObject();
135 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
145 stream.StartObject();
148 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
157 stream.StartObject();
158 readFromAbstract(obj, [](
JSONReader& sub,
const T& obj) {
166 && !is_custom_serialized<T>::value)
168 stream.StartObject();
169 readFromAbstract(obj, [](
JSONReader& sub,
const T& obj) {
172 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
182 stream.StartObject();
183 readFromAbstract(obj, [](
JSONReader& sub,
const T& obj) {
186 if constexpr(is_custom_serialized<T>::value || is_template<T>::value)
202 rapidjson::StringBuffer buffer;
203 JsonWriter stream{buffer};
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;
217 QByteArray toByteArray()
const
219 SCORE_ASSERT(stream.IsComplete());
220 return QByteArray{buffer.GetString(), (int)buffer.GetLength()};
222 std::string toStdString()
const
224 SCORE_ASSERT(stream.IsComplete());
225 return std::string{buffer.GetString(), buffer.GetLength()};
227 QString toString()
const
229 SCORE_ASSERT(stream.IsComplete());
230 return QString::fromUtf8(buffer.GetString(), buffer.GetLength());
235 template <
typename T>
239 template <
typename T,
typename Fun>
240 void readFromAbstract(
const T& in, Fun f);
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
257 self.stream.StartArray();
258 self.stream.Int(t.x());
259 self.stream.Int(t.y());
260 self.stream.EndArray();
262 void operator=(QPointF t)
const noexcept
264 self.stream.StartArray();
265 self.stream.Double(t.x());
266 self.stream.Double(t.y());
267 self.stream.EndArray();
269 void operator=(QSize t)
const noexcept
271 self.stream.StartArray();
272 self.stream.Int(t.width());
273 self.stream.Int(t.height());
274 self.stream.EndArray();
276 void operator=(QSizeF t)
const noexcept
278 self.stream.StartArray();
279 self.stream.Double(t.width());
280 self.stream.Double(t.height());
281 self.stream.EndArray();
283 void operator=(QRect t)
const noexcept
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();
292 void operator=(QRectF t)
const noexcept
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();
301 void operator=(
const QString& t)
const noexcept { *
this = t.toUtf8(); }
302 void operator=(
const QStringList& t)
const noexcept
304 self.stream.StartArray();
305 for(
const auto& str : t)
306 *
this = str.toUtf8();
307 self.stream.EndArray();
309 void operator=(
const QLatin1String& t)
const noexcept
311 self.stream.String(t.data(), t.size());
313 void operator=(
const std::string& t)
const noexcept
315 self.stream.String(t.data(), t.length());
317 void operator=(
const std::string_view& t)
const noexcept
319 self.stream.String(t.data(), t.length());
321 void operator=(
const QByteArray& t)
const noexcept
323 self.stream.String(t.data(), t.length());
325 void operator=(
const QVariantMap& t)
const noexcept { SCORE_ABORT; }
327 template <
typename T>
328 void operator=(
const T& t)
const noexcept
330 if constexpr(std::is_enum_v<T>)
334 self.stream.Int(
static_cast<int32_t
>(t));
344JSONReader::fake_obj::operator[](std::string_view str)
const noexcept
346 self.stream.Key(str.data(), str.length());
350JSONReader::fake_obj::operator[](
const QString& str)
const noexcept
352 const std::string& s = str.toStdString();
353 self.stream.Key(s.data(), s.length());
354 return assigner{self};
356template <std::
size_t N>
358JSONReader::fake_obj::operator[](
const char (&str)[N])
const noexcept
360 return (*
this)[std::string_view(str, N - 1)];
363template <
typename T,
typename Fun>
364void JSONReader::readFromAbstract(
const T& in, Fun f)
366 obj[strings.uuid] = in.concreteKey().impl();
368 in.serialize_impl(this->toVariant());
373 const rapidjson::Value& obj;
374 QString toString()
const noexcept
376 return QString::fromUtf8(obj.GetString(), obj.GetStringLength());
378 std::string toStdString()
const noexcept
380 return std::string(obj.GetString(), obj.GetStringLength());
382 QByteArray toByteArray()
const noexcept
384 return QByteArray(obj.GetString(), obj.GetStringLength());
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(); }
397 template <std::
size_t N>
398 JsonValue operator[](
const char (&str)[N])
const noexcept
402 JsonValue operator[](
const std::string& str)
const noexcept
406 JsonValue operator[](
const QString& str)
const noexcept
408 return (*
this)[str.toStdString()];
410 template <
typename T>
411 friend void operator<<=(T& t,
const JsonValue& self);
413 template <
typename T>
414 T to()
const noexcept
426 using is_visitor_tag = std::integral_constant<bool, true>;
427 using is_deserializer_tag = std::integral_constant<bool, true>;
429 VisitorVariant toVariant() {
return {*
this, JSONObject::type()}; }
435 explicit JSONWriter(
const rapidjson::Value& obj);
438 template <
typename T>
439 static auto unmarshall(
const rapidjson::Value& obj)
447 template <
typename T>
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;
457 template <
typename T>
462 || is_custom_serialized<T>::value)
466 else if constexpr(std::is_enum<T>::value)
468 obj =
static_cast<T
>(base.GetInt());
476 const rapidjson::Value& base;
480 const rapidjson::Value& ref;
481 template <std::
size_t N>
482 JsonValue operator[](
const char (&str)[N])
const noexcept
486 JsonValue operator[](
const std::string& str)
const noexcept
490 JsonValue operator[](
const QString& str)
const noexcept
492 return (*
this)[str.toStdString()];
494 template <std::
size_t N>
495 std::optional<JsonValue> tryGet(
const char (&str)[N])
const noexcept
497 if(
auto it = ref.FindMember(str); it != ref.MemberEnd())
501 std::optional<JsonValue> tryGet(
const std::string& str)
const noexcept
503 if(
auto it = ref.FindMember(str); it != ref.MemberEnd())
507 std::optional<JsonValue> tryGet(
const QString& str)
const noexcept
509 return tryGet(str.toStdString());
511 std::optional<JsonValue> constFind(
const std::string& str)
const noexcept
515 std::optional<JsonValue> constFind(
const QString& str)
const noexcept
517 return tryGet(str.toStdString());
527inline void operator<<=(T& t,
const JsonValue& self)
533inline void operator<<=(QString& t,
const JsonValue& self)
537inline void operator<<=(
float& t,
const JsonValue& self)
539 t = self.obj.GetFloat();
541inline void operator<<=(
double& t,
const JsonValue& self)
543 t = self.obj.GetDouble();
545inline void operator<<=(
int& t,
const JsonValue& self)
547 t = self.obj.GetInt();
549inline void operator<<=(int64_t& t,
const JsonValue& self)
551 t = self.obj.GetInt();
553inline void operator<<=(std::string& t,
const JsonValue& self)
555 t = self.toStdString();
557inline void operator<<=(QByteArray& t,
const JsonValue& self)
559 t = self.toByteArray();
561inline void operator<<=(
bool& t,
const JsonValue& self)
565inline void operator<<=(
char& t,
const JsonValue& self)
567 t = self.obj.GetStringLength() > 0 ? self.obj.GetString()[0] :
'\0';
573 template <
typename U>
576 s.obj[s.strings.ObjectName] = obj.objectName();
577 s.obj[s.strings.id] = obj.id().val();
580 template <
typename U>
583 obj.setObjectName(s.obj[s.strings.ObjectName].toString());
584 obj.setId(
Id<T>{s.obj[s.strings.id].toInt()});
595 template <
typename T>
598 s.stream.StartArray();
599 for(
const auto& elt : vec)
604 template <
typename T>
607 const auto& array = s.base.GetArray();
610 vec.reserve(array.Size());
611 for(
const auto& elt : array)
613 typename T::value_type v;
616 vec.push_back(std::move(v));
620 template <
template <
typename,
typename...>
typename T,
typename Arg,
typename... Args>
623 using arg_type = std::remove_cvref_t<Arg>;
624 s.stream.StartArray();
625 for(
const auto& elt : vec)
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>)
633 if constexpr(
sizeof(arg_type) > 4)
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>)
642 const QByteArray& b = elt.toUtf8();
643 s.stream.String(b.data(), b.size());
652 template <
typename, std::size_t,
typename...>
typename T,
typename Arg,
653 std::size_t N,
typename... Args>
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>)
661 SCORE_ASSERT(N >= array.Size());
666 vec.resize(array.Size());
669 auto it = vec.begin();
670 for(
const auto& elt : array)
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>)
676 SCORE_ASSERT(elt.IsString());
677 if(elt.GetStringLength() == 1)
678 *it = elt.GetString()[0];
682 else if constexpr(std::is_integral_v<arg_type>)
684 if constexpr(
sizeof(arg_type) > 4)
687 *it = elt.GetInt64();
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>)
698 *it = QString::fromUtf8(elt.GetString(), elt.GetStringLength());
710 template <
typename,
typename,
typename...>
typename T,
typename Arg,
typename Arg2,
714 using arg_type = std::remove_cvref_t<Arg>;
715 const auto& array = s.base.GetArray();
718 vec.resize(array.Size());
720 auto it = vec.begin();
721 for(
const auto& elt : array)
723 if constexpr(std::is_floating_point_v<arg_type>)
724 *it = elt.GetDouble();
725 else if constexpr(std::is_same_v<arg_type, char>)
727 SCORE_ASSERT(elt.IsString());
728 if(elt.GetStringLength() == 1)
729 *it = elt.GetString()[0];
733 else if constexpr(std::is_integral_v<arg_type>)
735 if constexpr(
sizeof(arg_type) > 4)
736 *it = elt.GetInt64();
740 else if constexpr(std::is_same_v<arg_type, std::string>)
741 *it = std::string{elt.GetString(), elt.GetStringLength()};
742 else if constexpr(std::is_same_v<arg_type, QString>)
744 *it = QString::fromUtf8(elt.GetString(), elt.GetStringLength());
755 template <
typename T>
758 s.stream.StartArray();
759 for(
const auto& elt : vec)
764 template <
typename T>
769 const auto& array = s.base.GetArray();
770 for(
const auto& elt : array)
775 vec.push_back(std::move(v));
780 template <
typename T>
783 s.stream.StartArray();
784 for(
const auto& elt : vec)
789 template <
typename T>
792 const auto& array = s.base.GetArray();
794 vec.reserve(array.Size());
796 for(
const auto& elt : array)
801 vec.push_back(std::move(v));
806template <
typename... Args>
811template <
typename... Args>
816template <
typename... Args>
821template <
typename... Args>
826template <
typename T, std::
size_t N,
typename Alloc>
832template <
typename T, std::
size_t N>
837template <
typename T, std::
size_t N>
842template <std::
size_t N>
847template <
typename T,
typename U,
bool O>
878template <
typename T,
typename U>
881 using type = std::pair<T, U>;
884 s.stream.StartArray();
892 const auto& arr = s.base.GetArray();
901 using type = QVariantMap;
927 using type = ossia::flat_set<T>;
930 ArraySerializer::readFrom(s, obj.tree().get_sequence_cref());
935 ArraySerializer::writeTo(s, obj.tree().get_sequence_ref());
944 const auto col = c.rgba64();
945 s.stream.StartArray();
946 s.stream.Int(col.red());
947 s.stream.Int(col.green());
948 s.stream.Int(col.blue());
949 s.stream.Int(col.alpha());
955 const auto& array = s.base.GetArray();
957 col.setRed(array[0].GetInt());
958 col.setGreen(array[1].GetInt());
959 col.setBlue(array[2].GetInt());
960 col.setAlpha(array[3].GetInt());
970 s.stream.StartArray();
978 const auto& array = s.base.GetArray();
979 c.setX(array[0].GetInt());
980 c.setY(array[1].GetInt());
989 s.stream.StartArray();
990 s.stream.Double(c.x());
991 s.stream.Double(c.y());
997 const auto& array = s.base.GetArray();
998 c.setX(array[0].GetDouble());
999 c.setY(array[1].GetDouble());
1008 s.stream.StartArray();
1009 s.stream.Int(c.width());
1010 s.stream.Int(c.height());
1011 s.stream.EndArray();
1016 const auto& array = s.base.GetArray();
1017 c.setWidth(array[0].GetInt());
1018 c.setHeight(array[1].GetInt());
1027 s.stream.StartArray();
1028 s.stream.Double(c.width());
1029 s.stream.Double(c.height());
1030 s.stream.EndArray();
1035 const auto& array = s.base.GetArray();
1036 c.setWidth(array[0].GetDouble());
1037 c.setHeight(array[1].GetDouble());
1046 s.stream.StartArray();
1047 s.stream.Int(c.x());
1048 s.stream.Int(c.y());
1049 s.stream.Int(c.width());
1050 s.stream.Int(c.height());
1051 s.stream.EndArray();
1056 const auto& array = s.base.GetArray();
1057 c.setX(array[0].GetInt());
1058 c.setY(array[1].GetInt());
1059 c.setWidth(array[2].GetInt());
1060 c.setHeight(array[3].GetInt());
1069 s.stream.StartArray();
1070 s.stream.Double(c.x());
1071 s.stream.Double(c.y());
1072 s.stream.Double(c.width());
1073 s.stream.Double(c.height());
1074 s.stream.EndArray();
1079 const auto& array = s.base.GetArray();
1080 c.setX(array[0].GetDouble());
1081 c.setY(array[1].GetDouble());
1082 c.setWidth(array[2].GetDouble());
1083 c.setHeight(array[3].GetDouble());
1086template <
typename T>
1092 s.stream.Int64(obj.val());
1097 obj.setVal(s.base.GetInt64());
1119void JSONReader::read<Process::Inlet*>(
Process::Inlet*
const&) =
delete;
1121SCORE_LIB_BASE_EXPORT
1122rapidjson::Document clone(
const rapidjson::Value& val)
noexcept;
1124SCORE_LIB_BASE_EXPORT
1125rapidjson::Document readJson(
const QByteArray& arr);
1127inline QByteArray jsonToByteArray(
const rapidjson::Value& arr)
noexcept
1129 rapidjson::StringBuffer buf;
1133 return QByteArray(buf.GetString(), buf.GetSize());
1136SCORE_LIB_BASE_EXPORT
1137rapidjson::Document toValue(
const JSONReader&)
noexcept;
1139template <
typename T>
1140T fromJson(
const QByteArray& rawData)
1142 const rapidjson::Document doc = readJson(rawData);
1149template <
typename T>
1150QByteArray toJson(
const T& t)
1154 return reader.toByteArray();
1160template <
typename Object>
1164 const auto doc = toValue(obj);
1199#define assign_with_default(member, optional, alt_value) \
1202 if(auto it = optional) \
1205 member = alt_value; \
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
Definition JSONVisitor.hpp:423
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:1115
Base toolkit upon which the software is built.
Definition Application.cpp:97
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