From 29954dbeb16ef206940a8fe7ed94e487f842d17a Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 18 Jun 2020 21:39:30 +0200 Subject: improvements to JSON Writer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit part of the master commit cb95276e6e6bf12a1c06d5c252551e55c788fcb2 Change-Id: Icf18fb4b3ebced375196a8cdbd2a543acf4e43c5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/100444 Tested-by: Jenkins CollaboraOffice Reviewed-by: Tomaž Vajngerl --- include/tools/json_writer.hxx | 66 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx index 588577d41303..bf07aa0aaa35 100644 --- a/include/tools/json_writer.hxx +++ b/include/tools/json_writer.hxx @@ -13,6 +13,11 @@ #include #include +namespace rtl +{ +class OStringBuffer; +} + /** Simple JSON encoder designed specifically for LibreOfficeKit purposes. * * (1) Minimal allocations/re-allocations/copying @@ -22,10 +27,14 @@ namespace tools { class ScopedJsonWriterNode; +class ScopedJsonWriterArray; +class ScopedJsonWriterStruct; class TOOLS_DLLPUBLIC JsonWriter { friend class ScopedJsonWriterNode; + friend class ScopedJsonWriterArray; + friend class ScopedJsonWriterStruct; int mSpaceAllocated; char* mpBuffer; @@ -38,33 +47,36 @@ public: ~JsonWriter(); [[nodiscard]] ScopedJsonWriterNode startNode(const char*); + [[nodiscard]] ScopedJsonWriterArray startArray(const char*); + [[nodiscard]] ScopedJsonWriterStruct startStruct(); void put(const char* pPropName, const OUString& rPropValue); void put(const char* pPropName, const OString& rPropValue); void put(const char* pPropName, const char* pPropVal); void put(const char*, int); - /** Hands ownership of the the underlying storage buffer to the caller, + /// This assumes that this data belongs at this point in the stream, and is valid, and properly encoded + void putRaw(const rtl::OStringBuffer&); + + /** Hands ownership of the underlying storage buffer to the caller, * after this no more document modifications may be written. */ char* extractData(); OString extractAsOString(); private: void endNode(); + void endArray(); + void endStruct(); void addCommaBeforeField(); + void reallocBuffer(int noMoreBytesRequired); + // this part inline to speed up the fast path inline void ensureSpace(int noMoreBytesRequired) { + assert(mpBuffer && "already extracted data"); int currentUsed = mPos - mpBuffer; if (currentUsed + noMoreBytesRequired >= mSpaceAllocated) - { - auto newSize = std::max(mSpaceAllocated * 2, (currentUsed + noMoreBytesRequired) * 2); - char* pNew = static_cast(malloc(newSize)); - memcpy(pNew, mpBuffer, currentUsed); - free(mpBuffer); - mpBuffer = pNew; - mPos = mpBuffer; - } + reallocBuffer(noMoreBytesRequired); } }; @@ -85,5 +97,41 @@ class ScopedJsonWriterNode 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: */ -- cgit