Skip to content

Added beautiful option for json #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 114 additions & 2 deletions src/JSONWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
Expand All @@ -13,11 +14,13 @@ namespace TinyJson
//- Will only output public fields and property getters on objects
public static class JSONWriter
{
public static string ToJson(this object item)
public static string ToJson(this object item, bool makeBeautiful = false)
{
StringBuilder stringBuilder = new StringBuilder();
AppendValue(stringBuilder, item);
return stringBuilder.ToString();
return makeBeautiful ?
MakeBeautiful(stringBuilder.ToString().ToArray()).ToString() :
stringBuilder.ToString();
}

static void AppendValue(StringBuilder stringBuilder, object item)
Expand Down Expand Up @@ -173,5 +176,114 @@ static string GetMemberName(MemberInfo member)

return member.Name;
}

private static string GetTabs(int level)
{
if (level == 0)
return null;


var chars = new char[level];
for (int i = 0; i < chars.Length; i++)
{
chars[i] = '\t';
}

return new string(chars);
}

private static StringBuilder MakeBeautiful(char[] chars)
{
var newStringBuilder = new StringBuilder();
var tabs = 0;

//char? chrRight;
char? chrLeft = null;
for (var i = 0; i < chars.Length; i++)
{
var chr = chars[i];
switch (chr)
{
case '[':
if (chrLeft.HasValue && chrLeft == ',')
{
newStringBuilder.Append('\n');
newStringBuilder.Append($"{GetTabs(tabs)}{chr}");
}
else
{
newStringBuilder.Append(chr);
}

tabs++;
break;
case ']':
tabs--;
newStringBuilder.Append($"\n{GetTabs(tabs)}{chr}");
break;
case '{':
if (chrLeft.HasValue && (chrLeft == ',' || chrLeft == '['))
{
newStringBuilder.Append('\n');
newStringBuilder.Append($"{GetTabs(tabs)}{chr}");
}
else
{
newStringBuilder.Append(chr);
}

tabs++;
break;
case '}':
tabs--;
newStringBuilder.Append($"\n{GetTabs(tabs)}{chr}");
break;
case '"':
if (chrLeft.HasValue && (chrLeft == '{' || chrLeft == '[' || chrLeft == ',' || chrLeft == ':'))
{
if (chrLeft != ':')
{
newStringBuilder.Append('\n');
newStringBuilder.Append($"{GetTabs(tabs)}{chr}");
}
else
{
newStringBuilder.Append(chr);
}


var nextIndex = GetNextChar(chars, i + 1);
newStringBuilder.Append(new string(chars, i + 1, nextIndex - i - 1));
i = nextIndex - 1;
chr = chars[i];
}
else
{
newStringBuilder.Append(chr);
}

break;
default:
newStringBuilder.Append(chr);
break;
}


chrLeft = chr;
}
return newStringBuilder;
}

private static int GetNextChar(char[] chars, in int start)
{
for (var i = start; i < chars.Length; i++)
{
var chr = chars[i];
if (chr == '"' && chars[i - 2] != '\\')
return i;
}

return -1;
}
}
}