From 02789185d251672b8cebe53b64a3446c805bc394 Mon Sep 17 00:00:00 2001 From: Mkatari3700 <58773431+Mkatari3700@users.noreply.github.com> Date: Tue, 13 Feb 2024 08:39:12 +0530 Subject: [PATCH] Serialize Customization and usage (#104) * Log changes * Serialization settings and examples to use it --------- Co-authored-by: Katari.Manikanta --- Conductor/Client/ApiClient.cs | 4 ++-- Conductor/Client/Configuration.cs | 16 ++++++++++++++- csharp-examples/WorkFlowExamples.cs | 31 +++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Conductor/Client/ApiClient.cs b/Conductor/Client/ApiClient.cs index 84a2360..29876f4 100644 --- a/Conductor/Client/ApiClient.cs +++ b/Conductor/Client/ApiClient.cs @@ -16,7 +16,7 @@ namespace Conductor.Client /// public partial class ApiClient { - private JsonSerializerSettings serializerSettings = new JsonSerializerSettings + public JsonSerializerSettings serializerSettings = new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }; @@ -341,7 +341,7 @@ public String Serialize(object obj) { try { - return obj != null ? JsonConvert.SerializeObject(obj) : null; + return obj != null ? JsonConvert.SerializeObject(obj, serializerSettings) : null; } catch (Exception e) { diff --git a/Conductor/Client/Configuration.cs b/Conductor/Client/Configuration.cs index f907b0c..f863236 100644 --- a/Conductor/Client/Configuration.cs +++ b/Conductor/Client/Configuration.cs @@ -5,7 +5,7 @@ using System.IO; using System; using RestSharp; -using System.Net.Http; +using Newtonsoft.Json; namespace Conductor.Client { @@ -93,6 +93,20 @@ public Configuration(int? timeOut = null) DefaultHeader = new ConcurrentDictionary(); } + /// + /// Initializes a new instance of the class + /// + /// JsonSerializerSettings setting custom serialization properties + /// Optional rest client request time out + public Configuration(JsonSerializerSettings serializerSettings, int? timeOut = null) + { + Timeout = timeOut ?? Timeout; + ApiClient = new ApiClient(Timeout); + ApiClient.serializerSettings = serializerSettings; + BasePath = "https://play.orkes.io/api"; + DefaultHeader = new ConcurrentDictionary(); + } + #endregion Constructors #region Properties diff --git a/csharp-examples/WorkFlowExamples.cs b/csharp-examples/WorkFlowExamples.cs index 8f34dc5..c359586 100644 --- a/csharp-examples/WorkFlowExamples.cs +++ b/csharp-examples/WorkFlowExamples.cs @@ -4,7 +4,7 @@ using Conductor.Executor; using Conductor.Api; using Conductor.Client.Authentication; -using Conductor.Client.Extensions; +using Newtonsoft.Json; namespace csharp_examples { @@ -30,12 +30,39 @@ public class WorkFlowExamples public void RegisterWorkFlow() { - Configuration configuration = new Configuration(REST_CLIENT_REQUEST_TIME_OUT) + // Method-1 for using custom serialization settings - START + + // Step 1:- Prepare JsonSerializer Settings with certain properties + JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore + }; + + // Step 2:- Use overloaded constructor of Configuration and pass the JsonSerializer Settings + // Restclient internally use the settings to serialize on request/response while making a call to serialize/deserialize + Configuration configuration = new Configuration(jsonSerializerSettings, REST_CLIENT_REQUEST_TIME_OUT) { AuthenticationSettings = new OrkesAuthenticationSettings(KEY_ID, KEY_SECRET) }; + WorkflowExecutor executor = new WorkflowExecutor(configuration); executor.RegisterWorkflow(GetConductorWorkflow(), true); + // Method-1 for using custom serialization settings - END + + + // Method-2 for using custom serialization settings - START + /* + Configuration configuration = new Configuration(); + configuration.ApiClient.serializerSettings = new JsonSerializerSettings() + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore + }; + WorkflowExecutor executor = new WorkflowExecutor(configuration); + executor.RegisterWorkflow(GetConductorWorkflow(), true); + */ + // Method-2 for using custom serialization settings - END } private ConductorWorkflow GetConductorWorkflow()