Implemented DLSiteClient.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using JSMR.Infrastructure.Integrations.DLSite.Models;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
||||
|
||||
[JsonSerializable(typeof(ProductInfoCollection))]
|
||||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
|
||||
public partial class DLSiteJsonContext : JsonSerializerContext
|
||||
{
|
||||
public static JsonSerializerOptions CreateOptions()
|
||||
{
|
||||
JsonSerializerOptions options = new(JsonSerializerDefaults.Web);
|
||||
options.Converters.Add(new NumberConverter());
|
||||
options.Converters.Add(new DictionaryConverter<string, decimal>());
|
||||
options.Converters.Add(new DictionaryConverter<string, string>());
|
||||
options.Converters.Add(new DateTimeConverterUsingDateTimeParse());
|
||||
options.Converters.Add(new NullableDateTimeConverterUsingDateTimeParse());
|
||||
options.Converters.Add(new ProductInfoOptionsConverter());
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
||||
|
||||
public class DateTimeConverterUsingDateTimeParse : JsonConverter<DateTime>
|
||||
{
|
||||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return DateTime.Parse(reader.GetString() ?? string.Empty);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString() ?? string.Empty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
||||
|
||||
public sealed class DictionaryConverter<TKey, TValue> : JsonConverter<Dictionary<TKey, TValue>> where TKey : notnull
|
||||
{
|
||||
public override Dictionary<TKey, TValue>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
=> JsonSerializer.Deserialize<Dictionary<TKey, TValue>>(ref reader, options);
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Dictionary<TKey, TValue> value, JsonSerializerOptions options)
|
||||
=> JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
||||
|
||||
public sealed class NullableDateTimeConverterUsingDateTimeParse : JsonConverter<DateTime?>
|
||||
{
|
||||
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType is JsonTokenType.Null)
|
||||
return null;
|
||||
|
||||
if (reader.TokenType is JsonTokenType.String)
|
||||
{
|
||||
var s = reader.GetString();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(s))
|
||||
return null;
|
||||
|
||||
if (DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var dt))
|
||||
return dt;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value is DateTime dt)
|
||||
writer.WriteStringValue(dt);
|
||||
else
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
||||
|
||||
public sealed class NumberConverter : JsonConverter<int>
|
||||
{
|
||||
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return reader.TokenType switch
|
||||
{
|
||||
JsonTokenType.Number => reader.TryGetInt32(out var i) ? i : 0,
|
||||
JsonTokenType.String => int.TryParse(reader.GetString(), out var j) ? j : 0,
|
||||
JsonTokenType.False => 0,
|
||||
JsonTokenType.True => 1,
|
||||
JsonTokenType.Null => 0,
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
|
||||
=> writer.WriteNumberValue(value);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
||||
|
||||
public sealed class ProductInfoOptionsConverter : JsonConverter<string[]>
|
||||
{
|
||||
public override string[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
string value = reader.GetString() ?? string.Empty;
|
||||
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return [];
|
||||
|
||||
return [.. value.Split('#')];
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, string[] value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(string.Join("#", value));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user