about summary refs log tree commit diff
path: root/LibMatrix/Extensions/ObjectExtensions.cs
blob: 085de7d7b9986a5de7c271ca258f93d9cf148825 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System.Text.Encodings.Web;
using System.Text.Json;

namespace LibMatrix.Extensions;

public static class ObjectExtensions {
    public static string ToJson(this object obj, bool indent = true, bool ignoreNull = false, bool unsafeContent = false) {
        var jso = new JsonSerializerOptions();
        if (indent) jso.WriteIndented = true;
        if (ignoreNull) jso.IgnoreNullValues = true;
        if (unsafeContent) jso.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
        return JsonSerializer.Serialize(obj, jso);
    }
}