Use compound json serializer context
This commit is contained in:
parent
fe275467e8
commit
6dbcb38ce2
5 changed files with 18 additions and 16 deletions
|
|
@ -9,13 +9,6 @@ public interface IMethod;
|
|||
[JsonDerivedType(typeof(LookupMethod), "lookup")]
|
||||
public class Method;
|
||||
|
||||
[JsonSerializable(typeof(Method)), JsonSourceGenerationOptions(
|
||||
GenerationMode = JsonSourceGenerationMode.Metadata,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString,
|
||||
UseStringEnumConverter = true
|
||||
)]
|
||||
internal partial class MethodContext : JsonSerializerContext;
|
||||
|
||||
public abstract class Method<TParam>(TParam parameters) : Method
|
||||
where TParam : MethodParameters
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ using System.Text.Json.Serialization;
|
|||
|
||||
namespace pdns_dhcp.PowerDns;
|
||||
|
||||
[JsonDerivedType(typeof(InitializeParameters))]
|
||||
[JsonDerivedType(typeof(LookupParameters))]
|
||||
public record class Parameters;
|
||||
|
||||
public record class MethodParameters : Parameters
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ public class PowerDnsHandler : ConnectionHandler
|
|||
buffer.Write(memory.Span);
|
||||
if (ConsumeJson(buffer, json, ref state))
|
||||
{
|
||||
var method = JsonSerializer.Deserialize(json.WrittenSpan, MethodContext.Default.Method)!;
|
||||
var method = JsonSerializer.Deserialize(json.WrittenSpan, PowerDnsSerializerContext.Default.Method)!;
|
||||
json.Clear();
|
||||
state = default;
|
||||
|
||||
|
|
@ -53,7 +53,7 @@ public class PowerDnsHandler : ConnectionHandler
|
|||
}
|
||||
catch (Exception e) { }
|
||||
|
||||
await JsonSerializer.SerializeAsync(writer, reply, ReplyContext.Default.Reply, connection.ConnectionClosed)
|
||||
await JsonSerializer.SerializeAsync(writer, reply, PowerDnsSerializerContext.Default.Reply, connection.ConnectionClosed)
|
||||
.ConfigureAwait(continueOnCapturedContext: false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
src/pdns-dhcp/PowerDns/PowerDnsSerializerContext.cs
Normal file
14
src/pdns-dhcp/PowerDns/PowerDnsSerializerContext.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace pdns_dhcp.PowerDns;
|
||||
|
||||
[JsonSerializable(typeof(Reply))]
|
||||
[JsonSerializable(typeof(Method))]
|
||||
[JsonSerializable(typeof(Parameters))]
|
||||
[JsonSourceGenerationOptions(
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString,
|
||||
UseStringEnumConverter = true,
|
||||
WriteIndented = false
|
||||
)]
|
||||
internal partial class PowerDnsSerializerContext : JsonSerializerContext;
|
||||
|
|
@ -6,13 +6,6 @@ namespace pdns_dhcp.PowerDns;
|
|||
[JsonDerivedType(typeof(LookupReply))]
|
||||
public abstract class Reply;
|
||||
|
||||
[JsonSerializable(typeof(Reply)), JsonSourceGenerationOptions(
|
||||
GenerationMode = JsonSourceGenerationMode.Metadata,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
WriteIndented = false
|
||||
)]
|
||||
internal partial class ReplyContext : JsonSerializerContext;
|
||||
|
||||
public abstract class Reply<T>(T result) : Reply
|
||||
{
|
||||
[JsonPropertyName("result")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue