From 4eb24a746fc93654a61b9581d35286e58b2905ef Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Fri, 3 Jan 2025 16:45:53 +0300 Subject: [PATCH] Configure JSON serializer --- Consul/Client.cs | 6 ++++++ Consul/Client_Request.cs | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Consul/Client.cs b/Consul/Client.cs index 89fa487d..8d312215 100644 --- a/Consul/Client.cs +++ b/Consul/Client.cs @@ -23,6 +23,7 @@ using System.Net.Http.Headers; using System.Security.Cryptography.X509Certificates; using Consul.Filtering; +using Newtonsoft.Json; namespace Consul { @@ -62,6 +63,11 @@ internal bool DisableServerCertificateValidation } } + /// + /// Allows to configure the serialization settings for a Consul client. + /// + public Action ConfigureSerializerSettings { get; set; } + /// /// The Uri to connect to the Consul agent. /// diff --git a/Consul/Client_Request.cs b/Consul/Client_Request.cs index b2f41a2c..cb70cb05 100644 --- a/Consul/Client_Request.cs +++ b/Consul/Client_Request.cs @@ -57,7 +57,7 @@ public abstract class ConsulRequest internal Stream ResponseStream { get; set; } internal string Endpoint { get; set; } - internal readonly JsonSerializer _serializer = new JsonSerializer(); + internal readonly JsonSerializer _serializer; internal ConsulRequest(ConsulClient client, string url, HttpMethod method) { @@ -78,6 +78,17 @@ internal ConsulRequest(ConsulClient client, string url, HttpMethod method) { Params["ns"] = client.Config.Namespace; } + + if (client.Config.ConfigureSerializerSettings == null) + { + _serializer = new JsonSerializer(); + } + else + { + var settings = new JsonSerializerSettings(); + client.Config.ConfigureSerializerSettings(settings); + _serializer = JsonSerializer.Create(settings); + } } protected abstract void ApplyOptions(ConsulClientConfiguration clientConfig);