Skip to content

Commit 2489a3a

Browse files
authored
Merge pull request #43 from contentstack/enh/DX-9377-client-migration
refactor: update serializer settings to use JsonSerializerOptions in ContentstackClient
2 parents 4593fc1 + 9f14ccd commit 2489a3a

2 files changed

Lines changed: 35 additions & 37 deletions

File tree

‎contentstack.model.generator.tests/ContentstackClientTests.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ public void Constructor_ShouldHandleNullOptions()
5959
}
6060

6161
[Fact]
62-
public void Constructor_ShouldSetSerializerSettings()
62+
public void Constructor_ShouldSetSerializerOptions()
6363
{
64-
64+
6565
var options = new ContentstackOptions
6666
{
6767
ApiKey = "test_api_key",
6868
Host = "api.contentstack.io"
6969
};
7070

71-
71+
7272
var client = new ContentstackClient(options);
7373

74-
75-
Assert.NotNull(client.SerializerSettings);
74+
75+
Assert.NotNull(client.SerializerOptions);
7676
}
7777

7878
[Theory]

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

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
using System;
22
using System.Collections.Generic;
3-
using Newtonsoft.Json.Linq;
4-
using Newtonsoft.Json;
3+
using System.Text.Json;
4+
using System.Text.Json.Nodes;
5+
using System.Text.Json.Serialization;
56
using System.Linq;
67
using System.Threading.Tasks;
78
using System.Net;
89
using System.IO;
9-
using System.Collections;
1010
using Contentstack.Model.Generator.Model;
1111
using contentstack.model.generator.Model;
1212

1313
namespace contentstack.CMA
1414
{
1515
public class ContentstackClient
1616
{
17-
public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings();
17+
public JsonSerializerOptions SerializerOptions { get; set; } = new JsonSerializerOptions
18+
{
19+
PropertyNameCaseInsensitive = true,
20+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
21+
AllowTrailingCommas = true,
22+
NumberHandling = JsonNumberHandling.AllowReadingFromString,
23+
};
1824

1925
#region Internal Variables
2026

@@ -131,19 +137,18 @@ internal static ContentstackException GetContentstackException(Exception ex)
131137
using (var reader = new StreamReader(stream))
132138
{
133139
errorMessage = reader.ReadToEnd();
134-
JObject data = JObject.Parse(errorMessage.Replace("\r\n", ""));
135-
136-
JToken token = data["error_code"];
137-
if (token != null)
138-
errorCode = token.Value<int>();
140+
var node = JsonNode.Parse(errorMessage.Replace("\r\n", ""));
141+
if (node is JsonObject data)
142+
{
143+
if (data["error_code"] is JsonValue ec)
144+
errorCode = ec.GetValue<int>();
139145

140-
token = data["error_message"];
141-
if (token != null)
142-
errorMessage = token.Value<string>();
146+
if (data["error_message"] is JsonValue em)
147+
errorMessage = em.GetValue<string>();
143148

144-
token = data["errors"];
145-
if (token != null)
146-
errors = token.ToObject<Dictionary<string, object>>();
149+
if (data["errors"] is JsonObject errs)
150+
errors = JsonSerializer.Deserialize<Dictionary<string, object>>(errs.ToJsonString());
151+
}
147152

148153
var response = exResp as HttpWebResponse;
149154
if (response != null)
@@ -206,11 +211,8 @@ public async Task<StackResponse> GetStack()
206211
{
207212
HttpRequestHandler RequestHandler = new HttpRequestHandler();
208213
var outputResult = await RequestHandler.ProcessRequest(_StackUrl, headers, mainJson);
209-
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
210-
SerializerSettings.DateFormatString = "yyyy-MM-dd";
211-
var stack = data["stack"];
212-
var stackJson = JsonConvert.SerializeObject(stack);
213-
return JsonConvert.DeserializeObject<StackResponse>(stackJson, this.SerializerSettings);
214+
var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject();
215+
return JsonSerializer.Deserialize<StackResponse>(root["stack"]!.ToJsonString(), this.SerializerOptions);
214216
}
215217
catch (Exception ex)
216218
{
@@ -245,13 +247,11 @@ public async Task<ContentstackResponse> GetContentTypes(int skip = 0)
245247
{
246248
HttpRequestHandler RequestHandler = new HttpRequestHandler();
247249
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson);
248-
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
249-
IList contentTypes = (IList)data["content_types"];
250+
var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject();
250251
ContentstackResponse contentstackResponse = new ContentstackResponse();
251-
var ContentTypeJson = JsonConvert.SerializeObject(contentTypes);
252-
contentstackResponse.listContentTypes = JsonConvert.DeserializeObject<List<Contenttype>>(ContentTypeJson);
253-
if (data["count"] != null) {
254-
contentstackResponse.Count = (int)data["count"];
252+
contentstackResponse.listContentTypes = JsonSerializer.Deserialize<List<Contenttype>>(root["content_types"]!.ToJsonString(), this.SerializerOptions);
253+
if (root["count"] is JsonValue count) {
254+
contentstackResponse.Count = count.GetValue<int>();
255255
}
256256
return contentstackResponse;
257257
}
@@ -286,13 +286,11 @@ public async Task<ContentstackResponse> GetGlobalFields(int skip = 0)
286286
{
287287
HttpRequestHandler RequestHandler = new HttpRequestHandler();
288288
var outputResult = await RequestHandler.ProcessRequest(_GlobalFieldsUrl, headers, mainJson);
289-
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
290-
IList globalFields = (IList)data["global_fields"];
289+
var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject();
291290
ContentstackResponse contentstackResponse = new ContentstackResponse();
292-
var ContentTypeJson = JsonConvert.SerializeObject(globalFields);
293-
contentstackResponse.listContentTypes = JsonConvert.DeserializeObject<List<Contenttype>>(ContentTypeJson);
294-
if (data["count"] != null) {
295-
contentstackResponse.Count = (int)data["count"];
291+
contentstackResponse.listContentTypes = JsonSerializer.Deserialize<List<Contenttype>>(root["global_fields"]!.ToJsonString(), this.SerializerOptions);
292+
if (root["count"] is JsonValue count) {
293+
contentstackResponse.Count = count.GetValue<int>();
296294
}
297295
return contentstackResponse;
298296
}

0 commit comments

Comments
 (0)