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")]
|
[JsonDerivedType(typeof(LookupMethod), "lookup")]
|
||||||
public class Method;
|
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
|
public abstract class Method<TParam>(TParam parameters) : Method
|
||||||
where TParam : MethodParameters
|
where TParam : MethodParameters
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace pdns_dhcp.PowerDns;
|
namespace pdns_dhcp.PowerDns;
|
||||||
|
|
||||||
|
[JsonDerivedType(typeof(InitializeParameters))]
|
||||||
|
[JsonDerivedType(typeof(LookupParameters))]
|
||||||
public record class Parameters;
|
public record class Parameters;
|
||||||
|
|
||||||
public record class MethodParameters : Parameters
|
public record class MethodParameters : Parameters
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ public class PowerDnsHandler : ConnectionHandler
|
||||||
buffer.Write(memory.Span);
|
buffer.Write(memory.Span);
|
||||||
if (ConsumeJson(buffer, json, ref state))
|
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();
|
json.Clear();
|
||||||
state = default;
|
state = default;
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ public class PowerDnsHandler : ConnectionHandler
|
||||||
}
|
}
|
||||||
catch (Exception e) { }
|
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);
|
.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))]
|
[JsonDerivedType(typeof(LookupReply))]
|
||||||
public abstract class Reply;
|
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
|
public abstract class Reply<T>(T result) : Reply
|
||||||
{
|
{
|
||||||
[JsonPropertyName("result")]
|
[JsonPropertyName("result")]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue