|
1 | 1 | using System; |
2 | 2 | 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; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Threading.Tasks; |
7 | 8 | using System.Net; |
8 | 9 | using System.IO; |
9 | | -using System.Collections; |
10 | 10 | using Contentstack.Model.Generator.Model; |
11 | 11 | using contentstack.model.generator.Model; |
12 | 12 |
|
13 | 13 | namespace contentstack.CMA |
14 | 14 | { |
15 | 15 | public class ContentstackClient |
16 | 16 | { |
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 | + }; |
18 | 24 |
|
19 | 25 | #region Internal Variables |
20 | 26 |
|
@@ -131,19 +137,18 @@ internal static ContentstackException GetContentstackException(Exception ex) |
131 | 137 | using (var reader = new StreamReader(stream)) |
132 | 138 | { |
133 | 139 | 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>(); |
139 | 145 |
|
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>(); |
143 | 148 |
|
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 | + } |
147 | 152 |
|
148 | 153 | var response = exResp as HttpWebResponse; |
149 | 154 | if (response != null) |
@@ -206,11 +211,8 @@ public async Task<StackResponse> GetStack() |
206 | 211 | { |
207 | 212 | HttpRequestHandler RequestHandler = new HttpRequestHandler(); |
208 | 213 | 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); |
214 | 216 | } |
215 | 217 | catch (Exception ex) |
216 | 218 | { |
@@ -245,13 +247,11 @@ public async Task<ContentstackResponse> GetContentTypes(int skip = 0) |
245 | 247 | { |
246 | 248 | HttpRequestHandler RequestHandler = new HttpRequestHandler(); |
247 | 249 | 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(); |
250 | 251 | 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>(); |
255 | 255 | } |
256 | 256 | return contentstackResponse; |
257 | 257 | } |
@@ -286,13 +286,11 @@ public async Task<ContentstackResponse> GetGlobalFields(int skip = 0) |
286 | 286 | { |
287 | 287 | HttpRequestHandler RequestHandler = new HttpRequestHandler(); |
288 | 288 | 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(); |
291 | 290 | 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>(); |
296 | 294 | } |
297 | 295 | return contentstackResponse; |
298 | 296 | } |
|
0 commit comments