diff --git a/Contentstack.Core/Internals/EntryJsonConverter.cs b/Contentstack.Core/Internals/EntryJsonConverter.cs index 9c4ab9d..f40c1dd 100644 --- a/Contentstack.Core/Internals/EntryJsonConverter.cs +++ b/Contentstack.Core/Internals/EntryJsonConverter.cs @@ -1,25 +1,25 @@ using System; -using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; using Contentstack.Core.Models; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace Contentstack.Core.Internals { [CSJsonConverter("EntryJsonConverter")] public class EntryJsonConverter : JsonConverter { - public override Entry ReadJson(JsonReader reader, Type objectType, Entry existingValue, bool hasExistingValue, JsonSerializer serializer) + public override Entry Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - JObject jObject = JObject.Load(reader); - JsonSerializerSettings SerializerSettings = new JsonSerializerSettings(); - JsonSerializer Serializer = JsonSerializer.Create(SerializerSettings); - Entry entry = jObject.ToObject(Serializer); - entry.ParseObject(jObject); - return entry; + using (JsonDocument document = JsonDocument.ParseValue(ref reader)) + { + JsonElement root = document.RootElement; + Entry entry = JsonSerializer.Deserialize(root.GetRawText(), options); + entry.ParseObject(root); + return entry; + } } - public override void WriteJson(JsonWriter writer, Entry value, JsonSerializer serializer) + public override void Write(Utf8JsonWriter writer, Entry value, JsonSerializerOptions options) { } diff --git a/Contentstack.Core/Models/Entry.cs b/Contentstack.Core/Models/Entry.cs index c8a8788..b64d425 100644 --- a/Contentstack.Core/Models/Entry.cs +++ b/Contentstack.Core/Models/Entry.cs @@ -1,11 +1,11 @@ using Markdig; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Text.Json; +using System.Text.Json.Nodes; using System.Threading.Tasks; using Contentstack.Core.Internals; using Contentstack.Core.Configuration; @@ -24,7 +24,7 @@ public class Entry private CachePolicy _CachePolicy; private Dictionary UrlQueries = new Dictionary(); private bool _IsCachePolicySet; - private JObject jObject; + private JsonObject jsonObject; private string _Url { get @@ -169,7 +169,7 @@ internal Entry(string contentTypeName) #region Internal Functions internal static ContentstackException GetContentstackError(Exception ex) { - Int32 errorCode = 0; + int errorCode = 0; string errorMessage = string.Empty; HttpStatusCode statusCode = HttpStatusCode.InternalServerError; ContentstackException contentstackError = new ContentstackException(ex); @@ -177,30 +177,29 @@ internal static ContentstackException GetContentstackError(Exception ex) try { - System.Net.WebException webEx = (System.Net.WebException)ex; + WebException webEx = (WebException)ex; using (var exResp = webEx.Response) using (var stream = exResp.GetResponseStream()) using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); - JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); + JsonObject data = JsonNode.Parse(errorMessage.Replace("\r\n", ""))?.AsObject(); - JToken token = data["error_code"]; - if (token != null) - errorCode = token.Value(); + if (data != null) + { + if (data.TryGetPropertyValue("error_code", out var token)) + errorCode = token.GetValue(); - token = data["error_message"]; - if (token != null) - errorMessage = token.Value(); + if (data.TryGetPropertyValue("error_message", out token)) + errorMessage = token.GetValue(); - token = data["errors"]; - if (token != null) - errors = token.ToObject>(); + if (data.TryGetPropertyValue("errors", out token)) + errors = JsonSerializer.Deserialize>(token.ToJsonString()); - var response = exResp as HttpWebResponse; - if (response != null) - statusCode = response.StatusCode; + if (exResp is HttpWebResponse response) + statusCode = response.StatusCode; + } } } catch @@ -887,7 +886,7 @@ public JObject ToJson() private Asset GetAsset(String key) { - JObject assetObject = (JObject)jObject.GetValue(key); + JsonElement assetObject = (JObject)jObject.GetValue(key); var asset = ContentTypeInstance.StackInstance.Asset(); asset.ParseObject(assetObject); return asset;