include/tools/json_writer.hxx         |   77 ++++------------------------------
 sw/source/core/crsr/viscrs.cxx        |    2 
 sw/source/uibase/uno/loktxdoc.cxx     |   24 +++++-----
 tools/qa/cppunit/test_json_writer.cxx |    2 
 tools/source/misc/json_writer.cxx     |   50 +++-------------------
 5 files changed, 33 insertions(+), 122 deletions(-)

New commits:
commit 7413b4ad50b38cf9839fc3e3d4d0bec6ada90a6a
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Mon Jan 8 12:19:03 2024 +0600
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Mon Jan 8 13:05:31 2024 +0100

    Simplify JsonWriter a bit
    
    Change-Id: Ifa4278cfd62df4179dd3ae627369c077e31dbd00
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161780
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx
index c5faf542f1d8..b885cb5e55a3 100644
--- a/include/tools/json_writer.hxx
+++ b/include/tools/json_writer.hxx
@@ -24,15 +24,14 @@
  */
 namespace tools
 {
-class ScopedJsonWriterNode;
-class ScopedJsonWriterArray;
-class ScopedJsonWriterStruct;
-
 class TOOLS_DLLPUBLIC JsonWriter
 {
-    friend class ScopedJsonWriterNode;
-    friend class ScopedJsonWriterArray;
-    friend class ScopedJsonWriterStruct;
+    // Auto-closes the node.
+    template <char closing> struct ScopedJsonWriterNode
+    {
+        JsonWriter& mrWriter;
+        ~ScopedJsonWriterNode() { mrWriter.endNode(closing); }
+    };
 
     char* mpBuffer;
     char* mPos;
@@ -45,9 +44,9 @@ public:
     JsonWriter();
     ~JsonWriter();
 
-    [[nodiscard]] ScopedJsonWriterNode startNode(std::string_view);
-    [[nodiscard]] ScopedJsonWriterArray startArray(std::string_view);
-    [[nodiscard]] ScopedJsonWriterStruct startStruct();
+    [[nodiscard]] ScopedJsonWriterNode<'}'> startNode(std::string_view 
nodeName);
+    [[nodiscard]] ScopedJsonWriterNode<']'> startArray(std::string_view 
nodeName);
+    [[nodiscard]] ScopedJsonWriterNode<'}'> startStruct();
 
     void put(std::u16string_view pPropName, const OUString& rPropValue);
 
@@ -83,9 +82,7 @@ public:
     bool isDataEquals(std::string_view) const;
 
 private:
-    void endNode();
-    void endArray();
-    void endStruct();
+    void endNode(char closing);
     void addCommaBeforeField();
     void writeEscapedOUString(const OUString& rPropVal);
     void closeDocument();
@@ -111,59 +108,5 @@ private:
 #endif
     }
 };
-
-/**
- * Auto-closes the node.
- */
-class ScopedJsonWriterNode
-{
-    friend class JsonWriter;
-
-    JsonWriter& mrWriter;
-
-    ScopedJsonWriterNode(JsonWriter& rWriter)
-        : mrWriter(rWriter)
-    {
-    }
-
-public:
-    ~ScopedJsonWriterNode() { mrWriter.endNode(); }
-};
-
-/**
- * Auto-closes the node.
- */
-class ScopedJsonWriterArray
-{
-    friend class JsonWriter;
-
-    JsonWriter& mrWriter;
-
-    ScopedJsonWriterArray(JsonWriter& rWriter)
-        : mrWriter(rWriter)
-    {
-    }
-
-public:
-    ~ScopedJsonWriterArray() { mrWriter.endArray(); }
-};
-
-/**
- * Auto-closes the node.
- */
-class ScopedJsonWriterStruct
-{
-    friend class JsonWriter;
-
-    JsonWriter& mrWriter;
-
-    ScopedJsonWriterStruct(JsonWriter& rWriter)
-        : mrWriter(rWriter)
-    {
-    }
-
-public:
-    ~ScopedJsonWriterStruct() { mrWriter.endStruct(); }
-};
 }
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/sw/source/core/crsr/viscrs.cxx b/sw/source/core/crsr/viscrs.cxx
index 0385ce85f71c..2dd958b0c42b 100644
--- a/sw/source/core/crsr/viscrs.cxx
+++ b/sw/source/core/crsr/viscrs.cxx
@@ -708,7 +708,7 @@ void SwSelPaintRects::HighlightContentControl()
 
             if (pContentControl && (pContentControl->GetComboBox() || 
pContentControl->GetDropDown()))
             {
-                tools::ScopedJsonWriterArray aItems = 
aJson.startArray("items");
+                auto aItems = aJson.startArray("items");
                 for (const auto& rItem : pContentControl->GetListItems())
                 {
                     aJson.putSimpleValue(rItem.ToString());
diff --git a/sw/source/uibase/uno/loktxdoc.cxx 
b/sw/source/uibase/uno/loktxdoc.cxx
index 47527605f69a..36c7de04e848 100644
--- a/sw/source/uibase/uno/loktxdoc.cxx
+++ b/sw/source/uibase/uno/loktxdoc.cxx
@@ -70,7 +70,7 @@ void GetTextFormFields(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
 
     SwDoc* pDoc = pDocShell->GetDoc();
     IDocumentMarkAccess* pMarkAccess = pDoc->getIDocumentMarkAccess();
-    tools::ScopedJsonWriterArray aFields = rJsonWriter.startArray("fields");
+    auto aFields = rJsonWriter.startArray("fields");
     for (auto it = pMarkAccess->getFieldmarksBegin(); it != 
pMarkAccess->getFieldmarksEnd(); ++it)
     {
         auto pFieldmark = dynamic_cast<sw::mark::IFieldmark*>(*it);
@@ -93,7 +93,7 @@ void GetTextFormFields(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
             continue;
         }
 
-        tools::ScopedJsonWriterStruct aField = rJsonWriter.startStruct();
+        auto aField = rJsonWriter.startStruct();
         rJsonWriter.put("type", aType);
         rJsonWriter.put("command", aCommand);
     }
@@ -175,7 +175,7 @@ void GetDocumentProperties(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell
     uno::Reference<beans::XPropertyAccess> 
xUDP(xDP->getUserDefinedProperties(), uno::UNO_QUERY);
     auto aUDPs = 
comphelper::sequenceToContainer<std::vector<beans::PropertyValue>>(
         xUDP->getPropertyValues());
-    tools::ScopedJsonWriterArray aProperties = 
rJsonWriter.startArray("userDefinedProperties");
+    auto aProperties = rJsonWriter.startArray("userDefinedProperties");
     for (const auto& rUDP : aUDPs)
     {
         if (!rUDP.Name.startsWith(aNamePrefix))
@@ -191,7 +191,7 @@ void GetDocumentProperties(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell
         OUString aValue;
         rUDP.Value >>= aValue;
 
-        tools::ScopedJsonWriterStruct aProperty = rJsonWriter.startStruct();
+        auto aProperty = rJsonWriter.startStruct();
         rJsonWriter.put("name", rUDP.Name);
         rJsonWriter.put("type", "string");
         rJsonWriter.put("value", aValue);
@@ -216,7 +216,7 @@ void GetBookmarks(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
     }
 
     IDocumentMarkAccess& rIDMA = 
*pDocShell->GetDoc()->getIDocumentMarkAccess();
-    tools::ScopedJsonWriterArray aBookmarks = 
rJsonWriter.startArray("bookmarks");
+    auto aBookmarks = rJsonWriter.startArray("bookmarks");
     for (auto it = rIDMA.getBookmarksBegin(); it != rIDMA.getBookmarksEnd(); 
++it)
     {
         sw::mark::IMark* pMark = *it;
@@ -225,7 +225,7 @@ void GetBookmarks(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
             continue;
         }
 
-        tools::ScopedJsonWriterStruct aProperty = rJsonWriter.startStruct();
+        auto aProperty = rJsonWriter.startStruct();
         rJsonWriter.put("name", pMark->GetName());
     }
 }
@@ -251,7 +251,7 @@ void GetBookmark(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
     SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
     SwPosition& rCursor = *pWrtShell->GetCursor()->GetPoint();
     sw::mark::IMark* pBookmark = rIDMA.getOneInnermostBookmarkFor(rCursor);
-    tools::ScopedJsonWriterNode aBookmark = rJsonWriter.startNode("bookmark");
+    auto aBookmark = rJsonWriter.startNode("bookmark");
     if (!pBookmark)
     {
         return;
@@ -298,7 +298,7 @@ void GetFields(tools::JsonWriter& rJsonWriter, SwDocShell* 
pDocShell,
     }
 
     SwDoc* pDoc = pDocShell->GetDoc();
-    tools::ScopedJsonWriterArray aBookmarks = 
rJsonWriter.startArray("setRefs");
+    auto aBookmarks = rJsonWriter.startArray("setRefs");
     std::vector<const SwFormatRefMark*> aRefMarks;
     for (sal_uInt16 i = 0; i < pDoc->GetRefMarks(); ++i)
     {
@@ -321,7 +321,7 @@ void GetFields(tools::JsonWriter& rJsonWriter, SwDocShell* 
pDocShell,
             continue;
         }
 
-        tools::ScopedJsonWriterStruct aProperty = rJsonWriter.startStruct();
+        auto aProperty = rJsonWriter.startStruct();
         rJsonWriter.put("name", pRefMark->GetRefName());
     }
 }
@@ -363,7 +363,7 @@ void GetField(tools::JsonWriter& rJsonWriter, SwDocShell* 
pDocShell,
     SwTextNode* pTextNode = rCursor.GetNode().GetTextNode();
     std::vector<SwTextAttr*> aAttrs
         = pTextNode->GetTextAttrsAt(rCursor.GetContentIndex(), 
RES_TXTATR_REFMARK);
-    tools::ScopedJsonWriterNode aRefmark = rJsonWriter.startNode("setRef");
+    auto aRefmark = rJsonWriter.startNode("setRef");
     if (aAttrs.empty())
     {
         return;
@@ -396,7 +396,7 @@ void GetSections(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
     }
 
     SwDoc* pDoc = pDocShell->GetDoc();
-    tools::ScopedJsonWriterArray aBookmarks = 
rJsonWriter.startArray("sections");
+    auto aBookmarks = rJsonWriter.startArray("sections");
     for (const auto& pSection : pDoc->GetSections())
     {
         if (!pSection->GetName().startsWith(aNamePrefix))
@@ -404,7 +404,7 @@ void GetSections(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
             continue;
         }
 
-        tools::ScopedJsonWriterStruct aProperty = rJsonWriter.startStruct();
+        auto aProperty = rJsonWriter.startStruct();
         rJsonWriter.put("name", pSection->GetName());
     }
 }
diff --git a/tools/qa/cppunit/test_json_writer.cxx 
b/tools/qa/cppunit/test_json_writer.cxx
index a82fc769be31..05f19515e1e4 100644
--- a/tools/qa/cppunit/test_json_writer.cxx
+++ b/tools/qa/cppunit/test_json_writer.cxx
@@ -81,7 +81,7 @@ void JsonWriterTest::testArray()
 {
     tools::JsonWriter aJson;
     {
-        tools::ScopedJsonWriterArray aArray = aJson.startArray("items");
+        auto aArray = aJson.startArray("items");
         aJson.putSimpleValue("foo");
         aJson.putSimpleValue("bar");
     }
diff --git a/tools/source/misc/json_writer.cxx 
b/tools/source/misc/json_writer.cxx
index e3e27bf756b4..277164ee12cc 100644
--- a/tools/source/misc/json_writer.cxx
+++ b/tools/source/misc/json_writer.cxx
@@ -40,78 +40,46 @@ JsonWriter::~JsonWriter()
     free(mpBuffer);
 }
 
-ScopedJsonWriterNode JsonWriter::startNode(std::string_view pNodeName)
+JsonWriter::ScopedJsonWriterNode<'}'> JsonWriter::startNode(std::string_view 
pNodeName)
 {
     putLiteral(pNodeName, "{ ");
 
     mStartNodeCount++;
     mbFirstFieldInNode = true;
 
-    return ScopedJsonWriterNode(*this);
+    return { *this };
 }
 
-void JsonWriter::endNode()
+void JsonWriter::endNode(char closing)
 {
     assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
     --mStartNodeCount;
     ensureSpace(1);
-    *mPos = '}';
+    *mPos = closing;
     ++mPos;
     mbFirstFieldInNode = false;
 
     validate();
 }
 
-ScopedJsonWriterArray JsonWriter::startArray(std::string_view pNodeName)
+JsonWriter::ScopedJsonWriterNode<']'> JsonWriter::startArray(std::string_view 
pNodeName)
 {
     putLiteral(pNodeName, "[ ");
 
     mStartNodeCount++;
     mbFirstFieldInNode = true;
 
-    return ScopedJsonWriterArray(*this);
+    return { *this };
 }
 
-void JsonWriter::endArray()
+JsonWriter::ScopedJsonWriterNode<'}'> JsonWriter::startStruct()
 {
-    assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
-    --mStartNodeCount;
-    ensureSpace(1);
-    *mPos = ']';
-    ++mPos;
-    mbFirstFieldInNode = false;
+    putRaw("{ ");
 
-    validate();
-}
-
-ScopedJsonWriterStruct JsonWriter::startStruct()
-{
-    ensureSpace(6);
-
-    addCommaBeforeField();
-
-    *mPos = '{';
-    ++mPos;
-    *mPos = ' ';
-    ++mPos;
     mStartNodeCount++;
     mbFirstFieldInNode = true;
 
-    validate();
-
-    return ScopedJsonWriterStruct(*this);
-}
-
-void JsonWriter::endStruct()
-{
-    assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
-    --mStartNodeCount;
-    ensureSpace(1);
-    *mPos = '}';
-    ++mPos;
-    mbFirstFieldInNode = false;
-
-    validate();
+    return { *this };
 }
 
 static char getEscapementChar(char ch)

Reply via email to