From 7413b4ad50b38cf9839fc3e3d4d0bec6ada90a6a Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Mon, 8 Jan 2024 12:19:03 +0600 Subject: Simplify JsonWriter a bit Change-Id: Ifa4278cfd62df4179dd3ae627369c077e31dbd00 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161780 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- tools/qa/cppunit/test_json_writer.cxx | 2 +- tools/source/misc/json_writer.cxx | 50 +++++++---------------------------- 2 files changed, 10 insertions(+), 42 deletions(-) (limited to 'tools') 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) -- cgit