37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
using YLErp.Commons;
|
|
|
|
namespace YLErp.Helpers
|
|
{
|
|
public static class JsonBeautify
|
|
{
|
|
/// <summary>
|
|
/// 提供一个简单美化版的数组对象JSON序列化
|
|
/// </summary>
|
|
public static string Serialize<T>(IEnumerable<T> array, bool ignoreNullValue = true, bool camelCase = false) where T : class
|
|
{
|
|
if (array is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(array));
|
|
}
|
|
|
|
var sb = StringBuilderPool.Acquire().Append('[').AppendLine();
|
|
|
|
var sbLastIndex = sb.Length;
|
|
|
|
foreach (var item in array)
|
|
{
|
|
sb.Append(item == null ? "null" : JsonHelper.Serialize(item));
|
|
sbLastIndex = sb.Length;
|
|
sb.Append(',').AppendLine();
|
|
}
|
|
|
|
if (sb.Length > sbLastIndex)
|
|
{
|
|
sb.Remove(sbLastIndex, sb.Length - sbLastIndex).AppendLine();
|
|
}
|
|
|
|
return sb.Append(']').Release();
|
|
}
|
|
}
|
|
}
|