1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
11 #include <tools/toolsdllapi.h>
12 #include <rtl/ustring.hxx>
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
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
;
43 bool mbFirstFieldInNode
;
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. */
64 OString
extractAsOString();
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
);
84 * Auto-closes the node.
86 class ScopedJsonWriterNode
88 friend class JsonWriter
;
92 ScopedJsonWriterNode(JsonWriter
& rWriter
)
98 ~ScopedJsonWriterNode() { mrWriter
.endNode(); }
102 * Auto-closes the node.
104 class ScopedJsonWriterArray
106 friend class JsonWriter
;
108 JsonWriter
& mrWriter
;
110 ScopedJsonWriterArray(JsonWriter
& rWriter
)
116 ~ScopedJsonWriterArray() { mrWriter
.endArray(); }
120 * Auto-closes the node.
122 class ScopedJsonWriterStruct
124 friend class JsonWriter
;
126 JsonWriter
& mrWriter
;
128 ScopedJsonWriterStruct(JsonWriter
& rWriter
)
134 ~ScopedJsonWriterStruct() { mrWriter
.endStruct(); }
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */