From a27353d42dba2ed346919b1e17c126ce321049a6 Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 29 Nov 2022 20:44:46 +0300 Subject: [PATCH] PIValueTreeConversions start --- .../serialization/piserializationmodule.h | 1 + .../serialization/pivaluetree_conversions.cpp | 120 ++++++++++++++++++ .../serialization/pivaluetree_conversions.h | 43 +++++++ libs/main/types/pivaluetree.h | 6 +- libs/main/types/pivariant.cpp | 8 +- 5 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 libs/main/serialization/pivaluetree_conversions.cpp create mode 100644 libs/main/serialization/pivaluetree_conversions.h diff --git a/libs/main/serialization/piserializationmodule.h b/libs/main/serialization/piserializationmodule.h index 4f5b970a..aa444483 100644 --- a/libs/main/serialization/piserializationmodule.h +++ b/libs/main/serialization/piserializationmodule.h @@ -54,5 +54,6 @@ #include "pibinarystream.h" #include "pichunkstream.h" #include "pijson.h" +#include "pivaluetree_conversions.h" #endif // PISERIALIZATIONMODULE_H diff --git a/libs/main/serialization/pivaluetree_conversions.cpp b/libs/main/serialization/pivaluetree_conversions.cpp new file mode 100644 index 00000000..c5c411b1 --- /dev/null +++ b/libs/main/serialization/pivaluetree_conversions.cpp @@ -0,0 +1,120 @@ +/* + PIP - Platform Independent Primitives + PIValueTree conversions + Ivan Pelipenko peri4ko@yandex.ru + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ +#include "pivaluetree_conversions.h" +#include "pipropertystorage.h" +#include "pijson.h" +#include "piiostring.h" + + +PIValueTree PIValueTreeConversions::fromPropertyStorage(const PIPropertyStorage & ps) { + PIValueTree ret; + for (const auto & i: ps) { + PIValueTree v; + v.setName(i.name); + v.setValue(i.value); + v.setComment(i.comment); + ret.addChild(v); + } + return ret; +} + + +PIValueTree fromJSONTree(const PIJSON & json) { + PIValueTree ret; + ret.setName(json["name"].toString()); + ret.setComment(json["comment"].toString()); + PIString value_str = json["value"].toString(); + PIVariant value = PIVariant::fromType(json["type"].toString()); + if (value_str.isNotEmpty()) { + if (value.isValid()) + value.setValueFromString(value_str); + else + value = value_str; + } + ret.setValue(value); + const PIJSON & attr(json["attributes"]); + for (const auto & i: attr.object()) + ret.attributes()[i.first] = i.second.value(); + bool is_array = ret.isArray(); + int chindex = 0; + const PIJSON & chl(json["children"]); + for (const auto & i: chl.array()) { + PIValueTree c = fromJSONTree(i); + if (is_array) c.setName(PIString::fromNumber(chindex++)); + ret.addChild(c); + } + return ret; +} + + +PIValueTree PIValueTreeConversions::fromJSON(const PIJSON & json) { + PIValueTree ret; + if (json.isObject()) ret = fromJSONTree(json); + if (json.isArray()) { + for (const auto & i: json.array()) + ret.addChild(fromJSONTree(i)); + } + return ret; +} + + +PIValueTree PIValueTreeConversions::fromText(PIIODevice * device) { + PIValueTree ret; + return ret; +} + + +PIJSON toJSONTree(const PIValueTree & root) { + PIJSON ret = PIJSON::newObject(); + if (root.name().isNotEmpty()) + ret["name"] = root.name(); + if (root.comment().isNotEmpty()) + ret["comment"] = root.comment(); + if (root.value().isValid()) { + ret["type"] = root.value().typeName(); + ret["value"] = root.value().toString(); + } + if (root.attributes().isNotEmpty()) { + PIJSON j; + for (const auto & a: root.attributes()) + j[a.first] = a.second.toString(); + ret["attributes"] = j; + } + if (root.hasChildren()) { + PIJSON j; + for (const auto & c: root.children()) + j << toJSONTree(c); + ret["children"] = j; + } + return ret; +} + + +PIJSON PIValueTreeConversions::toJSON(const PIValueTree & root) { + PIJSON ret = PIJSON::newArray(); + for (const auto & c: root.children()) + ret << toJSONTree(c); + return ret; +} + + +PIString PIValueTreeConversions::toText(const PIValueTree & root) { + PIString ret; + return ret; +} diff --git a/libs/main/serialization/pivaluetree_conversions.h b/libs/main/serialization/pivaluetree_conversions.h new file mode 100644 index 00000000..826a71a2 --- /dev/null +++ b/libs/main/serialization/pivaluetree_conversions.h @@ -0,0 +1,43 @@ +/*! \file pivaluetree_conversions.h + * \ingroup Serialization + * \brief + * \~english PIValueTree conversions + * \~russian Преобразования PIValueTree +*/ +/* + PIP - Platform Independent Primitives + PIValueTree conversions + Ivan Pelipenko peri4ko@yandex.ru + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef pivaluetree_conversions_H +#define pivaluetree_conversions_H + +#include "pivaluetree.h" + +class PIPropertyStorage; +class PIJSON; +class PIIODevice; + +namespace PIValueTreeConversions { + PIP_EXPORT PIValueTree fromPropertyStorage(const PIPropertyStorage & ps); + PIP_EXPORT PIValueTree fromJSON(const PIJSON & json); + PIP_EXPORT PIValueTree fromText(PIIODevice * device); + PIP_EXPORT PIJSON toJSON(const PIValueTree & root); + PIP_EXPORT PIString toText(const PIValueTree & root); +} + +#endif diff --git a/libs/main/types/pivaluetree.h b/libs/main/types/pivaluetree.h index 3cb1402c..c30c9fd3 100644 --- a/libs/main/types/pivaluetree.h +++ b/libs/main/types/pivaluetree.h @@ -109,8 +109,7 @@ BINARY_STREAM_WRITE(PIValueTree) { .add(2, v._comment) .add(3, v._attributes) .add(4, v._value) - .add(5, v._name) - .add(6, v._children); + .add(5, v._children); s << cs.data(); return s; } @@ -127,8 +126,7 @@ BINARY_STREAM_READ (PIValueTree) { case 2: cs.get(v._comment); case 3: cs.get(v._attributes); case 4: cs.get(v._value); - case 5: cs.get(v._name); - case 6: cs.get(v._children); + case 5: cs.get(v._children); } } return s; diff --git a/libs/main/types/pivariant.cpp b/libs/main/types/pivariant.cpp index dc58a4fb..2210961f 100644 --- a/libs/main/types/pivariant.cpp +++ b/libs/main/types/pivariant.cpp @@ -172,7 +172,7 @@ PIVariant::Type PIVariant::typeFromName(const PIString & tname) { if (s == "pibitarray" || s == "bitarray") return PIVariant::pivBitArray; if (s == "pibytearray" || s == "bytearray" || s == "vector" || s == "pivector" || s == "vector" || s == "pivector" || s == "vector" || s == "pivector") return PIVariant::pivByteArray; - if (s == "pistring" || s == "string") return PIVariant::pivString; + if (s == "pistring" || s == "string" || s == "text") return PIVariant::pivString; if (s == "pistringlist" || s == "stringlist" || s == "vector" || s == "vector" || s == "pivector" || s == "pivector") return PIVariant::pivStringList; if (s == "pitime" || s == "time") return PIVariant::pivTime; if (s == "pidate" || s == "date") return PIVariant::pivDate; @@ -221,12 +221,12 @@ PIVariant::Type PIVariant::typeFromID(uint type_id) { if (type_id == typeID()) return PIVariant::pivFile; if (type_id == typeID()) return PIVariant::pivDir; if (type_id == typeID()) return PIVariant::pivColor; + if (type_id == typeID()) return PIVariant::pivIODevice; if (type_id == typeID()) return PIVariant::pivPoint; if (type_id == typeID()) return PIVariant::pivRect; if (type_id == typeID()) return PIVariant::pivMathVector; if (type_id == typeID()) return PIVariant::pivMathMatrix; if (type_id == typeID()) return PIVariant::pivLine; - if (type_id == typeID()) return PIVariant::pivIODevice; #ifdef CUSTOM_PIVARIANT if (__PIVariantInfoStorage__::get()->map->contains(type_id)) return PIVariant::pivCustom; @@ -271,12 +271,12 @@ uint PIVariant::typeIDFromType(Type type) { case (PIVariant::pivFile ): return typeID(); case (PIVariant::pivDir ): return typeID(); case (PIVariant::pivColor ): return typeID(); + case (PIVariant::pivIODevice ): return typeID(); case (PIVariant::pivPoint ): return typeID(); case (PIVariant::pivRect ): return typeID(); case (PIVariant::pivMathVector): return typeID(); case (PIVariant::pivMathMatrix): return typeID(); case (PIVariant::pivLine ): return typeID(); - case (PIVariant::pivIODevice ): return typeID(); default: break; } return 0; @@ -385,11 +385,11 @@ PIString PIVariant::typeName(PIVariant::Type type) { case PIVariant::pivFile: return "File"; case PIVariant::pivDir: return "Dir"; case PIVariant::pivColor: return "Color"; + case PIVariant::pivIODevice: return "IODevice"; case PIVariant::pivPoint: return "Point"; case PIVariant::pivRect: return "Rect"; case PIVariant::pivMathVector: return "Vector"; case PIVariant::pivMathMatrix: return "Matrix"; - case PIVariant::pivIODevice: return "IODevice"; case PIVariant::pivCustom: return "Custom"; default: break; }