diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2020-06-18 21:39:30 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2020-08-10 23:07:20 +0200 |
commit | 29954dbeb16ef206940a8fe7ed94e487f842d17a (patch) | |
tree | f2f11352b7df4e3cfc68e8afdb86e894cb59d1e2 /include | |
parent | cf6388b6a5f3e3baa54ed8b02020e9a25487b32b (diff) |
improvements to JSON Writer
part of the master commit cb95276e6e6bf12a1c06d5c252551e55c788fcb2
Change-Id: Icf18fb4b3ebced375196a8cdbd2a543acf4e43c5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/100444
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/tools/json_writer.hxx | 66 |
1 files changed, 57 insertions, 9 deletions
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 <memory> #include <algorithm> +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<char*>(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: */ |