diff --git a/.github/workflows/jira.yml b/.github/workflows/jira.yml index 250abc7..caa4bbd 100644 --- a/.github/workflows/jira.yml +++ b/.github/workflows/jira.yml @@ -21,7 +21,7 @@ jobs: project: ${{ secrets.JIRA_PROJECT }} issuetype: ${{ secrets.JIRA_ISSUE_TYPE }} summary: | - Snyk | Vulnerability | ${{ github.event.repository.name }} | ${{ github.event.pull_request.title }} + ${{ github.event.pull_request.title }} description: | PR: ${{ github.event.pull_request.html_url }} diff --git a/.github/workflows/sca-scan.yml b/.github/workflows/sca-scan.yml index 4fa4560..decebb1 100644 --- a/.github/workflows/sca-scan.yml +++ b/.github/workflows/sca-scan.yml @@ -6,10 +6,18 @@ jobs: security-sca: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - name: Checkout repository + uses: actions/checkout@master + - name: Setup .NET Core @ Latest + uses: actions/setup-dotnet@v1 + with: + dotnet-version: "7.0.x" + - name: Run Dotnet Restore + run: | + dotnet restore - name: Run Snyk to check for vulnerabilities uses: snyk/actions/dotnet@master env: SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} with: - args: --fail-on=all + args: --file=Contentstack.Core/obj/project.assets.json --fail-on=all diff --git a/.talismanrc b/.talismanrc index 5104629..2fd7ede 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1,6 +1,6 @@ fileignoreconfig: - filename: Contentstack.Core/Internals/HttpRequestHandler.cs - checksum: 29bb8548d65cdc5800fc939e526797bb3515f528d8082a58a0c9c6215dec1651 + checksum: 93c1659f3bc7527956f0fd12db46441297fac3a4366d273bcbb3425d2351300e - filename: Contentstack.Core/Models/Entry.cs checksum: 79320b005882981fd7c79fe73832f28284db686927942e46b422ac9e88405023 - filename: Contentstack.Core/ContentstackClient.cs diff --git a/CODEOWNERS b/CODEOWNERS index 1be7e0d..0773923 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1 +1 @@ -* @contentstack/security-admin +* @contentstack/security-admin \ No newline at end of file diff --git a/Contentstack.Core/Configuration/LivePreviewConfig.cs b/Contentstack.Core/Configuration/LivePreviewConfig.cs index 0fbecdf..240db8e 100644 --- a/Contentstack.Core/Configuration/LivePreviewConfig.cs +++ b/Contentstack.Core/Configuration/LivePreviewConfig.cs @@ -1,4 +1,6 @@ -using System.Text.Json; +using System; +using System.Collections; +using Newtonsoft.Json.Linq; namespace Contentstack.Core.Configuration { @@ -11,6 +13,6 @@ public class LivePreviewConfig internal string LivePreview { get; set; } internal string ContentTypeUID { get; set; } internal string EntryUID { get; set; } - internal JsonElement PreviewResponse { get; set; } + internal JObject PreviewResponse { get; set; } } } diff --git a/Contentstack.Core/Contentstack.Core.csproj b/Contentstack.Core/Contentstack.Core.csproj index 4d4bb79..030a3a7 100644 --- a/Contentstack.Core/Contentstack.Core.csproj +++ b/Contentstack.Core/Contentstack.Core.csproj @@ -1,7 +1,7 @@  - net7.0 + netstandard2.0;net47;net472; contentstack.csharp Contentstack .NET SDK for the Contentstack Content Delivery API. @@ -28,6 +28,7 @@ + diff --git a/Contentstack.Core/ContentstackClient.cs b/Contentstack.Core/ContentstackClient.cs index e76fe32..b86d2f4 100644 --- a/Contentstack.Core/ContentstackClient.cs +++ b/Contentstack.Core/ContentstackClient.cs @@ -1,17 +1,18 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.IO; +using Contentstack.Core.Internals; +using Contentstack.Core.Configuration; +using Microsoft.Extensions.Options; +using Contentstack.Core.Models; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; using System.Linq; -using System.Net; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; -using Contentstack.Core.Configuration; +using System.Net; +using System.IO; +using System.Collections; +using Contentstack.Utils; using Contentstack.Core.Interfaces; -using Contentstack.Core.Internals; -using Contentstack.Core.Models; -using Microsoft.Extensions.Options; namespace Contentstack.Core { @@ -23,16 +24,7 @@ public class ContentstackClient /// /// Gets or sets the settings that should be used for deserialization. /// - public JsonSerializerOptions SerializerSettings { get; set; } = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - Converters = { - new JsonStringEnumConverter(), - new CustomUtcDateTimeConverter(), - new CustomNullableUtcDateTimeConverter(), - } - }; + public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings(); #region Internal Variables @@ -43,6 +35,7 @@ internal string StackApiKey } private ContentstackOptions _options; + internal JsonSerializer Serializer => JsonSerializer.Create(SerializerSettings); internal string _SyncUrl { get @@ -140,6 +133,10 @@ public ContentstackClient(IOptions options) throw new InvalidOperationException("Add PreviewToken or ManagementToken in LivePreviewConfig"); } } + this.SerializerSettings.DateParseHandling = DateParseHandling.None; + this.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat; + this.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; + this.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; foreach (Type t in CSJsonConverterAttribute.GetCustomAttribute(typeof(CSJsonConverterAttribute))) { @@ -212,18 +209,22 @@ internal static ContentstackException GetContentstackError(Exception ex) using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); - var data = JsonSerializer.Deserialize(errorMessage); + JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); - if (data.TryGetProperty("error_code", out var token)) - errorCode = token.GetInt32(); + JToken token = data["error_code"]; + if (token != null) + errorCode = token.Value(); - if (data.TryGetProperty("error_message", out token)) - errorMessage = token.GetString(); + token = data["error_message"]; + if (token != null) + errorMessage = token.Value(); - if (data.TryGetProperty("errors", out token)) - errors = JsonSerializer.Deserialize>(token.GetRawText()); + token = data["errors"]; + if (token != null) + errors = token.ToObject>(); - if (exResp is HttpWebResponse response) + var response = exResp as HttpWebResponse; + if (response != null) statusCode = response.StatusCode; } } @@ -325,8 +326,8 @@ public async Task GetContentTypes(Dictionary param = null { HttpRequestHandler RequestHandler = new HttpRequestHandler(this); var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout, proxy: this.Config.Proxy); - var data = JsonSerializer.Deserialize(outputResult); - var contentTypes = data.GetProperty("content_types").EnumerateArray().ToList(); + JObject data = JsonConvert.DeserializeObject(outputResult.Replace("\r\n", ""), this.SerializerSettings); + IList contentTypes = (IList)data["content_types"]; return contentTypes; } catch (Exception ex) @@ -335,7 +336,7 @@ public async Task GetContentTypes(Dictionary param = null } } - private async Task GetLivePreviewData() + private async Task GetLivePreviewData() { Dictionary headerAll = new Dictionary(); @@ -367,8 +368,8 @@ private async Task GetLivePreviewData() { HttpRequestHandler RequestHandler = new HttpRequestHandler(this); var outputResult = await RequestHandler.ProcessRequest(String.Format("{0}/content_types/{1}/entries/{2}", this.Config.getLivePreviewUrl(this.LivePreviewConfig), this.LivePreviewConfig.ContentTypeUID, this.LivePreviewConfig.EntryUID), headerAll, mainJson, Branch: this.Config.Branch, isLivePreview: true, timeout: this.Config.Timeout, proxy: this.Config.Proxy); - var data = JsonSerializer.Deserialize(outputResult); - return data.GetProperty("entry"); + JObject data = JsonConvert.DeserializeObject(outputResult.Replace("\r\n", ""), this.SerializerSettings); + return (JObject)data["entry"]; } catch (Exception ex) { @@ -785,7 +786,7 @@ private async Task GetResultAsync(string Init = "false", SyncType Syn { HttpRequestHandler requestHandler = new HttpRequestHandler(this); string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout, proxy: this.Config.Proxy); - SyncStack stackSyncOutput = JsonSerializer.Deserialize(js); + SyncStack stackSyncOutput = JsonConvert.DeserializeObject(js); return stackSyncOutput; } catch (Exception ex) diff --git a/Contentstack.Core/Internals/AssetJsonConverter.cs b/Contentstack.Core/Internals/AssetJsonConverter.cs index 96e581b..9fc7215 100644 --- a/Contentstack.Core/Internals/AssetJsonConverter.cs +++ b/Contentstack.Core/Internals/AssetJsonConverter.cs @@ -1,27 +1,27 @@ using System; -using System.Text.Json; -using System.Text.Json.Serialization; +using Contentstack.Core; using Contentstack.Core.Models; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Contentstack.Core.Internals { [CSJsonConverter("AssetJsonConverter")] public class AssetJsonConverter : JsonConverter { - public override Asset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override Asset ReadJson(JsonReader reader, Type objectType, Asset existingValue, bool hasExistingValue, JsonSerializer serializer) { - using (JsonDocument document = JsonDocument.ParseValue(ref reader)) - { - JsonElement root = document.RootElement; - Asset asset = JsonSerializer.Deserialize(root.GetRawText(), options); - asset.ParseObject(root); - return asset; - } + JObject jObject = JObject.Load(reader); + JsonSerializerSettings SerializerSettings = new JsonSerializerSettings(); + JsonSerializer Serializer = JsonSerializer.Create(SerializerSettings); + Asset asset = jObject.ToObject(Serializer); + asset.ParseObject(jObject); + return asset; } - public override void Write(Utf8JsonWriter writer, Asset value, JsonSerializerOptions options) + public override void WriteJson(JsonWriter writer, Asset value, JsonSerializer serializer) { throw new NotImplementedException(); } } -} \ No newline at end of file +} diff --git a/Contentstack.Core/Internals/ContentStackError.cs b/Contentstack.Core/Internals/ContentStackError.cs index 52f9b56..cb37a80 100644 --- a/Contentstack.Core/Internals/ContentStackError.cs +++ b/Contentstack.Core/Internals/ContentStackError.cs @@ -1,8 +1,8 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Net; -using System.Text.Json.Serialization; - + namespace Contentstack.Core.Internals { /// @@ -33,7 +33,7 @@ public class ContentstackException : Exception /// /// This is error message. /// - [JsonPropertyName("error_message")] + [JsonProperty("error_message")] public string ErrorMessage { get @@ -50,13 +50,13 @@ public string ErrorMessage /// /// This is error code. /// - [JsonPropertyName("error_code")] + [JsonProperty("error_code")] public int ErrorCode { get; set; } /// /// Set of errors in detail. /// - [JsonPropertyName("errors")] + [JsonProperty("errors")] public Dictionary Errors { get; set; } #endregion diff --git a/Contentstack.Core/Internals/ContentstackConvert.cs b/Contentstack.Core/Internals/ContentstackConvert.cs index a35a235..b2d4d74 100644 --- a/Contentstack.Core/Internals/ContentstackConvert.cs +++ b/Contentstack.Core/Internals/ContentstackConvert.cs @@ -1,5 +1,9 @@ -using System; +using Newtonsoft.Json; +using System; using System.IO; +using System.Net.NetworkInformation; +using System.Security.Cryptography; +using System.Text; using System.Text.RegularExpressions; namespace Contentstack.Core.Internals diff --git a/Contentstack.Core/Internals/HttpRequestHandler.cs b/Contentstack.Core/Internals/HttpRequestHandler.cs index e8710f0..6252f51 100644 --- a/Contentstack.Core/Internals/HttpRequestHandler.cs +++ b/Contentstack.Core/Internals/HttpRequestHandler.cs @@ -4,9 +4,9 @@ using System.Linq; using System.Net; using System.Text; -using System.Text.Json; -using System.Text.Json.Nodes; using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Contentstack.Core.Internals { @@ -35,7 +35,7 @@ public async Task ProcessRequest(string Url, Dictionary return value; } else if (kvp.Value is Dictionary) - value = JsonSerializer.Serialize(kvp.Value); + value = JsonConvert.SerializeObject(kvp.Value); else return String.Format("{0}={1}", kvp.Key, kvp.Value); @@ -75,7 +75,7 @@ public async Task ProcessRequest(string Url, Dictionary request = await plugin.OnRequest(client, request); }; - var serializedresult = JsonSerializer.Serialize(BodyJson); + var serializedresult = JsonConvert.SerializeObject(BodyJson); byte[] requestBody = Encoding.UTF8.GetBytes(serializedresult); StreamReader reader = null; HttpWebResponse response = null; @@ -108,42 +108,42 @@ public async Task ProcessRequest(string Url, Dictionary } - internal void updateLivePreviewContent(JsonObject response) + internal void updateLivePreviewContent(JObject response) { if (response.ContainsKey("uid") && response["uid"].ToString() == this.client.LivePreviewConfig.EntryUID) { - foreach (var property in this.client.LivePreviewConfig.PreviewResponse.AsObject()) + response.Merge(this.client.LivePreviewConfig.PreviewResponse, new JsonMergeSettings() { - response[property.Key] = property.Value; - } + MergeArrayHandling = MergeArrayHandling.Replace + }); } else { foreach (var content in response) { - if (content.Value is JsonArray) + if (content.Value.Type == JTokenType.Array) { - updateArray((JsonArray)response[content.Key]); + updateArray((JArray)response[content.Key]); } - else if (content.Value is JsonObject) + else if (content.Value.Type == JTokenType.Object) { - updateLivePreviewContent((JsonObject)response[content.Key]); + updateLivePreviewContent((JObject)response[content.Key]); } } } } - internal void updateArray(JsonArray array) + internal void updateArray(JArray array) { for (var i = 0; i < array.Count(); i++) { - if (array[i] is JsonArray) + if (array[i].Type == JTokenType.Array) { - updateArray((JsonArray)array[i]); + updateArray((JArray)array[i]); } - else if (array[i] is JsonObject) + else if (array[i].Type == JTokenType.Object) { - updateLivePreviewContent((JsonObject)array[i]); + updateLivePreviewContent((JObject)array[i]); } } } diff --git a/Contentstack.Core/Internals/JsonSerializerOptionsConverters.cs b/Contentstack.Core/Internals/JsonSerializerOptionsConverters.cs deleted file mode 100644 index 834e318..0000000 --- a/Contentstack.Core/Internals/JsonSerializerOptionsConverters.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Globalization; -using System.Text.Json; -using System.Text.Json.Serialization; - -public class CustomUtcDateTimeConverter : JsonConverter -{ - public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.String) - { - // Parse the string, treating it as UTC if no timezone is specified - if (DateTime.TryParse(reader.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateTime dateTime)) - { - return dateTime.Kind == DateTimeKind.Unspecified - ? DateTime.SpecifyKind(dateTime, DateTimeKind.Utc) - : dateTime; - } - } - - return reader.GetDateTime(); - } - - public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) - { - // Ensure the datetime is in UTC when writing - writer.WriteStringValue(value.Kind == DateTimeKind.Local - ? value.ToUniversalTime() - : value); - } -} - -public class CustomNullableUtcDateTimeConverter : JsonConverter -{ - public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.Null) - return null; - - if (reader.TokenType == JsonTokenType.String) - { - // Parse the string, treating it as UTC if no timezone is specified - if (DateTime.TryParse(reader.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateTime dateTime)) - { - return dateTime.Kind == DateTimeKind.Unspecified - ? DateTime.SpecifyKind(dateTime, DateTimeKind.Utc) - : dateTime; - } - } - - return reader.GetDateTime(); - } - - public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options) - { - if (value.HasValue) - { - // Ensure the datetime is in UTC when writing - writer.WriteStringValue(value.Value.Kind == DateTimeKind.Local - ? value.Value.ToUniversalTime() - : value.Value); - } - else - { - writer.WriteNullValue(); - } - } -} \ No newline at end of file diff --git a/Contentstack.Core/Internals/StackOutput.cs b/Contentstack.Core/Internals/StackOutput.cs index 0b20489..a934059 100644 --- a/Contentstack.Core/Internals/StackOutput.cs +++ b/Contentstack.Core/Internals/StackOutput.cs @@ -1,5 +1,8 @@ -using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; +using System.Linq; namespace Contentstack.Core.Internals diff --git a/Contentstack.Core/Models/Asset.cs b/Contentstack.Core/Models/Asset.cs index 47355f8..05756a2 100644 --- a/Contentstack.Core/Models/Asset.cs +++ b/Contentstack.Core/Models/Asset.cs @@ -3,13 +3,12 @@ using System.IO; using System.Linq; using System.Net; -using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; using System.Threading.Tasks; using Contentstack.Core.Configuration; using Contentstack.Core.Internals; using Contentstack.Utils.Interfaces; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Contentstack.Core.Models { @@ -99,19 +98,19 @@ public string Url /// /// This is Asset type uid. /// - [JsonPropertyName("_content_type_uid")] + [JsonProperty(propertyName: "_content_type_uid")] public string ContentTypeUid { get; set; } /// /// The size of the file in bytes. /// - [JsonPropertyName("file_size")] + [JsonProperty(PropertyName = "file_size")] public string FileSize { get; set; } /// /// The original name of the file. /// - [JsonPropertyName("filename")] + [JsonProperty(PropertyName = "filename")] public string FileName { get; set; } /// @@ -127,50 +126,50 @@ public string Url /// /// This content_type in asset. /// - [JsonPropertyName("content_type")] + [JsonProperty(propertyName: "content_type")] public string ContentType { get; set; } /// /// This for whether it is asset directory /// - [JsonPropertyName("is_dir")] + [JsonProperty(propertyName: "is_dir")] public Boolean IsDir { get; set; } /// /// Uid of user who updated the file /// - [JsonPropertyName("updated_by")] - public string UpdatedBy { get; set; } - + [JsonProperty(PropertyName = "updated_by")] + public string UpdatedBy { get; set; } + /// /// Uid of user who updated the file /// - [JsonPropertyName("created_by")] + [JsonProperty(PropertyName = "created_by")] public string CreatedBy { get; set; } /// /// The Uid of folder in which the asset is present /// - [JsonPropertyName("parent_uid")] + [JsonProperty(PropertyName = "parent_uid")] public string ParentUid { get; set; } /// /// The Version of Asset /// - [JsonPropertyName("_version")] + [JsonProperty(PropertyName = "_version")] public string Version { get; set; } /// /// Dimension Object of the asset containing Height and width /// - [JsonPropertyName("dimension")] + [JsonProperty(PropertyName = "dimension")] public Dictionary Dimension { get; set; } /// /// Dimension Object of the asset containing Height and width /// - [JsonPropertyName("publish_details")] + [JsonProperty(PropertyName = "publish_details")] public Dictionary PublishDetails { get; set; } #region Internal Constructors @@ -307,9 +306,9 @@ public void RemoveHeader(string key) } - internal void ParseObject(JsonElement jsonObj) + internal void ParseObject(JObject jsonObj) { - this._ObjectAttributes = JsonSerializer.Deserialize>(jsonObj); + this._ObjectAttributes = jsonObj.ToObject>(); } public DateTime GetCreateAt() @@ -420,9 +419,8 @@ public async Task Fetch() { HttpRequestHandler RequestHandler = new HttpRequestHandler(this.StackInstance); var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.StackInstance.Config.Branch, timeout: this.StackInstance.Config.Timeout, proxy: this.StackInstance.Config.Proxy); - JsonObject obj = JsonNode.Parse(ContentstackConvert.ToString(outputResult, "{}")).AsObject(); - // TODO: the serializer earlier taken was this.StackInstance.Serializer - return JsonSerializer.Deserialize(obj["$.asset"].ToJsonString(), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + JObject obj = JObject.Parse(ContentstackConvert.ToString(outputResult, "{}")); + return obj.SelectToken("$.asset").ToObject(this.StackInstance.Serializer); } catch (Exception ex) { @@ -488,16 +486,21 @@ internal static ContentstackException GetContentstackError(Exception ex) using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); - JsonObject data = JsonNode.Parse(errorMessage.Replace("\r\n", "")).AsObject(); + JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); + //errorCode = ContentstackConvert.ToInt32(data.Property("error_code").Value); + //errorMessage = ContentstackConvert.ToString(data.Property("error_message").Value); - if (data.TryGetPropertyValue("error_code", out JsonNode token)) - errorCode = token.GetValue(); + JToken token = data["error_code"]; + if (token != null) + errorCode = token.Value(); - if (data.TryGetPropertyValue("error_message", out token)) - errorMessage = token.GetValue(); + token = data["error_message"]; + if (token != null) + errorMessage = token.Value(); - if (data.TryGetPropertyValue("errors", out token)) - errors = JsonSerializer.Deserialize>(token.ToJsonString()); + token = data["errors"]; + if (token != null) + errors = token.ToObject>(); var response = exResp as HttpWebResponse; if (response != null) diff --git a/Contentstack.Core/Models/AssetLibrary.cs b/Contentstack.Core/Models/AssetLibrary.cs index 548a7a4..2c7e88d 100644 --- a/Contentstack.Core/Models/AssetLibrary.cs +++ b/Contentstack.Core/Models/AssetLibrary.cs @@ -3,11 +3,10 @@ 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.Configuration; using Contentstack.Core.Internals; +using Newtonsoft.Json.Linq; namespace Contentstack.Core.Models { @@ -87,13 +86,13 @@ public void SortWithKeyAndOrderBy(String key, OrderBy order) /// JObject jObject = await assetLibrary.Count(); /// /// - public async Task Count() + public async Task Count() { UrlQueries.Add("count", "true"); return await Exec(); } - public AssetLibrary Query(JsonObject QueryObject) + public AssetLibrary Query(JObject QueryObject) { try { @@ -436,10 +435,9 @@ public AssetLibrary RemoveHeader(string key) /// public async Task> FetchAll() { - JsonObject json = await Exec(); - // TODO: the serializer earlier taken was this.StackInstance.Serializer - var assets = JsonSerializer.Deserialize>(json["assets"].ToJsonString(), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); - var collection = JsonSerializer.Deserialize>(json.ToJsonString(), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + JObject json = await Exec(); + var assets = json.SelectToken("$.assets").ToObject>(this.Stack.Serializer); + var collection = json.ToObject>(this.Stack.Serializer); foreach (var entry in assets) { if (entry.GetType() == typeof(Asset)) @@ -451,7 +449,7 @@ public async Task> FetchAll() return collection; } - private async Task Exec() + private async Task Exec() { Dictionary headers = GetHeader(_Headers); @@ -476,7 +474,7 @@ private async Task Exec() { HttpRequestHandler RequestHandler = new HttpRequestHandler(this.Stack); var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Stack.Config.Branch, timeout: this.Stack.Config.Timeout, proxy: this.Stack.Config.Proxy); - return JsonNode.Parse(ContentstackConvert.ToString(outputResult, "{}")).AsObject(); + return JObject.Parse(ContentstackConvert.ToString(outputResult, "{}")); } catch (Exception ex) @@ -543,22 +541,23 @@ internal static ContentstackException GetContentstackError(Exception ex) using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); - JsonObject data = JsonNode.Parse(errorMessage.Replace("\r\n", "")).AsObject(); + JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); - if (data.TryGetPropertyValue("error_code", out JsonNode token)) - errorCode = token.GetValue(); + JToken token = data["error_code"]; + if (token != null) + errorCode = token.Value(); - if (data.TryGetPropertyValue("error_message", out token)) - errorMessage = token.GetValue(); + token = data["error_message"]; + if (token != null) + errorMessage = token.Value(); - if (data.TryGetPropertyValue("errors", out token)) - errors = JsonSerializer.Deserialize>(token.ToJsonString()); + token = data["errors"]; + if (token != null) + errors = token.ToObject>(); var response = exResp as HttpWebResponse; if (response != null) - { statusCode = response.StatusCode; - } } } catch diff --git a/Contentstack.Core/Models/ContentType.cs b/Contentstack.Core/Models/ContentType.cs index 5642950..837d15a 100644 --- a/Contentstack.Core/Models/ContentType.cs +++ b/Contentstack.Core/Models/ContentType.cs @@ -1,13 +1,13 @@ 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.Configuration; using Contentstack.Core.Internals; +using System.Threading.Tasks; +using System.Net; +using Newtonsoft.Json.Linq; +using System.Linq; +using System.IO; +using Newtonsoft.Json; namespace Contentstack.Core.Models { @@ -82,22 +82,23 @@ internal static ContentstackException GetContentstackError(Exception ex) using (var reader = new StreamReader(stream)) { errorMessage = reader.ReadToEnd(); - JsonObject data = JsonNode.Parse(errorMessage.Replace("\r\n", "")).AsObject(); + JObject data = JObject.Parse(errorMessage.Replace("\r\n", "")); - if (data.TryGetPropertyValue("error_code", out JsonNode token)) - errorCode = token.GetValue(); + JToken token = data["error_code"]; + if (token != null) + errorCode = token.Value(); - if (data.TryGetPropertyValue("error_message", out token)) - errorMessage = token.GetValue(); + token = data["error_message"]; + if (token != null) + errorMessage = token.Value(); - if (data.TryGetPropertyValue("errors", out token)) - errors = JsonSerializer.Deserialize>(token.ToJsonString()); + token = data["errors"]; + if (token != null) + errors = token.ToObject>(); var response = exResp as HttpWebResponse; if (response != null) - { statusCode = response.StatusCode; - } } } catch @@ -138,7 +139,7 @@ internal void SetStackInstance(ContentstackClient stack) /// /// is dictionary of additional parameter /// The Content-Type Schema Object. - public async Task Fetch(Dictionary param = null) + public async Task Fetch(Dictionary param = null) { Dictionary headers = GetHeader(_Headers); Dictionary headerAll = new Dictionary(); @@ -169,8 +170,8 @@ public async Task Fetch(Dictionary param = null) { HttpRequestHandler RequestHandler = new HttpRequestHandler(this.StackInstance); var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.StackInstance.Config.Branch, timeout: this.StackInstance.Config.Timeout, proxy: this.StackInstance.Config.Proxy); - JsonObject data = JsonNode.Parse(outputResult.Replace("\r\n", "")).AsObject(); - JsonObject contentTypes = data["content_type"].AsObject(); + JObject data = JsonConvert.DeserializeObject(outputResult.Replace("\r\n", ""), this.StackInstance.SerializerSettings); + JObject contentTypes = (Newtonsoft.Json.Linq.JObject)data["content_type"]; return contentTypes; } catch (Exception ex) diff --git a/Contentstack.Core/Models/ContentstackCollection.cs b/Contentstack.Core/Models/ContentstackCollection.cs old mode 100644 new mode 100755 index 458f5d4..0edd7c6 --- a/Contentstack.Core/Models/ContentstackCollection.cs +++ b/Contentstack.Core/Models/ContentstackCollection.cs @@ -1,10 +1,14 @@ -using System.Collections; +using Newtonsoft.Json; +using System; +using System.Collections; using System.Collections.Generic; +using System.Text; namespace Contentstack.Core.Models { - public class ContentstackCollection : IEnumerable - { + [JsonObject] + public class ContentstackCollection : IEnumerable + { /// /// The number of items skipped in this resultset. ///