44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace AyaNova.Util
|
|
{
|
|
|
|
|
|
internal static class JsonUtil
|
|
{
|
|
|
|
/// <summary>
|
|
/// Clean JSON string fragment, remove unnecessary characters
|
|
/// can be called with anything and should handle it properly even empty values etc
|
|
/// </summary>
|
|
/// <param name="jsonIn"></param>
|
|
/// <returns></returns>
|
|
public static string CompactJson(string jsonIn)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(jsonIn))
|
|
{
|
|
return jsonIn;
|
|
}
|
|
if (jsonIn.StartsWith("["))
|
|
{
|
|
JArray j = JArray.Parse(jsonIn);
|
|
return JsonConvert.SerializeObject(j, Formatting.None);
|
|
}
|
|
|
|
if (jsonIn.StartsWith("{"))
|
|
{
|
|
JObject j = JObject.Parse(jsonIn);
|
|
return JsonConvert.SerializeObject(j, Formatting.None);
|
|
}
|
|
|
|
//Not an object or an array so just return it, possibly suspect, maybe log this shit
|
|
return jsonIn;
|
|
}
|
|
|
|
|
|
}//eoc
|
|
|
|
}//eons |