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

namespace MatrixRoomUtils.Core.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);
    }
}