From 1e83197fdd4263ca4817a6ac16f274aaee3e66fd Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Fri, 25 Nov 2022 16:32:37 +0100 Subject: [PATCH] comphelper: support property values arrays in JsonToPropertyValues() Needed for an uncoming .uno:TextFormFields uno command where one of the parameters has the []com.sun.star.beans.PropertyValues type at an UNO level, and we can't provide JSON at the moment that would express that. Change-Id: I288a540b2fcac0e5a4a82bca235199c559ba2d0c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143302 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- comphelper/qa/unit/propertyvalue.cxx | 71 ++++++++++++++++++++ comphelper/source/misc/sequenceashashmap.cxx | 14 ++++ 2 files changed, 85 insertions(+) diff --git a/comphelper/qa/unit/propertyvalue.cxx b/comphelper/qa/unit/propertyvalue.cxx index 40f60bb0463d..738022917e9d 100644 --- a/comphelper/qa/unit/propertyvalue.cxx +++ b/comphelper/qa/unit/propertyvalue.cxx @@ -14,9 +14,12 @@ #include #include +#include #include #include +using namespace com::sun::star; + namespace { class MakePropertyValueTest : public CppUnit::TestFixture @@ -25,6 +28,7 @@ class MakePropertyValueTest : public CppUnit::TestFixture CPPUNIT_TEST(testLvalue); CPPUNIT_TEST(testRvalue); CPPUNIT_TEST(testBitField); + CPPUNIT_TEST(testJson); CPPUNIT_TEST_SUITE_END(); void testLvalue() @@ -52,6 +56,73 @@ class MakePropertyValueTest : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(cppu::UnoType::get(), v.Value.getValueType()); CPPUNIT_ASSERT_EQUAL(false, *o3tl::doAccess(v.Value)); } + + void testJson() + { + std::vector aRet = comphelper::JsonToPropertyValues(R"json( +{ + "FieldType": { + "type": "string", + "value": "vnd.oasis.opendocument.field.UNHANDLED" + }, + "FieldCommandPrefix": { + "type": "string", + "value": "ADDIN ZOTERO_ITEM" + }, + "Fields": { + "type": "[][]com.sun.star.beans.PropertyValue", + "value": [ + { + "FieldType": { + "type": "string", + "value": "vnd.oasis.opendocument.field.UNHANDLED" + }, + "FieldCommand": { + "type": "string", + "value": "ADDIN ZOTERO_ITEM new command 1" + }, + "Fields": { + "type": "string", + "value": "new result 1" + } + }, + { + "FieldType": { + "type": "string", + "value": "vnd.oasis.opendocument.field.UNHANDLED" + }, + "FieldCommandPrefix": { + "type": "string", + "value": "ADDIN ZOTERO_ITEM new command 2" + }, + "Fields": { + "type": "string", + "value": "new result 2" + } + } + ] + } +} +)json"); + CPPUNIT_ASSERT_EQUAL(static_cast(3), aRet.size()); + beans::PropertyValue aFirst = aRet[0]; + CPPUNIT_ASSERT_EQUAL(OUString("FieldType"), aFirst.Name); + CPPUNIT_ASSERT_EQUAL(OUString("vnd.oasis.opendocument.field.UNHANDLED"), + aFirst.Value.get()); + beans::PropertyValue aSecond = aRet[1]; + CPPUNIT_ASSERT_EQUAL(OUString("FieldCommandPrefix"), aSecond.Name); + CPPUNIT_ASSERT_EQUAL(OUString("ADDIN ZOTERO_ITEM"), aSecond.Value.get()); + beans::PropertyValue aThird = aRet[2]; + CPPUNIT_ASSERT_EQUAL(OUString("Fields"), aThird.Name); + uno::Sequence> aSeqs; + aThird.Value >>= aSeqs; + CPPUNIT_ASSERT_EQUAL(static_cast(2), aSeqs.getLength()); + uno::Sequence aFirstSeq = aSeqs[0]; + CPPUNIT_ASSERT_EQUAL(OUString("FieldType"), aFirstSeq[0].Name); + CPPUNIT_ASSERT_EQUAL(OUString("FieldCommand"), aFirstSeq[1].Name); + CPPUNIT_ASSERT_EQUAL(OUString("ADDIN ZOTERO_ITEM new command 1"), + aFirstSeq[1].Value.get()); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(MakePropertyValueTest); diff --git a/comphelper/source/misc/sequenceashashmap.cxx b/comphelper/source/misc/sequenceashashmap.cxx index 34a6a0c8a580..b18202aab5f5 100644 --- a/comphelper/source/misc/sequenceashashmap.cxx +++ b/comphelper/source/misc/sequenceashashmap.cxx @@ -31,6 +31,7 @@ #include #include #include +#include using namespace com::sun::star; @@ -366,6 +367,19 @@ std::vector JsonToPropertyValues(const OString& rJson aValue.Value <<= aSeq; } } + else if (rType == "[][]com.sun.star.beans.PropertyValue") + { + aNodeValue = rPair.second.get_child("value", aNodeNull); + std::vector> aSeqs; + for (const auto& rItem : aNodeValue) + { + std::stringstream s; + boost::property_tree::write_json(s, rItem.second); + std::vector aPropertyValues = JsonToPropertyValues(s.str().c_str()); + aSeqs.push_back(comphelper::containerToSequence(aPropertyValues)); + } + aValue.Value <<= comphelper::containerToSequence(aSeqs); + } else SAL_WARN("comphelper", "JsonToPropertyValues: unhandled type '" << rType << "'"); aArguments.push_back(aValue);