diff options
author | Henry Castro <hcastro@collabora.com> | 2018-06-18 08:28:40 -0400 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2018-06-20 21:33:18 +0200 |
commit | 87674a28893520eb8bb528c7e774a7ed926976cb (patch) | |
tree | c35696c9ae53e49a04e96d5ac4d8749582c300dd | |
parent | b949efe7ce89e3665704d899ab14094d9d9db53a (diff) |
lokit: add jsonToUnoAny
Change-Id: I79c2fe22fe7f3a8daa121ecaa529b6bca3216bf3
Reviewed-on: https://gerrit.libreoffice.org/56032
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
Tested-by: Jan Holesovsky <kendy@collabora.com>
-rw-r--r-- | desktop/source/lib/init.cxx | 81 |
1 files changed, 78 insertions, 3 deletions
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index e07fe4babb2a..a9cc9b8bb90f 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -48,6 +48,9 @@ #include <com/sun/star/lang/Locale.hpp> #include <com/sun/star/lang/XComponent.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/reflection/theCoreReflection.hpp> +#include <com/sun/star/reflection/XIdlClass.hpp> +#include <com/sun/star/reflection/XIdlReflection.hpp> #include <com/sun/star/style/XStyleFamiliesSupplier.hpp> #include <com/sun/star/ucb/XContentProvider.hpp> #include <com/sun/star/ucb/XUniversalContentBroker.hpp> @@ -230,19 +233,79 @@ static OUString getAbsoluteURL(const char* pURL) return OUString(); } +static uno::Any jsonToUnoAny(const boost::property_tree::ptree& aTree) +{ + uno::Any aAny; + uno::Any aValue; + sal_Int32 nFields; + uno::TypeClass aTypeClass; + uno::Reference< reflection::XIdlField > aField; + boost::property_tree::ptree aNodeNull, aNodeValue, aNodeField; + const std::string& rType = aTree.get<std::string>("type", ""); + const std::string& rValue = aTree.get<std::string>("value", ""); + uno::Sequence< uno::Reference< reflection::XIdlField > > aFields; + uno::Reference< reflection:: XIdlClass > xIdlClass = + css::reflection::theCoreReflection::get(comphelper::getProcessComponentContext())->forName(OUString::fromUtf8(rType.c_str())); + if (xIdlClass.is()) + { + aTypeClass = xIdlClass->getTypeClass(); + xIdlClass->createObject(aAny); + aFields = xIdlClass->getFields(); + nFields = aFields.getLength(); + aNodeValue = aTree.get_child("value", aNodeNull); + if (nFields > 0 && aNodeValue != aNodeNull) + { + for (sal_Int32 itField = 0; itField < nFields; ++itField) + { + aField = aFields[itField]; + aNodeField = aNodeValue.get_child(aField->getName().toUtf8().getStr(), aNodeNull); + if (aNodeField != aNodeNull) + { + aValue = jsonToUnoAny(aNodeField); + aField->set(aAny, aValue); + } + } + } + else if (!rValue.empty()) + { + if (aTypeClass == uno::TypeClass_VOID) + aAny.clear(); + else if (aTypeClass == uno::TypeClass_BYTE) + aAny <<= static_cast<sal_Int8>(OString(rValue.c_str()).toInt32()); + else if (aTypeClass == uno::TypeClass_BOOLEAN) + aAny <<= OString(rValue.c_str()).toBoolean(); + else if (aTypeClass == uno::TypeClass_SHORT) + aAny <<= static_cast<sal_Int16>(OString(rValue.c_str()).toInt32()); + else if (aTypeClass == uno::TypeClass_UNSIGNED_SHORT) + aAny <<= static_cast<sal_uInt16>(OString(rValue.c_str()).toUInt32()); + else if (aTypeClass == uno::TypeClass_LONG) + aAny <<= OString(rValue.c_str()).toInt32(); + else if (aTypeClass == uno::TypeClass_UNSIGNED_LONG) + aAny <<= static_cast<sal_uInt32>(OString(rValue.c_str()).toInt32()); + else if (aTypeClass == uno::TypeClass_FLOAT) + aAny <<= OString(rValue.c_str()).toFloat(); + else if (aTypeClass == uno::TypeClass_DOUBLE) + aAny <<= OString(rValue.c_str()).toDouble(); + else if (aTypeClass == uno::TypeClass_STRING) + aAny <<= OUString::fromUtf8(rValue.c_str()); + } + } + return aAny; +} + static std::vector<beans::PropertyValue> jsonToPropertyValuesVector(const char* pJSON) { std::vector<beans::PropertyValue> aArguments; if (pJSON && pJSON[0] != '\0') { - boost::property_tree::ptree aTree; + boost::property_tree::ptree aTree, aNodeNull, aNodeValue; std::stringstream aStream(pJSON); boost::property_tree::read_json(aStream, aTree); for (const auto& rPair : aTree) { - const std::string& rType = rPair.second.get<std::string>("type"); - const std::string& rValue = rPair.second.get<std::string>("value"); + const std::string& rType = rPair.second.get<std::string>("type", ""); + const std::string& rValue = rPair.second.get<std::string>("value", ""); beans::PropertyValue aValue; aValue.Name = OUString::fromUtf8(rPair.first.c_str()); @@ -256,6 +319,18 @@ static std::vector<beans::PropertyValue> jsonToPropertyValuesVector(const char* aValue.Value <<= OString(rValue.c_str()).toInt32(); else if (rType == "unsigned short") aValue.Value <<= static_cast<sal_uInt16>(OString(rValue.c_str()).toUInt32()); + else if (rType == "[]any") + { + aNodeValue = rPair.second.get_child("value", aNodeNull); + if (aNodeValue != aNodeNull && aNodeValue.size() > 0) + { + sal_Int32 itSeq = 0; + uno::Sequence< uno::Any > aSeq(aNodeValue.size()); + for (const auto& rSeqPair : aNodeValue) + aSeq[itSeq++] = jsonToUnoAny(rSeqPair.second); + aValue.Value <<= aSeq; + } + } else SAL_WARN("desktop.lib", "jsonToPropertyValuesVector: unhandled type '"<<rType<<"'"); aArguments.push_back(aValue); |