diff options
-rw-r--r-- | slicer/json/serializer.cpp | 146 | ||||
-rw-r--r-- | slicer/json/serializer.h | 2 |
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 { |