1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#pragma once
// Deprecated. Use library/cpp/json/writer in new code.
#include "json_value.h"
#include <library/cpp/json/writer/json.h>
#include <util/stream/output.h>
#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <util/generic/strbuf.h>
namespace NJson {
struct TJsonWriterConfig {
constexpr static ui32 DefaultDoubleNDigits = 10;
constexpr static ui32 DefaultFloatNDigits = 6;
constexpr static EFloatToStringMode DefaultFloatToStringMode = PREC_NDIGITS;
inline TJsonWriterConfig& SetUnbuffered(bool v) noexcept {
Unbuffered = v;
return *this;
}
inline TJsonWriterConfig& SetValidateUtf8(bool v) noexcept {
ValidateUtf8 = v;
return *this;
}
inline TJsonWriterConfig& SetFormatOutput(bool v) noexcept {
FormatOutput = v;
return *this;
}
ui32 DoubleNDigits = DefaultDoubleNDigits;
ui32 FloatNDigits = DefaultFloatNDigits;
EFloatToStringMode FloatToStringMode = DefaultFloatToStringMode;
bool FormatOutput = false;
bool SortKeys = false;
bool ValidateUtf8 = true;
bool DontEscapeStrings = false;
bool Unbuffered = false;
bool WriteNanAsString = false; // NaN and Inf are not valid json values, so if WriteNanAsString is set, writer would write string intead of throwing exception (default case)
};
class TJsonWriter {
IOutputStream* Out;
NJsonWriter::TBuf Buf;
const ui32 DoubleNDigits;
const ui32 FloatNDigits;
const EFloatToStringMode FloatToStringMode;
const bool SortKeys;
const bool ValidateUtf8;
const bool DontEscapeStrings;
const bool DontFlushInDestructor;
public:
TJsonWriter(IOutputStream* out, bool formatOutput, bool sortkeys = false, bool validateUtf8 = true);
TJsonWriter(IOutputStream* out, const TJsonWriterConfig& config, bool DontFlushInDestructor = false);
~TJsonWriter();
void Flush();
void OpenMap();
void OpenMap(const TStringBuf& key) {
Buf.WriteKey(key);
OpenMap();
}
void CloseMap();
void OpenArray();
void OpenArray(const TStringBuf& key) {
Buf.WriteKey(key);
OpenArray();
}
void CloseArray();
void WriteNull();
void Write(const TStringBuf& value);
void Write(float value);
void Write(double value);
void Write(bool value);
void Write(const TJsonValue* value);
void Write(const TJsonValue& value);
// must use all variations of integer types since long
// and long long are different types but with same size
void Write(long long value);
void Write(unsigned long long value);
void Write(long value) {
Write((long long)value);
}
void Write(unsigned long value) {
Write((unsigned long long)value);
}
void Write(int value) {
Write((long long)value);
}
void Write(unsigned int value) {
Write((unsigned long long)value);
}
void Write(short value) {
Write((long long)value);
}
void Write(unsigned short value) {
Write((unsigned long long)value);
}
void Write(const unsigned char* value) {
Write((const char*)value);
}
void Write(const char* value) {
Write(TStringBuf(value));
}
void Write(const TString& value) {
Write(TStringBuf(value));
}
void Write(const std::string& value) {
Write(TStringBuf(value));
}
// write raw json without checks
void UnsafeWrite(const TStringBuf& value) {
Buf.UnsafeWriteValue(value);
}
template <typename T>
void Write(const TStringBuf& key, const T& value) {
Buf.WriteKey(key);
Write(value);
}
// write raw json without checks
void UnsafeWrite(const TStringBuf& key, const TStringBuf& value) {
Buf.WriteKey(key);
UnsafeWrite(value);
}
void WriteNull(const TStringBuf& key) {
Buf.WriteKey(key);
WriteNull();
}
template <typename T>
void WriteOptional(const TStringBuf& key, const TMaybe<T>& value) {
if (value) {
Write(key, *value);
}
}
void WriteOptional(const TStringBuf&, const TNothing&) {
// nothing to do
}
void WriteKey(const TStringBuf key) {
Buf.WriteKey(key);
}
void WriteKey(const unsigned char* key) {
WriteKey((const char*)key);
}
void WriteKey(const char* key) {
WriteKey(TStringBuf{key});
}
void WriteKey(const TString& key) {
WriteKey(TStringBuf{key});
}
void WriteKey(const std::string& key) {
WriteKey(TStringBuf{key});
}
NJsonWriter::TBufState State() const {
return Buf.State();
}
void Reset(const NJsonWriter::TBufState& from) {
return Buf.Reset(from);
}
void Reset(NJsonWriter::TBufState&& from) {
return Buf.Reset(std::move(from));
}
};
void WriteJson(IOutputStream*, const TJsonValue*, bool formatOutput = false, bool sortkeys = false, bool validateUtf8 = true);
TString WriteJson(const TJsonValue*, bool formatOutput = true, bool sortkeys = false, bool validateUtf8 = false);
TString WriteJson(const TJsonValue&, bool formatOutput = true, bool sortkeys = false, bool validateUtf8 = false);
void WriteJson(IOutputStream*, const TJsonValue*, const TJsonWriterConfig& config);
}
|