This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
brian91292
committed
Dec 20, 2019
1 parent
cae8eb8
commit 7b89ab1
Showing
6 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Text; | ||
|
||
namespace StreamCore.Utils | ||
{ | ||
public class JsonFormatter | ||
{ | ||
public static string Indent = " "; | ||
|
||
public static string PrettyPrint(string input) | ||
{ | ||
var output = new StringBuilder(input.Length * 2); | ||
char? quote = null; | ||
int depth = 0; | ||
|
||
for (int i = 0; i < input.Length; ++i) | ||
{ | ||
char ch = input[i]; | ||
|
||
switch (ch) | ||
{ | ||
case '{': | ||
case '[': | ||
output.Append(ch); | ||
if (!quote.HasValue) | ||
{ | ||
output.AppendLine(); | ||
output.Append(Indent.Repeat(++depth)); | ||
} | ||
break; | ||
case '}': | ||
case ']': | ||
if (quote.HasValue) | ||
output.Append(ch); | ||
else | ||
{ | ||
output.AppendLine(); | ||
output.Append(Indent.Repeat(--depth)); | ||
output.Append(ch); | ||
} | ||
break; | ||
case '"': | ||
case '\'': | ||
output.Append(ch); | ||
if (quote.HasValue) | ||
{ | ||
if (!output.IsEscaped(i)) | ||
quote = null; | ||
} | ||
else quote = ch; | ||
break; | ||
case ',': | ||
output.Append(ch); | ||
if (!quote.HasValue) | ||
{ | ||
output.AppendLine(); | ||
output.Append(Indent.Repeat(depth)); | ||
} | ||
break; | ||
case ':': | ||
if (quote.HasValue) output.Append(ch); | ||
else output.Append(" : "); | ||
break; | ||
default: | ||
if (quote.HasValue || !char.IsWhiteSpace(ch)) | ||
output.Append(ch); | ||
break; | ||
} | ||
} | ||
|
||
return output.ToString(); | ||
} | ||
} | ||
} |