Bump version to 6.4-15
[LibreOffice.git] / include / tools / json_writer.hxx
blobbf07aa0aaa35077f511ba2dad010f498e3ffbdc4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 #pragma once
11 #include <tools/toolsdllapi.h>
12 #include <rtl/ustring.hxx>
13 #include <memory>
14 #include <algorithm>
16 namespace rtl
18 class OStringBuffer;
21 /** Simple JSON encoder designed specifically for LibreOfficeKit purposes.
23 * (1) Minimal allocations/re-allocations/copying
24 * (2) Small/simple JSON documents
25 * (3) ascii property names
27 namespace tools
29 class ScopedJsonWriterNode;
30 class ScopedJsonWriterArray;
31 class ScopedJsonWriterStruct;
33 class TOOLS_DLLPUBLIC JsonWriter
35 friend class ScopedJsonWriterNode;
36 friend class ScopedJsonWriterArray;
37 friend class ScopedJsonWriterStruct;
39 int mSpaceAllocated;
40 char* mpBuffer;
41 int mStartNodeCount;
42 char* mPos;
43 bool mbFirstFieldInNode;
45 public:
46 JsonWriter();
47 ~JsonWriter();
49 [[nodiscard]] ScopedJsonWriterNode startNode(const char*);
50 [[nodiscard]] ScopedJsonWriterArray startArray(const char*);
51 [[nodiscard]] ScopedJsonWriterStruct startStruct();
53 void put(const char* pPropName, const OUString& rPropValue);
54 void put(const char* pPropName, const OString& rPropValue);
55 void put(const char* pPropName, const char* pPropVal);
56 void put(const char*, int);
58 /// This assumes that this data belongs at this point in the stream, and is valid, and properly encoded
59 void putRaw(const rtl::OStringBuffer&);
61 /** Hands ownership of the underlying storage buffer to the caller,
62 * after this no more document modifications may be written. */
63 char* extractData();
64 OString extractAsOString();
66 private:
67 void endNode();
68 void endArray();
69 void endStruct();
70 void addCommaBeforeField();
71 void reallocBuffer(int noMoreBytesRequired);
73 // this part inline to speed up the fast path
74 inline void ensureSpace(int noMoreBytesRequired)
76 assert(mpBuffer && "already extracted data");
77 int currentUsed = mPos - mpBuffer;
78 if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
79 reallocBuffer(noMoreBytesRequired);
83 /**
84 * Auto-closes the node.
86 class ScopedJsonWriterNode
88 friend class JsonWriter;
90 JsonWriter& mrWriter;
92 ScopedJsonWriterNode(JsonWriter& rWriter)
93 : mrWriter(rWriter)
97 public:
98 ~ScopedJsonWriterNode() { mrWriter.endNode(); }
102 * Auto-closes the node.
104 class ScopedJsonWriterArray
106 friend class JsonWriter;
108 JsonWriter& mrWriter;
110 ScopedJsonWriterArray(JsonWriter& rWriter)
111 : mrWriter(rWriter)
115 public:
116 ~ScopedJsonWriterArray() { mrWriter.endArray(); }
120 * Auto-closes the node.
122 class ScopedJsonWriterStruct
124 friend class JsonWriter;
126 JsonWriter& mrWriter;
128 ScopedJsonWriterStruct(JsonWriter& rWriter)
129 : mrWriter(rWriter)
133 public:
134 ~ScopedJsonWriterStruct() { mrWriter.endStruct(); }
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */