summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-08-07 19:09:21 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-08-07 19:24:06 +0100
commit18c188ca112773a39f02b3c4637e22a3d5c15af6 (patch)
tree2e7b337f392301794b4965e2702d269f371b8b3c
parentType-safe variant emplace based model tree iteration (diff)
downloadslicer-18c188ca112773a39f02b3c4637e22a3d5c15af6.tar.bz2
slicer-18c188ca112773a39f02b3c4637e22a3d5c15af6.tar.xz
slicer-18c188ca112773a39f02b3c4637e22a3d5c15af6.zip
Implement ModelTreeIterate as a wrapper around ModelTreeIterateRoot
Adding the HasValue() checks for complex, sequence and dictionary.
-rw-r--r--slicer/json/serializer.cpp146
-rw-r--r--slicer/json/serializer.h2
2 files changed, 60 insertions, 88 deletions
diff --git a/slicer/json/serializer.cpp b/slicer/json/serializer.cpp
index 59201ca..1019602 100644
--- a/slicer/json/serializer.cpp
+++ b/slicer/json/serializer.cpp
@@ -229,7 +229,11 @@ namespace Slicer {
return;
}
- ModelTreeIterateRoot(a.emplace_back(), mp);
+ ModelTreeIterateTo(
+ [&a]() -> json::Value & {
+ return a.emplace_back();
+ },
+ mp);
}
void
@@ -238,118 +242,82 @@ namespace Slicer {
if (!mp->HasValue()) {
return;
}
- json::Object::key_type k;
- json::Value kv;
- mp->GetChild(keyName)->GetValue(JsonValueTarget(kv));
- JsonValueSource(kv).set(k);
- ModelTreeIterateRoot(d[std::move(k)], mp->GetChild(valueName));
+ ModelTreeIterateTo(
+ [&d, mp]() -> json::Value & {
+ json::Object::key_type k;
+ json::Value kv;
+ mp->GetChild(keyName)->GetValue(JsonValueTarget(kv));
+ JsonValueSource(kv).set(k);
+ return d[std::move(k)];
+ },
+ mp->GetChild(valueName));
}
void
JsonSerializer::ModelTreeIterate(json::Object & o, const std::string & name, ModelPartParam mp)
{
- if (name.empty() || !mp) {
+ if (name.empty()) {
return;
}
- switch (mp->GetType()) {
- case ModelPartType::Null:
- o[name].emplace<json::Null>();
- break;
- case ModelPartType::Simple: {
- if (json::Value v; mp->GetValue(JsonValueTarget(v))) {
- o[name] = std::move(v);
- }
- break;
- }
- case ModelPartType::Complex:
- if (mp->HasValue()) {
- auto oec = [&o, &name](const auto & lmp) {
- auto & obj = o[name].emplace<json::Object>();
- lmp->OnEachChild([&obj](auto && PH1, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterate(obj, PH1, PH2);
- });
- return &obj;
- };
- if (auto typeIdName = mp->GetTypeIdProperty()) {
- if (auto typeId = mp->GetTypeId()) {
- oec(mp->GetSubclassModelPart(*typeId))->emplace(*typeIdName, *typeId);
- return;
- }
- }
- oec(mp);
- }
- break;
- case ModelPartType::Sequence:
- if (mp->HasValue()) {
- mp->OnEachChild([&array = o[name].emplace<json::Array>()](auto &&, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterateSeq(array, PH2);
- });
- }
- break;
- case ModelPartType::Dictionary:
- if (mp->HasValue()) {
- if (mp->GetMetadata().flagSet(md_object)) {
- mp->OnEachChild([&obj = o[name].emplace<json::Object>()](auto &&, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterateDictObj(obj, PH2);
- });
- }
- else {
- mp->OnEachChild([&array = o[name].emplace<json::Array>()](auto &&, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterateSeq(array, PH2);
- });
- }
- }
- break;
- }
+ ModelTreeIterateTo(
+ [&o, &name]() -> json::Value & {
+ return o[name];
+ },
+ mp);
}
void
- JsonSerializer::ModelTreeIterateRoot(json::Value & n, ModelPartParam mp)
+ JsonSerializer::ModelTreeIterateTo(const std::function<json::Value &()> & n, ModelPartParam mp)
{
if (mp) {
switch (mp->GetType()) {
case ModelPartType::Null:
- n.emplace<json::Null>();
+ n().emplace<json::Null>();
break;
case ModelPartType::Simple:
if (json::Value v; mp->GetValue(JsonValueTarget(v))) {
- n = std::move(v);
+ n() = std::move(v);
}
break;
- case ModelPartType::Complex: {
- auto oec = [&n](const auto & lmp) {
- auto & obj = n.emplace<json::Object>();
- lmp->OnEachChild([&obj](auto && PH1, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterate(obj, PH1, PH2);
- });
- return &obj;
- };
- if (auto typeIdName = mp->GetTypeIdProperty()) {
- if (auto typeId = mp->GetTypeId()) {
- oec(mp->GetSubclassModelPart(*typeId))->emplace(*typeIdName, *typeId);
- return;
+ case ModelPartType::Complex:
+ if (mp->HasValue()) {
+ auto oec = [&n](const auto & lmp) {
+ auto & obj = n().emplace<json::Object>();
+ lmp->OnEachChild([&obj](auto && PH1, auto && PH2, auto &&) {
+ return JsonSerializer::ModelTreeIterate(obj, PH1, PH2);
+ });
+ return &obj;
+ };
+ if (auto typeIdName = mp->GetTypeIdProperty()) {
+ if (auto typeId = mp->GetTypeId()) {
+ oec(mp->GetSubclassModelPart(*typeId))->emplace(*typeIdName, *typeId);
+ return;
+ }
}
+ oec(mp);
}
- oec(mp);
break;
- }
case ModelPartType::Sequence:
- mp->OnEachChild([&arr = n.emplace<json::Array>()](auto &&, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterateSeq(arr, PH2);
- });
- break;
- case ModelPartType::Dictionary:
- if (mp->GetMetadata().flagSet(md_object)) {
- mp->OnEachChild([&obj = n.emplace<json::Object>()](auto &&, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterateDictObj(obj, PH2);
- });
- }
- else {
- mp->OnEachChild([&arr = n.emplace<json::Array>()](auto &&, auto && PH2, auto &&) {
+ if (mp->HasValue()) {
+ mp->OnEachChild([&arr = n().emplace<json::Array>()](auto &&, auto && PH2, auto &&) {
return JsonSerializer::ModelTreeIterateSeq(arr, PH2);
});
}
break;
+ case ModelPartType::Dictionary:
+ if (mp->HasValue()) {
+ if (mp->GetMetadata().flagSet(md_object)) {
+ mp->OnEachChild([&obj = n().emplace<json::Object>()](auto &&, auto && PH2, auto &&) {
+ return JsonSerializer::ModelTreeIterateDictObj(obj, PH2);
+ });
+ }
+ else {
+ mp->OnEachChild([&arr = n().emplace<json::Array>()](auto &&, auto && PH2, auto &&) {
+ return JsonSerializer::ModelTreeIterateSeq(arr, PH2);
+ });
+ }
+ }
+ break;
}
}
}
@@ -394,7 +362,11 @@ namespace Slicer {
JsonValueSerializer::Serialize(ModelPartForRootParam modelRoot)
{
modelRoot->OnEachChild([this](auto &&, auto && PH2, auto &&) {
- return JsonSerializer::ModelTreeIterateRoot(value, PH2);
+ return JsonSerializer::ModelTreeIterateTo(
+ [this]() -> json::Value & {
+ return value;
+ },
+ PH2);
});
}
}
diff --git a/slicer/json/serializer.h b/slicer/json/serializer.h
index 447821e..923c37b 100644
--- a/slicer/json/serializer.h
+++ b/slicer/json/serializer.h
@@ -16,7 +16,7 @@ namespace Slicer {
static void ModelTreeIterate(json::Object &, const std::string &, ModelPartParam mp);
static void ModelTreeIterateDictObj(json::Object &, ModelPartParam mp);
static void ModelTreeIterateSeq(json::Array &, ModelPartParam mp);
- static void ModelTreeIterateRoot(json::Value &, ModelPartParam mp);
+ static void ModelTreeIterateTo(const std::function<json::Value &()> &, ModelPartParam mp);
};
class DLL_PUBLIC JsonValueSerializer : public JsonSerializer {