是否可以使用Newtonsoft JSON.NET库缩小/格式化JSON字符串而不强制系统重新解析代码?这就是我的方法:
public async Task<string> Minify(string json)
{
// TODO: Some way to do this without a re-parse?
var jsonObj = await JsonOpener.GetJsonFromString(json);
return jsonObj.ToString(Formatting.None);
}
public async Task<string> Beautify(string json)
{
// TODO: Some way to do this without a re-parse?
var jsonObj = await JsonOpener.GetJsonFromString(json);
return FormatJson(jsonObj);
}
private string FormatJson(JToken input)
{
// We could just do input.ToString(Formatting.Indented), but this allows us
// to take advantage of JsonTextWriter's formatting options.
using (var stringWriter = new StringWriter(new StringBuilder())) …Run Code Online (Sandbox Code Playgroud)