@@ -18,6 +18,16 @@ public OpenApiJsonWriter(TextWriter textWriter) : base(textWriter, null)
18
18
{
19
19
}
20
20
21
+ /// <summary>
22
+ /// Initializes a new instance of the <see cref="OpenApiJsonWriter"/> class.
23
+ /// </summary>
24
+ /// <param name="textWriter">The text writer.</param>
25
+ /// <param name="settings">Settings for controlling how the OpenAPI document will be written out.</param>
26
+ public OpenApiJsonWriter ( TextWriter textWriter , OpenApiJsonWriterSettings settings ) : base ( textWriter , settings )
27
+ {
28
+ _produceTerseOutput = settings . Terse ;
29
+ }
30
+
21
31
/// <summary>
22
32
/// Initializes a new instance of the <see cref="OpenApiJsonWriter"/> class.
23
33
/// </summary>
@@ -27,6 +37,11 @@ public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings)
27
37
{
28
38
}
29
39
40
+ /// <summary>
41
+ /// Indicates whether or not the produced document will be written in a compact or pretty fashion.
42
+ /// </summary>
43
+ private bool _produceTerseOutput = false ;
44
+
30
45
/// <summary>
31
46
/// Base Indentation Level.
32
47
/// This denotes how many indentations are needed for the property in the base object.
@@ -51,7 +66,7 @@ public override void WriteStartObject()
51
66
Writer . Write ( WriterConstants . ArrayElementSeparator ) ;
52
67
}
53
68
54
- Writer . WriteLine ( ) ;
69
+ WriteLine ( ) ;
55
70
WriteIndentation ( ) ;
56
71
}
57
72
@@ -68,13 +83,16 @@ public override void WriteEndObject()
68
83
var currentScope = EndScope ( ScopeType . Object ) ;
69
84
if ( currentScope . ObjectCount != 0 )
70
85
{
71
- Writer . WriteLine ( ) ;
86
+ WriteLine ( ) ;
72
87
DecreaseIndentation ( ) ;
73
88
WriteIndentation ( ) ;
74
89
}
75
90
else
76
91
{
77
- Writer . Write ( WriterConstants . WhiteSpaceForEmptyObject ) ;
92
+ if ( ! _produceTerseOutput )
93
+ {
94
+ Writer . Write ( WriterConstants . WhiteSpaceForEmptyObject ) ;
95
+ }
78
96
DecreaseIndentation ( ) ;
79
97
}
80
98
@@ -99,7 +117,7 @@ public override void WriteStartArray()
99
117
Writer . Write ( WriterConstants . ArrayElementSeparator ) ;
100
118
}
101
119
102
- Writer . WriteLine ( ) ;
120
+ WriteLine ( ) ;
103
121
WriteIndentation ( ) ;
104
122
}
105
123
@@ -115,7 +133,7 @@ public override void WriteEndArray()
115
133
var current = EndScope ( ScopeType . Array ) ;
116
134
if ( current . ObjectCount != 0 )
117
135
{
118
- Writer . WriteLine ( ) ;
136
+ WriteLine ( ) ;
119
137
DecreaseIndentation ( ) ;
120
138
WriteIndentation ( ) ;
121
139
}
@@ -143,7 +161,7 @@ public override void WritePropertyName(string name)
143
161
Writer . Write ( WriterConstants . ObjectMemberSeparator ) ;
144
162
}
145
163
146
- Writer . WriteLine ( ) ;
164
+ WriteLine ( ) ;
147
165
148
166
currentScope . ObjectCount ++ ;
149
167
@@ -154,6 +172,11 @@ public override void WritePropertyName(string name)
154
172
Writer . Write ( name ) ;
155
173
156
174
Writer . Write ( WriterConstants . NameValueSeparator ) ;
175
+
176
+ if ( ! _produceTerseOutput )
177
+ {
178
+ Writer . Write ( WriterConstants . NameValueSeparatorWhiteSpaceSuffix ) ;
179
+ }
157
180
}
158
181
159
182
/// <summary>
@@ -198,7 +221,7 @@ protected override void WriteValueSeparator()
198
221
Writer . Write ( WriterConstants . ArrayElementSeparator ) ;
199
222
}
200
223
201
- Writer . WriteLine ( ) ;
224
+ WriteLine ( ) ;
202
225
WriteIndentation ( ) ;
203
226
currentScope . ObjectCount ++ ;
204
227
}
@@ -212,5 +235,31 @@ public override void WriteRaw(string value)
212
235
WriteValueSeparator ( ) ;
213
236
Writer . Write ( value ) ;
214
237
}
238
+
239
+ /// <summary>
240
+ /// Write the indentation.
241
+ /// </summary>
242
+ public override void WriteIndentation ( )
243
+ {
244
+ if ( _produceTerseOutput )
245
+ {
246
+ return ;
247
+ }
248
+
249
+ base . WriteIndentation ( ) ;
250
+ }
251
+
252
+ /// <summary>
253
+ /// Writes a line terminator to the text string or stream.
254
+ /// </summary>
255
+ private void WriteLine ( )
256
+ {
257
+ if ( _produceTerseOutput )
258
+ {
259
+ return ;
260
+ }
261
+
262
+ Writer . WriteLine ( ) ;
263
+ }
215
264
}
216
265
}
0 commit comments