LibreOffice Module tools (master) 1
json_writer.hxx
Go to the documentation of this file.
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.
4 *
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
10
11#include <sal/config.h>
12
13#include <tools/toolsdllapi.h>
14#include <rtl/string.hxx>
15#include <rtl/ustring.hxx>
16#include <sal/types.h>
17
18#include <string>
19#include <string_view>
20#include <utility>
21
28namespace tools
29{
30class ScopedJsonWriterNode;
31class ScopedJsonWriterArray;
32class ScopedJsonWriterStruct;
33
35{
39
40 char* mpBuffer;
41 char* mPos;
45 bool mbClosed; // cannot add to it anymore
46
47public:
48 JsonWriter();
50
51 [[nodiscard]] ScopedJsonWriterNode startNode(std::string_view);
52 [[nodiscard]] ScopedJsonWriterArray startArray(std::string_view);
53 [[nodiscard]] ScopedJsonWriterStruct startStruct();
54
55 void put(std::u16string_view pPropName, const OUString& rPropValue);
56
57 void put(std::string_view pPropName, const OUString& rPropValue);
58 // Assumes utf-8 property value encoding
59 void put(std::string_view pPropName, std::string_view rPropValue);
60 void put(std::string_view pPropName, const char* pPropVal)
61 {
62 put(pPropName, std::string_view(pPropVal));
63 }
64 template <size_t N> void put(std::string_view pPropName, const char (&pPropVal)[N])
65 {
66 put(pPropName, std::string_view(pPropVal, N));
67 }
68
69 template <typename N, std::enable_if_t<std::is_arithmetic_v<N>, int> = 0>
70 void put(std::string_view pPropName, N n)
71 {
72 putLiteral(pPropName, OString::number(n));
73 }
74 void put(std::string_view pPropName, bool);
75
76 void putSimpleValue(const OUString& rPropValue);
77
79 void putRaw(std::string_view);
80
83 OString finishAndGetAsOString();
84
86 bool isDataEquals(std::string_view) const;
87
88private:
89 void endNode();
90 void endArray();
91 void endStruct();
92 void addCommaBeforeField();
93 void writeEscapedOUString(const OUString& rPropVal);
95 void ensureSpace(int noMoreBytesRequired);
96 void ensureSpaceAndWriteNameColon(std::string_view name, int valSize);
97 void putLiteral(std::string_view propName, std::string_view propValue);
98
99 // overflow validation in debug mode
100 static constexpr unsigned char JSON_WRITER_DEBUG_MARKER = 0xde;
101
102 inline void addValidationMark()
103 {
104#ifndef NDEBUG
105 *(mpBuffer + mSpaceAllocated - 1) = JSON_WRITER_DEBUG_MARKER;
106#endif
107 }
108
109 inline void validate()
110 {
111#ifndef NDEBUG
112 unsigned char c = *(mpBuffer + mSpaceAllocated - 1);
113 assert(c == JSON_WRITER_DEBUG_MARKER);
114#endif
115 }
116};
117
122{
123 friend class JsonWriter;
124
126
128 : mrWriter(rWriter)
129 {
130 }
131
132public:
134};
135
140{
141 friend class JsonWriter;
142
144
146 : mrWriter(rWriter)
147 {
148 }
149
150public:
152};
153
158{
159 friend class JsonWriter;
160
162
164 : mrWriter(rWriter)
165 {
166 }
167
168public:
170};
171}
172/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
void put(std::string_view pPropName, const char *pPropVal)
Definition: json_writer.hxx:60
void put(std::string_view pPropName, const char(&pPropVal)[N])
Definition: json_writer.hxx:64
void put(std::string_view pPropName, N n)
Definition: json_writer.hxx:70
Auto-closes the node.
ScopedJsonWriterArray(JsonWriter &rWriter)
Auto-closes the node.
ScopedJsonWriterNode(JsonWriter &rWriter)
Auto-closes the node.
ScopedJsonWriterStruct(JsonWriter &rWriter)
sal_Int64 n
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
#define N
#define TOOLS_DLLPUBLIC
Definition: toolsdllapi.h:28