linking: api: use JsonWriter

Change-Id: If5bf1897f1aef8db1672789cbee14b90cb96dc08
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151959
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Szymon Kłos <szymon.klos@collabora.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154378
Tested-by: Jenkins
This commit is contained in:
Szymon Kłos
2023-05-18 15:00:38 +02:00
parent 52d265c0d2
commit 6086d89618
3 changed files with 39 additions and 40 deletions

View File

@@ -410,7 +410,7 @@ std::vector<beans::PropertyValue> desktop::jsonToPropertyValuesVector(const char
return aArguments; 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() ); const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
@@ -451,47 +451,27 @@ static bool extractLinks(const uno::Reference< container::XNameAccess >& xLinks,
if (subcontent) if (subcontent)
{ {
jsonText.append("\""); aJson.put(aStrDisplayname, aLink);
jsonText.append(aStrDisplayname);
jsonText.append("\": \"");
jsonText.append(aLink);
jsonText.append("\"");
if (i < nLinks-1)
{
jsonText.append(", ");
}
} }
else else
{ {
uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY ); uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY );
bIsTarget = xSI->supportsService( aProp_LinkTarget ); 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) if (bIsTarget)
{ {
jsonText.append("\""); aJson.put(aStrDisplayname, aLink);
jsonText.append(aLink);
jsonText.append("\"");
continue; continue;
} }
jsonText.append("{"); else
} {
std::unique_ptr<char[], o3tl::free_delete> pName(convertOUString(aStrDisplayname));
auto aNode = aJson.startNode(pName.get());
uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY ); uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY );
if( xLTS.is() ) if( xLTS.is() )
{ extractLinks(xLTS->getLinks(), true, aJson);
extractLinks(xLTS->getLinks(), true, jsonText); }
} }
} }
catch(...) 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) 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() ) if( xLTS.is() )
{ {
OUStringBuffer jsonText("{ \"Targets\": { "); tools::JsonWriter aJson;
bool lastParentheses = extractLinks(xLTS->getLinks(), false, jsonText); {
jsonText.append("} }"); auto aNode = aJson.startNode("Targets");
if (!lastParentheses) extractLinks(xLTS->getLinks(), false, aJson);
jsonText.append(" }"); }
return strdup(aJson.finishAndGetAsOString().getStr());
OUString res(jsonText.makeStringAndClear());
return convertOUString(res);
} }
xComp->dispose(); xComp->dispose();
} }

View File

@@ -52,6 +52,8 @@ public:
[[nodiscard]] ScopedJsonWriterArray startArray(std::string_view); [[nodiscard]] ScopedJsonWriterArray startArray(std::string_view);
[[nodiscard]] ScopedJsonWriterStruct startStruct(); [[nodiscard]] ScopedJsonWriterStruct startStruct();
void put(std::u16string_view pPropName, const OUString& rPropValue);
void put(std::string_view pPropName, const OUString& rPropValue); void put(std::string_view pPropName, const OUString& rPropValue);
// Assumes utf-8 property value encoding // Assumes utf-8 property value encoding
void put(std::string_view pPropName, std::string_view rPropValue); void put(std::string_view pPropName, std::string_view rPropValue);

View File

@@ -225,6 +225,26 @@ void JsonWriter::writeEscapedOUString(const OUString& rPropVal)
validate(); 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) void JsonWriter::put(std::string_view pPropName, const OUString& rPropVal)
{ {
// Values can be any UTF-8, // Values can be any UTF-8,