foxcord/Foxcord/Serialization/JsonSerializer.cs

27 lines
1,019 B
C#
Raw Normal View History

2024-07-02 01:14:46 +02:00
using System.Net.Http.Json;
using System.Text.Json;
using DefaultJsonSerializer = System.Text.Json.JsonSerializer;
namespace Foxcord.Serialization;
public static class JsonSerializer
{
private static readonly JsonSerializerOptions Options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true
};
public static string Serialize(object? value) => DefaultJsonSerializer.Serialize(value, Options);
public static byte[] SerializeToUtf8Bytes(object? value) =>
DefaultJsonSerializer.SerializeToUtf8Bytes(value, Options);
public static TValue? Deserialize<TValue>(string json) => DefaultJsonSerializer.Deserialize<TValue>(json, Options);
public static ValueTask<TValue?> DeserializeAsync<TValue>(Stream json) =>
DefaultJsonSerializer.DeserializeAsync<TValue>(json, Options);
public static Task<TValue?> ReadAsJsonAsync<TValue>(this HttpContent content) =>
content.ReadFromJsonAsync<TValue>(Options);
}