Skip to content

Commit 1b66857

Browse files
authored
Merge pull request #96 from contentstack/revert-83-refactor/DX-1473
Revert "refactor: Updating to system.text.json"
2 parents 188a919 + a9dfb24 commit 1b66857

17 files changed

+172
-214
lines changed

.github/workflows/jira.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
project: ${{ secrets.JIRA_PROJECT }}
2222
issuetype: ${{ secrets.JIRA_ISSUE_TYPE }}
2323
summary: |
24-
Snyk | Vulnerability | ${{ github.event.repository.name }} | ${{ github.event.pull_request.title }}
24+
${{ github.event.pull_request.title }}
2525
description: |
2626
PR: ${{ github.event.pull_request.html_url }}
2727

.github/workflows/sca-scan.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ jobs:
66
security-sca:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@master
9+
- name: Checkout repository
10+
uses: actions/checkout@master
11+
- name: Setup .NET Core @ Latest
12+
uses: actions/setup-dotnet@v1
13+
with:
14+
dotnet-version: "7.0.x"
15+
- name: Run Dotnet Restore
16+
run: |
17+
dotnet restore
1018
- name: Run Snyk to check for vulnerabilities
1119
uses: snyk/actions/dotnet@master
1220
env:
1321
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
1422
with:
15-
args: --fail-on=all
23+
args: --file=Contentstack.Core/obj/project.assets.json --fail-on=all

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fileignoreconfig:
22
- filename: Contentstack.Core/Internals/HttpRequestHandler.cs
3-
checksum: 29bb8548d65cdc5800fc939e526797bb3515f528d8082a58a0c9c6215dec1651
3+
checksum: 93c1659f3bc7527956f0fd12db46441297fac3a4366d273bcbb3425d2351300e
44
- filename: Contentstack.Core/Models/Entry.cs
55
checksum: 79320b005882981fd7c79fe73832f28284db686927942e46b422ac9e88405023
66
- filename: Contentstack.Core/ContentstackClient.cs

CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @contentstack/security-admin
1+
* @contentstack/security-admin

Contentstack.Core/Configuration/LivePreviewConfig.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Text.Json;
1+
using System;
2+
using System.Collections;
3+
using Newtonsoft.Json.Linq;
24

35
namespace Contentstack.Core.Configuration
46
{
@@ -11,6 +13,6 @@ public class LivePreviewConfig
1113
internal string LivePreview { get; set; }
1214
internal string ContentTypeUID { get; set; }
1315
internal string EntryUID { get; set; }
14-
internal JsonElement PreviewResponse { get; set; }
16+
internal JObject PreviewResponse { get; set; }
1517
}
1618
}

Contentstack.Core/Contentstack.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net7.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;net47;net472;</TargetFrameworks>
55
<PackageId>contentstack.csharp</PackageId>
66
<Authors>Contentstack</Authors>
77
<Description>.NET SDK for the Contentstack Content Delivery API.</Description>
@@ -28,6 +28,7 @@
2828
<AllowUnsafeBlocks></AllowUnsafeBlocks>
2929
</PropertyGroup>
3030
<ItemGroup>
31+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3132
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
3233
<PackageReference Include="Markdig" Version="0.36.2" />
3334
<PackageReference Include="contentstack.utils" Version="1.0.5" />

Contentstack.Core/ContentstackClient.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
4-
using System.IO;
3+
using Contentstack.Core.Internals;
4+
using Contentstack.Core.Configuration;
5+
using Microsoft.Extensions.Options;
6+
using Contentstack.Core.Models;
7+
using Newtonsoft.Json.Linq;
8+
using Newtonsoft.Json;
59
using System.Linq;
6-
using System.Net;
7-
using System.Text.Json;
8-
using System.Text.Json.Serialization;
910
using System.Threading.Tasks;
10-
using Contentstack.Core.Configuration;
11+
using System.Net;
12+
using System.IO;
13+
using System.Collections;
14+
using Contentstack.Utils;
1115
using Contentstack.Core.Interfaces;
12-
using Contentstack.Core.Internals;
13-
using Contentstack.Core.Models;
14-
using Microsoft.Extensions.Options;
1516

1617
namespace Contentstack.Core
1718
{
@@ -23,16 +24,7 @@ public class ContentstackClient
2324
/// <summary>
2425
/// Gets or sets the settings that should be used for deserialization.
2526
/// </summary>
26-
public JsonSerializerOptions SerializerSettings { get; set; } = new JsonSerializerOptions
27-
{
28-
PropertyNameCaseInsensitive = true,
29-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
30-
Converters = {
31-
new JsonStringEnumConverter(),
32-
new CustomUtcDateTimeConverter(),
33-
new CustomNullableUtcDateTimeConverter(),
34-
}
35-
};
27+
public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings();
3628

3729
#region Internal Variables
3830

@@ -43,6 +35,7 @@ internal string StackApiKey
4335
}
4436
private ContentstackOptions _options;
4537

