From 28618121020d8d4fcd87ec3ab75ddda05e127384 Mon Sep 17 00:00:00 2001 From: OMpawar-21 Date: Thu, 2 Jul 2026 16:59:21 +0530 Subject: [PATCH] feat: migrate HTTP layer from Newtonsoft.Json to System.Text.Json Replace all Newtonsoft usage across the HTTP abstraction layer as part of the STJ migration. Renames OpenJObjectResponse() to OpenJsonObjectResponse() returning System.Text.Json.Nodes.JsonObject, swaps JsonSerializer (Newtonsoft) for JsonSerializerOptions throughout the request/response pipeline, and replaces JsonConvert.SerializeObject with JsonSerializer.Serialize for query param building. Files changed: - CMA/Http/IResponse.cs - CMA/Http/ContentstackResponse.cs - CMA/HTTPRequestHandler.cs - CMA/Http/ContentstackHttpRequest.cs --- .../CMA/HTTPRequestHandler.cs | 8 +++----- .../CMA/Http/ContentstackHttpRequest.cs | 12 +++++------ .../CMA/Http/ContentstackResponse.cs | 20 +++++++++---------- .../CMA/Http/IResponse.cs | 4 ++-- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/contentstack.model.generator/CMA/HTTPRequestHandler.cs b/contentstack.model.generator/CMA/HTTPRequestHandler.cs index 9214d28..a805352 100644 --- a/contentstack.model.generator/CMA/HTTPRequestHandler.cs +++ b/contentstack.model.generator/CMA/HTTPRequestHandler.cs @@ -1,5 +1,5 @@ -using Newtonsoft.Json; -using System; +using System; +using System.Text.Json; using System.Collections.Generic; using System.IO; using System.Linq; @@ -14,12 +14,10 @@ namespace contentstack.CMA internal class HttpRequestHandler { private readonly HttpClient _httpClient; - private readonly JsonSerializer _serializer; public HttpRequestHandler() { _httpClient = new HttpClient(); - _serializer = new JsonSerializer(); } public async Task ProcessRequest(string Url, Dictionary Headers, Dictionary BodyJson, string FileName = null) @@ -37,7 +35,7 @@ public async Task ProcessRequest(string Url, Dictionary return value; } else if (kvp.Value is Dictionary) - value = JsonConvert.SerializeObject(kvp.Value); + value = JsonSerializer.Serialize(kvp.Value); else return String.Format("{0}={1}", kvp.Key, kvp.Value); diff --git a/contentstack.model.generator/CMA/Http/ContentstackHttpRequest.cs b/contentstack.model.generator/CMA/Http/ContentstackHttpRequest.cs index 16f19fe..1aff203 100644 --- a/contentstack.model.generator/CMA/Http/ContentstackHttpRequest.cs +++ b/contentstack.model.generator/CMA/Http/ContentstackHttpRequest.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Net; using System.IO; -using Newtonsoft.Json; +using System.Text.Json; namespace contentstack.CMA.Http { @@ -15,7 +15,7 @@ internal class ContentstackHttpRequest : IHttpRequest private bool _disposed = false; private readonly HttpClient _httpClient; private readonly HttpRequestMessage _request; - private readonly JsonSerializer _serializer; + private readonly JsonSerializerOptions _options; #endregion #region Public @@ -52,10 +52,10 @@ public HttpRequestMessage Request #endregion #region Constructor - internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializer serializer) + internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializerOptions options) { _httpClient = httpClient; - _serializer = serializer; + _options = options; _request = new HttpRequestMessage(); } #endregion @@ -124,14 +124,14 @@ public async Task GetResponseAsync() if (responseMessage.StatusCode >= HttpStatusCode.Ambiguous && responseMessage.StatusCode < HttpStatusCode.BadRequest) - return new ContentstackResponse(responseMessage, _serializer); + return new ContentstackResponse(responseMessage, _options); if (!responseMessage.IsSuccessStatusCode) { throw new HttpRequestException($"HTTP request failed with status code: {responseMessage.StatusCode}"); } - return new ContentstackResponse(responseMessage, _serializer); + return new ContentstackResponse(responseMessage, _options); } catch (HttpRequestException httpException) { diff --git a/contentstack.model.generator/CMA/Http/ContentstackResponse.cs b/contentstack.model.generator/CMA/Http/ContentstackResponse.cs index 4a4605e..ca0d813 100644 --- a/contentstack.model.generator/CMA/Http/ContentstackResponse.cs +++ b/contentstack.model.generator/CMA/Http/ContentstackResponse.cs @@ -4,8 +4,8 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; +using System.Text.Json; +using System.Text.Json.Nodes; namespace contentstack.CMA.Http { @@ -20,7 +20,7 @@ public class ContentstackResponse : IResponse, IDisposable Dictionary _headers; HashSet _headerNamesSet; private readonly HttpResponseMessage _response; - private readonly JsonSerializer _serializer; + private readonly JsonSerializerOptions _options; #region Public /// @@ -126,10 +126,10 @@ private void CopyHeaderValues(HttpResponseMessage response) } #endregion - internal ContentstackResponse(HttpResponseMessage response, JsonSerializer serializer) + internal ContentstackResponse(HttpResponseMessage response, JsonSerializerOptions options) { _response = response; - _serializer = serializer; + _options = options; this.StatusCode = response.StatusCode; this.IsSuccessStatusCode = response.IsSuccessStatusCode; @@ -146,11 +146,11 @@ internal ContentstackResponse(HttpResponseMessage response, JsonSerializer seria /// /// Json Object format response. /// - /// The JObject. - public JObject OpenJObjectResponse() + /// The JsonObject. + public JsonObject OpenJsonObjectResponse() { ThrowIfDisposed(); - return JObject.Parse(OpenResponse()); + return JsonNode.Parse(OpenResponse())!.AsObject(); } /// @@ -171,8 +171,8 @@ public string OpenResponse() public TResponse OpenTResponse() { ThrowIfDisposed(); - JObject jObject = OpenJObjectResponse(); - return jObject.ToObject(_serializer); + JsonObject jObject = OpenJsonObjectResponse(); + return JsonSerializer.Deserialize(jObject.ToJsonString(), _options)!; } diff --git a/contentstack.model.generator/CMA/Http/IResponse.cs b/contentstack.model.generator/CMA/Http/IResponse.cs index 87b94d0..868b40f 100644 --- a/contentstack.model.generator/CMA/Http/IResponse.cs +++ b/contentstack.model.generator/CMA/Http/IResponse.cs @@ -1,6 +1,6 @@ using System; using System.Net; -using Newtonsoft.Json.Linq; +using System.Text.Json.Nodes; namespace contentstack.CMA.Http { @@ -19,7 +19,7 @@ public interface IResponse string OpenResponse(); - JObject OpenJObjectResponse(); + JsonObject OpenJsonObjectResponse(); TResponse OpenTResponse(); }