mirror of
https://github.com/ckaczor/ChrisKaczor.FeverClient.git
synced 2026-02-16 10:58:32 -05:00
Initial commit
This commit is contained in:
25
Converters/BoolConverter.cs
Normal file
25
Converters/BoolConverter.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.FeverClient.Converters
|
||||
{
|
||||
public class BoolTimestampConverter : JsonConverter<bool>
|
||||
{
|
||||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.Number)
|
||||
{
|
||||
throw new JsonException("Expected a numeric boolean value");
|
||||
}
|
||||
|
||||
var number = reader.GetInt64();
|
||||
return number == 1;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
{
|
||||
var number = value ? 1 : 0;
|
||||
writer.WriteNumberValue(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Converters/CommaSeparatedListConverter.cs
Normal file
25
Converters/CommaSeparatedListConverter.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.FeverClient.Converters;
|
||||
|
||||
internal class CommaSeparatedListConverter : JsonConverter<IEnumerable<int>>
|
||||
{
|
||||
public override IEnumerable<int> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.String)
|
||||
{
|
||||
throw new JsonException("Expected a string");
|
||||
}
|
||||
|
||||
var commaSeparatedList = reader.GetString() ?? string.Empty;
|
||||
|
||||
return commaSeparatedList.Split(",").Select(int.Parse).AsEnumerable();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IEnumerable<int> value, JsonSerializerOptions options)
|
||||
{
|
||||
var commaSeparatedList = string.Join(",", value);
|
||||
writer.WriteStringValue(commaSeparatedList);
|
||||
}
|
||||
}
|
||||
24
Converters/EpochConverter.cs
Normal file
24
Converters/EpochConverter.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.FeverClient.Converters;
|
||||
|
||||
public class EpochConverter : JsonConverter<DateTimeOffset>
|
||||
{
|
||||
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.Number)
|
||||
{
|
||||
throw new JsonException("Expected a numeric epoch timestamp");
|
||||
}
|
||||
|
||||
var unixTimestamp = reader.GetInt64();
|
||||
return DateTimeOffset.FromUnixTimeSeconds(unixTimestamp);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
|
||||
{
|
||||
var unixTimestamp = value.ToUnixTimeSeconds();
|
||||
writer.WriteNumberValue(unixTimestamp);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user