android: Update app-specific/MIME type icons
[LibreOffice.git] / include / tools / json_writer.hxx
blob12b3eea25a8c92c72bf6b7420db38b11f79bbae1
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 <sal/config.h>
13 #include <tools/toolsdllapi.h>
14 #include <rtl/string.hxx>
15 #include <rtl/ustring.hxx>
16 #include <sal/types.h>
18 #include <string>
19 #include <string_view>
20 #include <utility>
22 /** Simple JSON encoder designed specifically for LibreOfficeKit purposes.
24 * (1) Minimal allocations/re-allocations/copying
25 * (2) Small/simple JSON documents
26 * (3) ascii property names
28 namespace tools
30 class ScopedJsonWriterNode;
31 class ScopedJsonWriterArray;
32 class ScopedJsonWriterStruct;
34 class TOOLS_DLLPUBLIC JsonWriter
36 friend class ScopedJsonWriterNode;
37 friend class ScopedJsonWriterArray;
38 friend class ScopedJsonWriterStruct;
40 char* mpBuffer;
41 char* mPos;
42 int mSpaceAllocated;
43 int mStartNodeCount;
44 bool mbFirstFieldInNode;
45 bool mbClosed; // cannot add to it anymore
47 public:
48 JsonWriter();
49 ~JsonWriter();
51 [[nodiscard]] ScopedJsonWriterNode startNode(std::string_view);
52 [[nodiscard]] ScopedJsonWriterArray startArray(std::string_view);
53 [[nodiscard]] ScopedJsonWriterStruct startStruct();
55 void put(std::string_view pPropName, const OUString& rPropValue);
56 // Assumes utf-8 property value encoding
57 void put(std::string_view pPropName, std::string_view rPropValue);
58 void put(std::string_view pPropName, const char* pPropVal)
60 put(pPropName, std::string_view(pPropVal));
62 template <size_t N> void put(std::string_view pPropName, const char (&pPropVal)[N])
64 put(pPropName, std::string_view(pPropVal, N));
67 template <typename N, std::enable_if_t<std::is_arithmetic_v<N>, int> = 0>
68 void put(std::string_view pPropName, N n)
70 putLiteral(pPropName, OString::number(n));
72 void put(std::string_view pPropName, bool);
74 void putSimpleValue(const OUString& rPropValue);
76 /// This assumes that this data belongs at this point in the stream, and is valid, and properly encoded
77 void putRaw(std::string_view);
79 /** Closes the tags, and returns data.
80 * After this no more document modifications may be written. */
81 OString finishAndGetAsOString();
83 /** returns true if the current JSON data matches the string */
84 bool isDataEquals(std::string_view) const;
86 private:
87 void endNode();
88 void endArray();
89 void endStruct();
90 void addCommaBeforeField();
91 void writeEscapedOUString(const OUString& rPropVal);
92 void closeDocument();
93 void ensureSpace(int noMoreBytesRequired);
94 void ensureSpaceAndWriteNameColon(std::string_view name, int valSize);
95 void putLiteral(std::string_view propName, std::string_view propValue);
97 // overflow validation in debug mode
98 static constexpr unsigned char JSON_WRITER_DEBUG_MARKER = 0xde;
100 inline void addValidationMark()
102 #ifndef NDEBUG
103 *(mpBuffer + mSpaceAllocated - 1) = JSON_WRITER_DEBUG_MARKER;
104 #endif
107 inline void validate()
109 #ifndef NDEBUG
110 unsigned char c = *(mpBuffer + mSpaceAllocated - 1);
111 assert(c == JSON_WRITER_DEBUG_MARKER);
112 #endif
117 * Auto-closes the node.
119 class ScopedJsonWriterNode
121 friend class JsonWriter;
123 JsonWriter& mrWriter;
125 ScopedJsonWriterNode(JsonWriter& rWriter)
126 : mrWriter(rWriter)
130 public:
131 ~ScopedJsonWriterNode() { mrWriter.endNode(); }
135 * Auto-closes the node.
137 class ScopedJsonWriterArray
139 friend class JsonWriter;
141 JsonWriter& mrWriter;
143 ScopedJsonWriterArray(JsonWriter& rWriter)
144 : mrWriter(rWriter)
148 public:
149 ~ScopedJsonWriterArray() { mrWriter.endArray(); }
153 * Auto-closes the node.
155 class ScopedJsonWriterStruct
157 friend class JsonWriter;
159 JsonWriter& mrWriter;
161 ScopedJsonWriterStruct(JsonWriter& rWriter)
162 : mrWriter(rWriter)
166 public:
167 ~ScopedJsonWriterStruct() { mrWriter.endStruct(); }
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */