Skip to content

Commit 4593fc1

Browse files
authored
Merge pull request #41 from contentstack/enhc/DX-9374
feat: migrate HTTP layer from Newtonsoft.Json to System.Text.Json
2 parents 216d181 + 2861812 commit 4593fc1

4 files changed

Lines changed: 21 additions & 23 deletions

File tree

‎contentstack.model.generator/CMA/HTTPRequestHandler.cs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Newtonsoft.Json;
2-
using System;
1+
using System;
2+
using System.Text.Json;
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
@@ -14,12 +14,10 @@ namespace contentstack.CMA
1414
internal class HttpRequestHandler
1515
{
1616
private readonly HttpClient _httpClient;
17-
private readonly JsonSerializer _serializer;
1817

1918
public HttpRequestHandler()
2019
{
2120
_httpClient = new HttpClient();
22-
_serializer = new JsonSerializer();
2321
}
2422

2523
public async Task<string> ProcessRequest(string Url, Dictionary<string, object> Headers, Dictionary<string, object> BodyJson, string FileName = null)
@@ -37,7 +35,7 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
3735
return value;
3836
}
3937
else if (kvp.Value is Dictionary<string, object>)
40-
value = JsonConvert.SerializeObject(kvp.Value);
38+
value = JsonSerializer.Serialize(kvp.Value);
4139
else
4240
return String.Format("{0}={1}", kvp.Key, kvp.Value);
4341

‎contentstack.model.generator/CMA/Http/ContentstackHttpRequest.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Net;
77
using System.IO;
8-
using Newtonsoft.Json;
8+
using System.Text.Json;
99

1010
namespace contentstack.CMA.Http
1111
{
@@ -15,7 +15,7 @@ internal class ContentstackHttpRequest : IHttpRequest
1515
private bool _disposed = false;
1616
private readonly HttpClient _httpClient;
1717
private readonly HttpRequestMessage _request;
18-
private readonly JsonSerializer _serializer;
18+
private readonly JsonSerializerOptions _options;
1919
#endregion
2020

2121
#region Public
@@ -52,10 +52,10 @@ public HttpRequestMessage Request
5252
#endregion
5353

5454
#region Constructor
55-
internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializer serializer)
55+
internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializerOptions options)
5656
{
5757
_httpClient = httpClient;
58-
_serializer = serializer;
58+
_options = options;
5959
_request = new HttpRequestMessage();
6060
}
6161
#endregion
@@ -124,14 +124,14 @@ public async Task<IResponse> GetResponseAsync()
124124

125125
if (responseMessage.StatusCode >= HttpStatusCode.Ambiguous &&
126126
responseMessage.StatusCode < HttpStatusCode.BadRequest)
127-
return new ContentstackResponse(responseMessage, _serializer);
127+
return new ContentstackResponse(responseMessage, _options);
128128

129129
if (!responseMessage.IsSuccessStatusCode)
130130
{
131131
throw new HttpRequestException($"HTTP request failed with status code: {responseMessage.StatusCode}");
132132
}
133133

134-
return new ContentstackResponse(responseMessage, _serializer);
134+
return new ContentstackResponse(responseMessage, _options);
135135
}
136136
catch (HttpRequestException httpException)
137137
{

‎contentstack.model.generator/CMA/Http/ContentstackResponse.cs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.Net;
55
using System.Net.Http;
66
using System.Net.Http.Headers;
7-
using Newtonsoft.Json;
8-
using Newtonsoft.Json.Linq;
7+
using System.Text.Json;
8+
using System.Text.Json.Nodes;
99

1010
namespace contentstack.CMA.Http
1111
{
@@ -20,7 +20,7 @@ public class ContentstackResponse : IResponse, IDisposable
2020
Dictionary<string, string> _headers;
2121
HashSet<string> _headerNamesSet;
2222
private readonly HttpResponseMessage _response;
23-
private readonly JsonSerializer _serializer;
23+
private readonly JsonSerializerOptions _options;
2424

2525
#region Public
2626
/// <summary>
@@ -126,10 +126,10 @@ private void CopyHeaderValues(HttpResponseMessage response)
126126
}
127127
#endregion
128128

129-
internal ContentstackResponse(HttpResponseMessage response, JsonSerializer serializer)
129+
internal ContentstackResponse(HttpResponseMessage response, JsonSerializerOptions options)
130130
{
131131
_response = response;
132-
_serializer = serializer;
132+
_options = options;
133133

134134
this.StatusCode = response.StatusCode;
135135
this.IsSuccessStatusCode = response.IsSuccessStatusCode;
@@ -146,11 +146,11 @@ internal ContentstackResponse(HttpResponseMessage response, JsonSerializer seria
146146
/// <summary>
147147
/// Json Object format response.
148148
/// </summary>
149-
/// <returns>The JObject.</returns>
150-
public JObject OpenJObjectResponse()
149+
/// <returns>The JsonObject.</returns>
150+
public JsonObject OpenJsonObjectResponse()
151151
{
152152
ThrowIfDisposed();
153-
return JObject.Parse(OpenResponse());
153+
return JsonNode.Parse(OpenResponse())!.AsObject();
154154
}
155155

156156
/// <summary>
@@ -171,8 +171,8 @@ public string OpenResponse()
171171
public TResponse OpenTResponse<TResponse>()
172172
{
173173
ThrowIfDisposed();
174-
JObject jObject = OpenJObjectResponse();
175-
return jObject.ToObject<TResponse>(_serializer);
174+
JsonObject jObject = OpenJsonObjectResponse();
175+
return JsonSerializer.Deserialize<TResponse>(jObject.ToJsonString(), _options)!;
176176
}
177177

178178

‎contentstack.model.generator/CMA/Http/IResponse.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Net;
3-
using Newtonsoft.Json.Linq;
3+
using System.Text.Json.Nodes;
44

55
namespace contentstack.CMA.Http
66
{
@@ -19,7 +19,7 @@ public interface IResponse
1919

2020
string OpenResponse();
2121

22-
JObject OpenJObjectResponse();
22+
JsonObject OpenJsonObjectResponse();
2323

2424
TResponse OpenTResponse<TResponse>();
2525
}

0 commit comments

Comments
 (0)