Skip to content

Commit ec5845e

Browse files
committed
Remove tab-based JSON pretty printing support
Removed the option to use tabs for JSON pretty printing and standardized on spaces for indentation. Updated constructors and internal logic in DisplayValueFormatter and JsonValueFormatter to use spacesPerIndent only. Also removed the related test case from JsonFormattingTests.
1 parent 0ebe3c2 commit ec5845e

File tree

3 files changed

+17
-39
lines changed

3 files changed

+17
-39
lines changed

Serilog.Sinks.RichTextBox.WinForms.Colored.Test/Integration/JsonFormattingTests.cs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void PrettyPrintJson_FormatsNestedObjectsWithIndentation()
107107
var options = new RichTextBoxSinkOptions(
108108
theme: _defaultTheme,
109109
prettyPrintJson: true,
110-
spacesPerIndent: 4,
110+
spacesPerIndent: 4);
111111

112112
var result = RenderAndGetText(logEvent, "{Message:l}", options);
113113
var expected = "{\n \"Id\": 123,\n \"Name\": \"test\",\n \"$type\": \"MyObj\"\n}";
@@ -128,7 +128,7 @@ public void PrettyPrintJson_FormatsArraysWithIndentation()
128128
var options = new RichTextBoxSinkOptions(
129129
theme: _defaultTheme,
130130
prettyPrintJson: true,
131-
spacesPerIndent: 4,
131+
spacesPerIndent: 4);
132132

133133
var result = RenderAndGetText(logEvent, "{Message:l}", options);
134134
var expected = "[\n 1,\n 2,\n 3\n]";
@@ -149,7 +149,7 @@ public void PrettyPrintJson_FormatsDictionariesWithIndentation()
149149
var options = new RichTextBoxSinkOptions(
150150
theme: _defaultTheme,
151151
prettyPrintJson: true,
152-
spacesPerIndent: 4,
152+
spacesPerIndent: 4);
153153

154154
var result = RenderAndGetText(logEvent, "{Message:l}", options);
155155
var expected = "{\n \"a\": 1,\n \"b\": \"hello\"\n}";
@@ -173,7 +173,7 @@ public void PrettyPrintJson_FormatsNestedStructures()
173173
var options = new RichTextBoxSinkOptions(
174174
theme: _defaultTheme,
175175
prettyPrintJson: true,
176-
spacesPerIndent: 4,
176+
spacesPerIndent: 4);
177177

178178
var result = RenderAndGetText(logEvent, "{Message:l}", options);
179179
var expected = "{\n \"Inner\": {\n \"Value\": 42,\n \"$type\": \"Inner\"\n },\n \"Name\": \"test\",\n \"$type\": \"Outer\"\n}";
@@ -191,32 +191,14 @@ public void PrettyPrintJson_EmptyCollectionsFormatCorrectly()
191191
var options = new RichTextBoxSinkOptions(
192192
theme: _defaultTheme,
193193
prettyPrintJson: true,
194-
spacesPerIndent: 4,
194+
spacesPerIndent: 4);
195195

196196
var result = RenderAndGetText(logEvent, "{Message:l}", options);
197197
Assert.Contains("Array: []", result);
198198
Assert.Contains("Dict: {}", result);
199199
Assert.Contains("Object: {}", result);
200200
}
201201