38+
internal JsonSerializer Serializer => JsonSerializer.Create(SerializerSettings);
4639
internal string _SyncUrl
4740
{
4841
get
@@ -140,6 +133,10 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
140133
throw new InvalidOperationException("Add PreviewToken or ManagementToken in LivePreviewConfig");
141134
}
142135
}
136+
this.SerializerSettings.DateParseHandling = DateParseHandling.None;
137+
this.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
138+
this.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
139+
this.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
143140

144141
foreach (Type t in CSJsonConverterAttribute.GetCustomAttribute(typeof(CSJsonConverterAttribute)))
145142
{
@@ -212,18 +209,22 @@ internal static ContentstackException GetContentstackError(Exception ex)
212209
using (var reader = new StreamReader(stream))
213210
{
214211
errorMessage = reader.ReadToEnd();
215-
var data = JsonSerializer.Deserialize<JsonElement>(errorMessage);
212+
JObject data = JObject.Parse(errorMessage.Replace("\r\n", ""));
216213

217-
if (data.TryGetProperty("error_code", out var token))
218-
errorCode = token.GetInt32();
214+
JToken token = data["error_code"];
215+
if (token != null)
216+
errorCode = token.Value<int>();
219217

220-
if (data.TryGetProperty("error_message", out token))
221-
errorMessage = token.GetString();
218+
token = data["error_message"];
219+
if (token != null)
220+
errorMessage = token.Value<string>();
222221

223-
if (data.TryGetProperty("errors", out token))
224-
errors = JsonSerializer.Deserialize<Dictionary<string, object>>(token.GetRawText());
222+
token = data["errors"];
223+
if (token != null)
224+
errors = token.ToObject<Dictionary<string, object>>();
225225

226-
if (exResp is HttpWebResponse response)
226+
var response = exResp as HttpWebResponse;
227+
if (response != null)
227228
statusCode = response.StatusCode;
228229
}
229230
}
@@ -325,8 +326,8 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
325326
{
326327
HttpRequestHandler RequestHandler = new HttpRequestHandler(this);
327328
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout, proxy: this.Config.Proxy);
328-
var data = JsonSerializer.Deserialize<JsonElement>(outputResult);
329-
var contentTypes = data.GetProperty("content_types").EnumerateArray().ToList();
329+
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
330+
IList contentTypes = (IList)data["content_types"];
330331
return contentTypes;
331332
}
332333
catch (Exception ex)
@@ -335,7 +336,7 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
335336
}
336337
}
337338

338-
private async Task<JsonElement> GetLivePreviewData()
339+
private async Task<JObject> GetLivePreviewData()
339340
{
340341

341342
Dictionary<String, object> headerAll = new Dictionary<string, object>();
@@ -367,8 +368,8 @@ private async Task<JsonElement> GetLivePreviewData()
367368
{
368369
HttpRequestHandler RequestHandler = new HttpRequestHandler(this);
369370
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);
370-
var data = JsonSerializer.Deserialize<JsonElement>(outputResult);
371-
return data.GetProperty("entry");
371+
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
372+
return (JObject)data["entry"];
372373
}
373374
catch (Exception ex)
374375
{
@@ -785,7 +786,7 @@ private async Task<SyncStack> GetResultAsync(string Init = "false", SyncType Syn
785786
{
786787
HttpRequestHandler requestHandler = new HttpRequestHandler(this);
787788
string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout, proxy: this.Config.Proxy);
788-
SyncStack stackSyncOutput = JsonSerializer.Deserialize<SyncStack>(js);
789+
SyncStack stackSyncOutput = JsonConvert.DeserializeObject<SyncStack>(js);
789790
return stackSyncOutput;
790791
}
791792
catch (Exception ex)
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
using System;
2-
using System.Text.Json;
3-
using System.Text.Json.Serialization;
2+
using Contentstack.Core;
43
using Contentstack.Core.Models;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
56

67
namespace Contentstack.Core.Internals
78
{
89
[CSJsonConverter("AssetJsonConverter")]
910
public class AssetJsonConverter : JsonConverter<Asset>
1011
{
11-
public override Asset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
12+
public override Asset ReadJson(JsonReader reader, Type objectType, Asset existingValue, bool hasExistingValue, JsonSerializer serializer)
1213
{
13-
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
14-
{
15-
JsonElement root = document.RootElement;
16-
Asset asset = JsonSerializer.Deserialize<Asset>(root.GetRawText(), options);
17-
asset.ParseObject(root);
18-
return asset;
19-
}
14+
JObject jObject = JObject.Load(reader);
15+
JsonSerializerSettings SerializerSettings = new JsonSerializerSettings();
16+
JsonSerializer Serializer = JsonSerializer.Create(SerializerSettings);
17+
Asset asset = jObject.ToObject<Asset>(Serializer);
18+
asset.ParseObject(jObject);
19+
return asset;
2020
}
2121

22-
public override void Write(Utf8JsonWriter writer, Asset value, JsonSerializerOptions options)
22+
public override void WriteJson(JsonWriter writer, Asset value, JsonSerializer serializer)
2323
{
2424
throw new NotImplementedException();
2525
}
2626
}
27-
}
27+
}

