From 6086d896183a529d4a0b83d4862970c8f320b0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20K=C5=82os?= Date: Thu, 18 May 2023 15:00:38 +0200 Subject: [PATCH] linking: api: use JsonWriter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If5bf1897f1aef8db1672789cbee14b90cb96dc08 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151959 Tested-by: Jenkins CollaboraOffice Reviewed-by: Szymon Kłos Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154378 Tested-by: Jenkins --- desktop/source/lib/init.cxx | 57 +++++++++---------------------- include/tools/json_writer.hxx | 2 ++ tools/source/misc/json_writer.cxx | 20 +++++++++++ 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 99ba738c17d5..8d830c0cbd00 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -410,7 +410,7 @@ std::vector desktop::jsonToPropertyValuesVector(const char return aArguments; } -static bool extractLinks(const uno::Reference< container::XNameAccess >& xLinks, bool subcontent, OUStringBuffer& jsonText) +static void extractLinks(const uno::Reference< container::XNameAccess >& xLinks, bool subcontent, tools::JsonWriter& aJson) { const uno::Sequence< OUString > aNames( xLinks->getElementNames() ); @@ -451,47 +451,27 @@ static bool extractLinks(const uno::Reference< container::XNameAccess >& xLinks, if (subcontent) { - jsonText.append("\""); - jsonText.append(aStrDisplayname); - jsonText.append("\": \""); - jsonText.append(aLink); - jsonText.append("\""); - if (i < nLinks-1) - { - jsonText.append(", "); - } + aJson.put(aStrDisplayname, aLink); } else { uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY ); bIsTarget = xSI->supportsService( aProp_LinkTarget ); - if (i != 0) - { - if (!bIsTarget) - jsonText.append("}"); - if (i < nLinks) - { - jsonText.append(", "); - } - } - jsonText.append("\""); - jsonText.append(aStrDisplayname); - jsonText.append("\": "); if (bIsTarget) { - jsonText.append("\""); - jsonText.append(aLink); - jsonText.append("\""); + aJson.put(aStrDisplayname, aLink); continue; } - jsonText.append("{"); - } + else + { + std::unique_ptr pName(convertOUString(aStrDisplayname)); + auto aNode = aJson.startNode(pName.get()); - uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY ); - if( xLTS.is() ) - { - extractLinks(xLTS->getLinks(), true, jsonText); + uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY ); + if( xLTS.is() ) + extractLinks(xLTS->getLinks(), true, aJson); + } } } catch(...) @@ -500,7 +480,6 @@ static bool extractLinks(const uno::Reference< container::XNameAccess >& xLinks, } } } - return bIsTarget; } static void unoAnyToJson(tools::JsonWriter& rJson, std::string_view pNodeName, const uno::Any& anyItem) @@ -3145,14 +3124,12 @@ static char* lo_extractRequest(LibreOfficeKit* /*pThis*/, const char* pFilePath) if( xLTS.is() ) { - OUStringBuffer jsonText("{ \"Targets\": { "); - bool lastParentheses = extractLinks(xLTS->getLinks(), false, jsonText); - jsonText.append("} }"); - if (!lastParentheses) - jsonText.append(" }"); - - OUString res(jsonText.makeStringAndClear()); - return convertOUString(res); + tools::JsonWriter aJson; + { + auto aNode = aJson.startNode("Targets"); + extractLinks(xLTS->getLinks(), false, aJson); + } + return strdup(aJson.finishAndGetAsOString().getStr()); } xComp->dispose(); } diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx index 12b3eea25a8c..a84e9660704d 100644 --- a/include/tools/json_writer.hxx +++ b/include/tools/json_writer.hxx @@ -52,6 +52,8 @@ public: [[nodiscard]] ScopedJsonWriterArray startArray(std::string_view); [[nodiscard]] ScopedJsonWriterStruct startStruct(); + void put(std::u16string_view pPropName, const OUString& rPropValue); + void put(std::string_view pPropName, const OUString& rPropValue); // Assumes utf-8 property value encoding void put(std::string_view pPropName, std::string_view rPropValue); diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx index 8303e4c2973e..6e8cd7eafe86 100644 --- a/tools/source/misc/json_writer.cxx +++ b/tools/source/misc/json_writer.cxx @@ -225,6 +225,26 @@ void JsonWriter::writeEscapedOUString(const OUString& rPropVal) validate(); } +void JsonWriter::put(std::u16string_view pPropName, const OUString& rPropVal) +{ + auto nPropNameLength = pPropName.length(); + // But values can be any UTF-8, + // if the string only contains of 0x2028, it will be expanded 6 times (see writeEscapedSequence) + auto nWorstCasePropValLength = rPropVal.getLength() * 6; + ensureSpace(nPropNameLength + nWorstCasePropValLength + 8); + + addCommaBeforeField(); + + writeEscapedOUString(OUString(pPropName)); + + memcpy(mPos, ": ", 2); + mPos += 2; + + writeEscapedOUString(rPropVal); + + validate(); +} + void JsonWriter::put(std::string_view pPropName, const OUString& rPropVal) { // Values can be any UTF-8,