202-
[Fact]
203-
public void PrettyPrintJson_UsesTabsWhenConfigured()
204-
{
205-
var prop = new LogEventProperty("Test", new StructureValue(new[]
206-
{
207-
new LogEventProperty("Id", new ScalarValue(123))
208-
}, "MyObj"));
209-
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{Test:j}"), new[] { prop });
210-
211-
var options = new RichTextBoxSinkOptions(
212-
theme: _defaultTheme,
213-
prettyPrintJson: true,
214-
spacesPerIndent: 4,
215-
216-
var result = RenderAndGetText(logEvent, "{Message:l}", options);
217-
var expected = "{\n\t\t\t\t\"Id\": 123,\n\t\t\t\t\"$type\": \"MyObj\"\n}";
218-
Assert.Equal(expected, result);
219-
}
220202

221203
[Fact]
222204
public void PrettyPrintJson_RespectsSpacesPerIndent()
@@ -285,7 +267,7 @@ public void PrettyPrintJson_DictionaryWithNullKey()
285267
var options = new RichTextBoxSinkOptions(
286268
theme: _defaultTheme,
287269
prettyPrintJson: true,
288-
spacesPerIndent: 4,
270+
spacesPerIndent: 4);
289271

290272
var result = RenderAndGetText(logEvent, "{Message:l}", options);
291273
Assert.Contains("\"null\"", result);
@@ -305,7 +287,7 @@ public void PrettyPrintJson_DictionaryWithNonStringKey()
305287
var options = new RichTextBoxSinkOptions(
306288
theme: _defaultTheme,
307289
prettyPrintJson: true,
308-
spacesPerIndent: 4,
290+
spacesPerIndent: 4);
309291

310292
var result = RenderAndGetText(logEvent, "{Message:l}", options);
311293
Assert.Contains("\"123\"", result);

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Formatting/DisplayValueFormatter.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@ public class DisplayValueFormatter : ValueFormatter
3030
{
3131
private readonly IFormatProvider? _formatProvider;
3232
private readonly bool _prettyPrintJson;
33-
private readonly int _indentSize;
34-
private readonly bool _useSpacesForIndent;
33+
private readonly int _spacesPerIndent;
3534
private readonly StringBuilder _scalarBuilder = new();
3635
private readonly StringBuilder _literalBuilder = new(64);
3736
private JsonValueFormatter? _jsonValueFormatter;
3837

39-
public DisplayValueFormatter(Theme theme, IFormatProvider? formatProvider, bool prettyPrintJson = false, int indentSize = 4, bool useSpacesForIndent = true) : base(theme, formatProvider)
38+
public DisplayValueFormatter(Theme theme, IFormatProvider? formatProvider, bool prettyPrintJson = false, int spacesPerIndent = 4, bool useSpacesForIndent = true) : base(theme, formatProvider)
4039
{
4140
_formatProvider = formatProvider;
4241
_prettyPrintJson = prettyPrintJson;
43-
_indentSize = indentSize;
44-
_useSpacesForIndent = useSpacesForIndent;
42+
_spacesPerIndent = spacesPerIndent;
4543
}
4644

4745
private void FormatLiteralValue(ScalarValue scalar, IRtfCanvas canvas, string? format, bool isLiteral)
@@ -106,7 +104,7 @@ protected override bool VisitDictionaryValue(ValueFormatterState state, Dictiona
106104
{
107105
if (state.Format.Contains("j"))
108106
{
109-
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider, _prettyPrintJson, _indentSize, _useSpacesForIndent);
107+
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider, _prettyPrintJson, _spacesPerIndent, true);
110108
_jsonValueFormatter.Format(dictionary, state.Canvas, state.Format, state.IsLiteral);
111109
return true;
112110
}
@@ -142,7 +140,7 @@ protected override bool VisitSequenceValue(ValueFormatterState state, SequenceVa
142140
{
143141
if (state.Format.Contains("j"))
144142
{
145-
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider, _prettyPrintJson, _indentSize, _useSpacesForIndent);
143+
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider, _prettyPrintJson, _spacesPerIndent, true);
146144
_jsonValueFormatter.Format(sequence, state.Canvas, state.Format, state.IsLiteral);
147145
return true;
148146
}
@@ -169,7 +167,7 @@ protected override bool VisitStructureValue(ValueFormatterState state, Structure
169167
{
170168
if (state.Format.Contains("j"))
171169
{
172-
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider, _prettyPrintJson, _indentSize, _useSpacesForIndent);
170+
_jsonValueFormatter ??= new JsonValueFormatter(Theme, _formatProvider, _prettyPrintJson, _spacesPerIndent, true);
173171
_jsonValueFormatter.Format(structure, state.Canvas, state.Format, state.IsLiteral);
174172
return true;
175173
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Formatting/JsonValueFormatter.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,21 @@ public class JsonValueFormatter : ValueFormatter
3131
{
3232
private readonly IFormatProvider? _formatProvider;
3333
private readonly bool _prettyPrint;
34-
private readonly int _indentSize;
35-
private readonly bool _useSpacesForIndent;
34+
private readonly int _spacesPerIndent;
3635
private readonly StringBuilder _literalBuilder = new(64);
3736
private readonly StringBuilder _scalarBuilder = new();
3837
private readonly StringBuilder _jsonStringBuilder = new();
3938

40-
public JsonValueFormatter(Theme theme, IFormatProvider? formatProvider, bool prettyPrint = false, int indentSize = 4, bool useSpacesForIndent = true) : base(theme, formatProvider)
39+
public JsonValueFormatter(Theme theme, IFormatProvider? formatProvider, bool prettyPrint = false, int spacesPerIndent = 4, bool useSpacesForIndent = true) : base(theme, formatProvider)
4140
{
4241
_formatProvider = formatProvider;
4342
_prettyPrint = prettyPrint;
44-
_indentSize = indentSize;
45-
_useSpacesForIndent = useSpacesForIndent;
43+
_spacesPerIndent = spacesPerIndent;
4644
}
4745

4846
protected override ValueFormatterState CreateInitialState(IRtfCanvas canvas, string format, bool isLiteral)
4947
{
50-
return new ValueFormatterState(canvas, format, isLiteral, 0, _useSpacesForIndent, _indentSize, true);
48+
return new ValueFormatterState(canvas, format, isLiteral, 0, _spacesPerIndent, true);
5149
}
5250

5351
protected override bool VisitScalarValue(ValueFormatterState state, ScalarValue scalar)

0 commit comments

Comments
 (0)