Contentstack.Core/Internals/ContentStackError.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using System;
23
using System.Collections.Generic;
34
using System.Net;
4-
using System.Text.Json.Serialization;
5-
5+
66
namespace Contentstack.Core.Internals
77
{
88
/// <summary>
@@ -33,7 +33,7 @@ public class ContentstackException : Exception
3333
/// <summary>
3434
/// This is error message.
3535
/// </summary>
36-
[JsonPropertyName("error_message")]
36+
[JsonProperty("error_message")]
3737
public string ErrorMessage
3838
{
3939
get
@@ -50,13 +50,13 @@ public string ErrorMessage
5050
/// <summary>
5151
/// This is error code.
5252
/// </summary>
53-
[JsonPropertyName("error_code")]
53+
[JsonProperty("error_code")]
5454
public int ErrorCode { get; set; }
5555

5656
/// <summary>
5757
/// Set of errors in detail.
5858
/// </summary>
59-
[JsonPropertyName("errors")]
59+
[JsonProperty("errors")]
6060
public Dictionary<string, object> Errors { get; set; }
6161

6262
#endregion

Contentstack.Core/Internals/ContentstackConvert.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using System;
23
using System.IO;
4+
using System.Net.NetworkInformation;
5+
using System.Security.Cryptography;
6+
using System.Text;
37
using System.Text.RegularExpressions;
48

59
namespace Contentstack.Core.Internals

Contentstack.Core/Internals/HttpRequestHandler.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System.Linq;
55
using System.Net;
66
using System.Text;
7-
using System.Text.Json;
8-
using System.Text.Json.Nodes;
97
using System.Threading.Tasks;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
1010

1111
namespace Contentstack.Core.Internals
1212
{
@@ -35,7 +35,7 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
3535
return value;
3636
}
3737
else if (kvp.Value is Dictionary<string, object>)
38-
value = JsonSerializer.Serialize(kvp.Value);
38+
value = JsonConvert.SerializeObject(kvp.Value);
3939
else
4040
return String.Format("{0}={1}", kvp.Key, kvp.Value);
4141

@@ -75,7 +75,7 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
7575
request = await plugin.OnRequest(client, request);
7676
};
7777

78-
var serializedresult = JsonSerializer.Serialize(BodyJson);
78+
var serializedresult = JsonConvert.SerializeObject(BodyJson);
7979
byte[] requestBody = Encoding.UTF8.GetBytes(serializedresult);
8080
StreamReader reader = null;
8181
HttpWebResponse response = null;
@@ -108,42 +108,42 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
108108

109109
}
110110

111-
internal void updateLivePreviewContent(JsonObject response)
111+
internal void updateLivePreviewContent(JObject response)
112112
{
113113
if (response.ContainsKey("uid") && response["uid"].ToString() == this.client.LivePreviewConfig.EntryUID)
114114
{
115-
foreach (var property in this.client.LivePreviewConfig.PreviewResponse.AsObject())
115+
response.Merge(this.client.LivePreviewConfig.PreviewResponse, new JsonMergeSettings()
116116
{
117-
response[property.Key] = property.Value;
118-
}
117+
MergeArrayHandling = MergeArrayHandling.Replace
118+
});
119119
}
120120
else
121121
{
122122
foreach (var content in response)
123123
{
124-
if (content.Value is JsonArray)
124+
if (content.Value.Type == JTokenType.Array)
125125
{
126-
updateArray((JsonArray)response[content.Key]);
126+
updateArray((JArray)response[content.Key]);
127127
}
128-
else if (content.Value is JsonObject)
128+
else if (content.Value.Type == JTokenType.Object)
129129
{
130-
updateLivePreviewContent((JsonObject)response[content.Key]);
130+
updateLivePreviewContent((JObject)response[content.Key]);
131131
}
132132
}
133133
}
134134
}
135135

136-
internal void updateArray(JsonArray array)
136+
internal void updateArray(JArray array)
137137
{
138138
for (var i = 0; i < array.Count(); i++)
139139
{
140-
if (array[i] is JsonArray)
140+
if (array[i].Type == JTokenType.Array)
141141
{
142-
updateArray((JsonArray)array[i]);
142+
updateArray((JArray)array[i]);
143143
}
144-
else if (array[i] is JsonObject)
144+
else if (array[i].Type == JTokenType.Object)
145145
{
146-
updateLivePreviewContent((JsonObject)array[i]);
146+
updateLivePreviewContent((JObject)array[i]);
147147
}
148148
}
149149
}

0 commit comments

Comments
 (0)