From 9f14ccd243552b1113d6c319db89c0a264abdbd9 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Fri, 3 Jul 2026 15:02:54 +0530 Subject: [PATCH] refactor: update serializer settings to use JsonSerializerOptions in ContentstackClient --- .../ContentstackClientTests.cs | 10 +-- .../CMA/ContentstackClient.cs | 62 +++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/contentstack.model.generator.tests/ContentstackClientTests.cs b/contentstack.model.generator.tests/ContentstackClientTests.cs index 677e080..2ce54c6 100644 --- a/contentstack.model.generator.tests/ContentstackClientTests.cs +++ b/contentstack.model.generator.tests/ContentstackClientTests.cs @@ -59,20 +59,20 @@ public void Constructor_ShouldHandleNullOptions() } [Fact] - public void Constructor_ShouldSetSerializerSettings() + public void Constructor_ShouldSetSerializerOptions() { - + var options = new ContentstackOptions { ApiKey = "test_api_key", Host = "api.contentstack.io" }; - + var client = new ContentstackClient(options); - - Assert.NotNull(client.SerializerSettings); + + Assert.NotNull(client.SerializerOptions); } [Theory] diff --git a/contentstack.model.generator/CMA/ContentstackClient.cs b/contentstack.model.generator/CMA/ContentstackClient.cs index 86bd4a7..b5feb70 100644 --- a/contentstack.model.generator/CMA/ContentstackClient.cs +++ b/contentstack.model.generator/CMA/ContentstackClient.cs @@ -1,12 +1,12 @@ using System; using System.Collections.Generic; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using System.Linq; using System.Threading.Tasks; using System.Net; using System.IO; -using System.Collections; using Contentstack.Model.Generator.Model; using contentstack.model.generator.Model; @@ -14,7 +14,13 @@ namespace contentstack.CMA { public class ContentstackClient { - public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings(); + public JsonSerializerOptions SerializerOptions { get; set; } = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + AllowTrailingCommas = true, + NumberHandling = JsonNumberHandling.AllowReadingFromString, + }; #region Internal Variables @@ -131,19 +137,18 @@ internal static ContentstackException GetContentstackException(Exception ex) using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); - JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); - - JToken token = data["error_code"]; - if (token != null) - errorCode = token.Value(); + var node = JsonNode.Parse(errorMessage.Replace("\r\n", "")); + if (node is JsonObject data) + { + if (data["error_code"] is JsonValue ec) + errorCode = ec.GetValue(); - token = data["error_message"]; - if (token != null) - errorMessage = token.Value(); + if (data["error_message"] is JsonValue em) + errorMessage = em.GetValue(); - token = data["errors"]; - if (token != null) - errors = token.ToObject>(); + if (data["errors"] is JsonObject errs) + errors = JsonSerializer.Deserialize>(errs.ToJsonString()); + } var response = exResp as HttpWebResponse; if (response != null) @@ -206,11 +211,8 @@ public async Task GetStack() { HttpRequestHandler RequestHandler = new HttpRequestHandler(); var outputResult = await RequestHandler.ProcessRequest(_StackUrl, headers, mainJson); - JObject data = JsonConvert.DeserializeObject(outputResult.Replace("\r\n", ""), this.SerializerSettings); - SerializerSettings.DateFormatString = "yyyy-MM-dd"; - var stack = data["stack"]; - var stackJson = JsonConvert.SerializeObject(stack); - return JsonConvert.DeserializeObject(stackJson, this.SerializerSettings); + var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject(); + return JsonSerializer.Deserialize(root["stack"]!.ToJsonString(), this.SerializerOptions); } catch (Exception ex) { @@ -245,13 +247,11 @@ public async Task GetContentTypes(int skip = 0) { HttpRequestHandler RequestHandler = new HttpRequestHandler(); var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson); - JObject data = JsonConvert.DeserializeObject(outputResult.Replace("\r\n", ""), this.SerializerSettings); - IList contentTypes = (IList)data["content_types"]; + var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject(); ContentstackResponse contentstackResponse = new ContentstackResponse(); - var ContentTypeJson = JsonConvert.SerializeObject(contentTypes); - contentstackResponse.listContentTypes = JsonConvert.DeserializeObject>(ContentTypeJson); - if (data["count"] != null) { - contentstackResponse.Count = (int)data["count"]; + contentstackResponse.listContentTypes = JsonSerializer.Deserialize>(root["content_types"]!.ToJsonString(), this.SerializerOptions); + if (root["count"] is JsonValue count) { + contentstackResponse.Count = count.GetValue(); } return contentstackResponse; } @@ -286,13 +286,11 @@ public async Task GetGlobalFields(int skip = 0) { HttpRequestHandler RequestHandler = new HttpRequestHandler(); var outputResult = await RequestHandler.ProcessRequest(_GlobalFieldsUrl, headers, mainJson); - JObject data = JsonConvert.DeserializeObject(outputResult.Replace("\r\n", ""), this.SerializerSettings); - IList globalFields = (IList)data["global_fields"]; + var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject(); ContentstackResponse contentstackResponse = new ContentstackResponse(); - var ContentTypeJson = JsonConvert.SerializeObject(globalFields); - contentstackResponse.listContentTypes = JsonConvert.DeserializeObject>(ContentTypeJson); - if (data["count"] != null) { - contentstackResponse.Count = (int)data["count"]; + contentstackResponse.listContentTypes = JsonSerializer.Deserialize>(root["global_fields"]!.ToJsonString(), this.SerializerOptions); + if (root["count"] is JsonValue count) { + contentstackResponse.Count = count.GetValue(); } return contentstackResponse; }