Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7fa7e16

Browse files
committed
Add IndentJson() extension method to PrettyPrint JSON
1 parent 6dbfcb9 commit 7fa7e16

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Globalization;
1717
using System.IO;
18+
using System.Linq;
1819
using System.Text;
1920
using ServiceStack.Text.Common;
2021
using ServiceStack.Text.Jsv;
@@ -334,6 +335,71 @@ private static bool HasCircularReferences(object value, Stack<object> parentValu
334335

335336
return false;
336337
}
338+
339+
private static void times(int count, Action fn)
340+
{
341+
for (var i = 0; i < count; i++) fn();
342+
}
343+
344+
private const string Indent = " ";
345+
public static string IndentJson(this string json)
346+
{
347+
var indent = 0;
348+
var quoted = false;
349+
var sb = StringBuilderThreadStatic.Allocate();
350+
351+
for (var i = 0; i < json.Length; i++)
352+
{
353+
var ch = json[i];
354+
switch (ch)
355+
{
356+
case '{':
357+
case '[':
358+
sb.Append(ch);
359+
if (!quoted)
360+
{
361+
sb.AppendLine();
362+
times(++indent, () => sb.Append(Indent));
363+
}
364+
break;
365+
case '}':
366+
case ']':
367+
if (!quoted)
368+
{
369+
sb.AppendLine();
370+
times(--indent, () => sb.Append(Indent));
371+
}
372+
sb.Append(ch);
373+
break;
374+
case '"':
375+
sb.Append(ch);
376+
var escaped = false;
377+
var index = i;
378+
while (index > 0 && json[--index] == '\\')
379+
escaped = !escaped;
380+
if (!escaped)
381+
quoted = !quoted;
382+
break;
383+
case ',':
384+
sb.Append(ch);
385+
if (!quoted)
386+
{
387+
sb.AppendLine();
388+
times(indent, () => sb.Append(Indent));
389+
}
390+
break;
391+
case ':':
392+
sb.Append(ch);
393+
if (!quoted)
394+
sb.Append(" ");
395+
break;
396+
default:
397+
sb.Append(ch);
398+
break;
399+
}
400+
}
401+
return StringBuilderThreadStatic.ReturnAndFree(sb);
402+
}
337403
}
338404

339405
public class JsvStringSerializer : IStringSerializer

tests/ServiceStack.Text.Tests/DynamicModels/GoogleMapsApiTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using NUnit.Framework;
4+
using Platform;
45

56
namespace ServiceStack.Text.Tests.DynamicModels
67
{
@@ -357,7 +358,10 @@ public void Can_parse_GMaps_api()
357358
{
358359
//short for JsonSerializer.DeserializeFromString<GeoLocationResults>(Json)
359360
var geoApiResponse = JsonDto.FromJson<GeoLocationResponse>();
360-
Console.WriteLine(geoApiResponse.Dump());
361+
//geoApiResponse.PrintDump();
362+
363+
//"Pretty Print:".Print();
364+
//geoApiResponse.ToJson().IndentJson().Print();
361365
}
362366
}
363367
}

0 commit comments

Comments
 (0)