diff options
author | randomdan <randomdan@localhost> | 2014-09-15 23:15:42 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-09-15 23:15:42 +0000 |
commit | f5d62d11bd667c4b195cca086161c5a499da8f8e (patch) | |
tree | 599668ecffda805abb7548121fed6729ba9a478c | |
parent | Fix support for json to deserialize inherited types (diff) | |
download | slicer-f5d62d11bd667c4b195cca086161c5a499da8f8e.tar.bz2 slicer-f5d62d11bd667c4b195cca086161c5a499da8f8e.tar.xz slicer-f5d62d11bd667c4b195cca086161c5a499da8f8e.zip |
Allow overriding of the slicer-typeid property name
-rw-r--r-- | slicer/json/serializer.cpp | 24 | ||||
-rw-r--r-- | slicer/slicer/modelParts.cpp | 6 | ||||
-rw-r--r-- | slicer/slicer/modelParts.h | 4 | ||||
-rw-r--r-- | slicer/slicer/parser.cpp | 6 | ||||
-rw-r--r-- | slicer/test/initial/inherit-d.json | 1 | ||||
-rw-r--r-- | slicer/test/run-slicer.cpp | 1 | ||||
-rw-r--r-- | slicer/test/types.ice | 10 | ||||
-rw-r--r-- | slicer/xml/serializer.cpp | 12 |
8 files changed, 50 insertions, 14 deletions
diff --git a/slicer/json/serializer.cpp b/slicer/json/serializer.cpp index 48b6b36..2c718ae 100644 --- a/slicer/json/serializer.cpp +++ b/slicer/json/serializer.cpp @@ -128,9 +128,11 @@ namespace Slicer { } void operator()(const json::Object & o) const { - auto typeAttrItr = o.find("slicer-typeid"); - if (typeAttrItr != o.end() && boost::get<json::String>(typeAttrItr->second.get())) { - modelPart = modelPart->GetSubclassModelPart(boost::get<json::String>(*typeAttrItr->second)); + if (auto typeIdName = modelPart->GetTypeIdProperty()) { + auto typeAttrItr = o.find(*typeIdName); + if (typeAttrItr != o.end() && boost::get<json::String>(typeAttrItr->second.get())) { + modelPart = modelPart->GetSubclassModelPart(boost::get<json::String>(*typeAttrItr->second)); + } } modelPart->Create(); BOOST_FOREACH(const auto & element, o) { @@ -185,9 +187,11 @@ namespace Slicer { case mpt_Complex: { auto nn = json::ValuePtr(new json::Value(json::Object())); - if (auto typeId = mp->GetTypeId()) { - boost::get<json::Object>(*nn).insert({"slicer-typeid", json::ValuePtr(new json::Value(*typeId))}); - mp = mp->GetSubclassModelPart(*typeId); + if (auto typeIdName = mp->GetTypeIdProperty()) { + if (auto typeId = mp->GetTypeId()) { + boost::get<json::Object>(*nn).insert({*typeIdName, json::ValuePtr(new json::Value(*typeId))}); + mp = mp->GetSubclassModelPart(*typeId); + } } mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, boost::get<json::Object>(*n).insert({name, nn}).first->second.get(), _1, _2)); break; @@ -213,9 +217,11 @@ namespace Slicer { break; case mpt_Complex: *n = json::Object(); - if (auto typeId = mp->GetTypeId()) { - boost::get<json::Object>(*n).insert({"slicer-typeid", json::ValuePtr(new json::Value(*typeId))}); - mp = mp->GetSubclassModelPart(*typeId); + if (auto typeIdName = mp->GetTypeIdProperty()) { + if (auto typeId = mp->GetTypeId()) { + boost::get<json::Object>(*n).insert({*typeIdName, json::ValuePtr(new json::Value(*typeId))}); + mp = mp->GetSubclassModelPart(*typeId); + } } mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2)); break; diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 4c05513..b163da3 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -40,6 +40,12 @@ namespace Slicer { return TypeId(); } + IceUtil::Optional<std::string> + ModelPart::GetTypeIdProperty() const + { + return IceUtil::Optional<std::string>(); + } + void ModelPart::SetValue(ValueSourcePtr) { diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 41b0617..9a0dab9 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -102,6 +102,7 @@ namespace Slicer { virtual ModelPartPtr GetChild(const std::string & memberName) = 0; virtual ModelPartPtr GetSubclassModelPart(const std::string &); virtual TypeId GetTypeId() const; + virtual IceUtil::Optional<std::string> GetTypeIdProperty() const; virtual ModelPartType GetType() const = 0; virtual void Create(); virtual void Complete(); @@ -332,8 +333,11 @@ namespace Slicer { virtual TypeId GetTypeId() const override; + virtual IceUtil::Optional<std::string> GetTypeIdProperty() const override { return typeIdProperty; } + private: T & ModelObject; + static std::string typeIdProperty; }; template<typename T> diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index 6c65c1f..e3487c7 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -129,6 +129,12 @@ namespace Slicer { fprintf(cpp, "\t};\n\n"); fprintf(cpp, "template<>\n"); + auto typeId = metaDataValue("slicer:typeid:", c->getMetaData()); + fprintf(cpp, "std::string ModelPartForClass< %s >::typeIdProperty(\"%s\");\n\n", + typeToString(decl).c_str(), + typeId ? typeId->c_str() : "slicer-typeid"); + + fprintf(cpp, "template<>\n"); auto name = metaDataValue("slicer:root:", c->getMetaData()); fprintf(cpp, "std::string ModelPartForClassRoot< %s >::rootName(\"%s\");\n\n", typeToString(decl).c_str(), diff --git a/slicer/test/initial/inherit-d.json b/slicer/test/initial/inherit-d.json new file mode 100644 index 0000000..8b61a69 --- /dev/null +++ b/slicer/test/initial/inherit-d.json @@ -0,0 +1 @@ +{"b":{"a":1.000000,"b":2.000000,"mytype":"::TestModule::D12"}} diff --git a/slicer/test/run-slicer.cpp b/slicer/test/run-slicer.cpp index 1f0c986..9cbf74a 100644 --- a/slicer/test/run-slicer.cpp +++ b/slicer/test/run-slicer.cpp @@ -278,6 +278,7 @@ main(int, char ** argv) verifyByFile<TestModule::BuiltIns, Slicer::JsonFile>(root, tmpf, "builtins2.json", checkBuiltIns_valuesCorrect); verifyByFile<TestModule::Optionals, Slicer::JsonFile>(root, tmpf, "optionals-areset2.json", checkOptionals_areset); verifyByFile<TestModule::InheritanceCont, Slicer::JsonFile>(root, tmpf, "inherit-c.json", checkInherits_types); + verifyByFile<TestModule::InheritanceCont2, Slicer::JsonFile>(root, tmpf, "inherit-d.json"); verifyByHelper<TestModule::Optionals, Slicer::JsonValue, json::Value>(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); verifyByHelper<TestModule::Optionals, Slicer::XmlDocument, xmlpp::Document *>(root, tmph, "optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); diff --git a/slicer/test/types.ice b/slicer/test/types.ice index 1f484d3..eb7a8c9 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -75,4 +75,14 @@ module TestModule { interface IgnoreMe { int someFunction(); }; + ["slicer:typeid:mytype"] + class Base2 { + int a; + }; + class D12 extends Base2 { + int b; + }; + class InheritanceCont2 { + Base2 b; + }; }; diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index 103f8f5..0ac4034 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -167,9 +167,10 @@ namespace Slicer { if (auto element = dynamic_cast<const xmlpp::Element *>(node)) { auto smp = mp->GetChild(element->get_name()); if (smp) { - auto typeAttr = element->get_attribute("slicer-typeid"); - if (typeAttr) { - smp = smp->GetSubclassModelPart(typeAttr->get_value()); + if (auto typeIdPropName = smp->GetTypeIdProperty()) { + if (auto typeAttr = element->get_attribute(*typeIdPropName)) { + smp = smp->GetSubclassModelPart(typeAttr->get_value()); + } } smp->Create(); auto attrs(element->get_attributes()); @@ -212,9 +213,10 @@ namespace Slicer { } else if (mp) { auto element = n->add_child(name); + auto typeIdPropName = mp->GetTypeIdProperty(); auto typeId = mp->GetTypeId(); - if (typeId) { - element->set_attribute("slicer-typeid", *typeId); + if (typeId && typeIdPropName) { + element->set_attribute(*typeIdPropName, *typeId); mp = mp->GetSubclassModelPart(*typeId); } mp->GetValue(new XmlContentValueTarget(element)); |