Extension method from SDG for testing

This commit is contained in:
N00MKRAD 2023-12-26 22:59:27 +01:00
parent 355d44a5b3
commit f4bf63b5a7
1 changed files with 15 additions and 0 deletions

View File

@ -11,6 +11,8 @@ using Flowframes.Data;
using System.Management.Automation;
using System.Drawing;
using Flowframes.MiscUtils;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
namespace Flowframes
{
@ -396,5 +398,18 @@ namespace Flowframes
{
return !string.IsNullOrWhiteSpace(s);
}
public static string ToJson(this object o, bool indent = false, bool ignoreErrors = true)
{
var settings = new JsonSerializerSettings();
if (ignoreErrors)
settings.Error = (s, e) => { e.ErrorContext.Handled = true; };
// Serialize enums as strings.
settings.Converters.Add(new StringEnumConverter());
return JsonConvert.SerializeObject(o, indent ? Formatting.Indented : Formatting.None, settings);
}
}
}