1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
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 ;
4
+ using System . IO ;
9
5
using System . Linq ;
10
- using System . Threading . Tasks ;
11
6
using System . Net ;
12
- using System . IO ;
13
- using System . Collections ;
14
- using Contentstack . Utils ;
7
+ using System . Text . Json ;
8
+ using System . Text . Json . Serialization ;
9
+ using System . Threading . Tasks ;
10
+ using Contentstack . Core . Configuration ;
15
11
using Contentstack . Core . Interfaces ;
12
+ using Contentstack . Core . Internals ;
13
+ using Contentstack . Core . Models ;
14
+ using Microsoft . Extensions . Options ;
16
15
17
16
namespace Contentstack . Core
18
17
{
@@ -24,7 +23,16 @@ public class ContentstackClient
24
23
/// <summary>
25
24
/// Gets or sets the settings that should be used for deserialization.
26
25
/// </summary>
27
- public JsonSerializerSettings SerializerSettings { get ; set ; } = new JsonSerializerSettings ( ) ;
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
+ } ;
28
36
29
37
#region Internal Variables
30
38
@@ -35,7 +43,6 @@ internal string StackApiKey
35
43
}
36
44
private ContentstackOptions _options ;
37
45
38
- internal JsonSerializer Serializer => JsonSerializer . Create ( SerializerSettings ) ;
39
46
internal string _SyncUrl
40
47
{
41
48
get
@@ -133,10 +140,6 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
133
140
throw new InvalidOperationException ( "Add PreviewToken or ManagementToken in LivePreviewConfig" ) ;
134
141
}
135
142
}
136
- this . SerializerSettings . DateParseHandling = DateParseHandling . None ;
137
- this . SerializerSettings . DateFormatHandling = DateFormatHandling . IsoDateFormat ;
138
- this . SerializerSettings . DateTimeZoneHandling = DateTimeZoneHandling . Utc ;
139
- this . SerializerSettings . NullValueHandling = NullValueHandling . Ignore ;
140
143
141
144
foreach ( Type t in CSJsonConverterAttribute . GetCustomAttribute ( typeof ( CSJsonConverterAttribute ) ) )
142
145
{
@@ -209,22 +212,18 @@ internal static ContentstackException GetContentstackError(Exception ex)
209
212
using ( var reader = new StreamReader ( stream ) )
210
213
{
211
214
errorMessage = reader . ReadToEnd ( ) ;
212
- JObject data = JObject . Parse ( errorMessage . Replace ( " \r \n " , "" ) ) ;
215
+ var data = JsonSerializer . Deserialize < JsonElement > ( errorMessage ) ;
213
216
214
- JToken token = data [ "error_code" ] ;
215
- if ( token != null )
216
- errorCode = token . Value < int > ( ) ;
217
+ if ( data . TryGetProperty ( "error_code" , out var token ) )
218
+ errorCode = token . GetInt32 ( ) ;
217
219
218
- token = data [ "error_message" ] ;
219
- if ( token != null )
220
- errorMessage = token . Value < string > ( ) ;
220
+ if ( data . TryGetProperty ( "error_message" , out token ) )
221
+ errorMessage = token . GetString ( ) ;
221
222
222
- token = data [ "errors" ] ;
223
- if ( token != null )
224
- errors = token . ToObject < Dictionary < string , object > > ( ) ;
223
+ if ( data . TryGetProperty ( "errors" , out token ) )
224
+ errors = JsonSerializer . Deserialize < Dictionary < string , object > > ( token . GetRawText ( ) ) ;
225
225
226
- var response = exResp as HttpWebResponse ;
227
- if ( response != null )
226
+ if ( exResp is HttpWebResponse response )
228
227
statusCode = response . StatusCode ;
229
228
}
230
229
}
@@ -326,8 +325,8 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
326
325
{
327
326
HttpRequestHandler RequestHandler = new HttpRequestHandler ( this ) ;
328
327
var outputResult = await RequestHandler . ProcessRequest ( _Url , headers , mainJson , Branch : this . Config . Branch , timeout : this . Config . Timeout , proxy : this . Config . Proxy ) ;
329
- JObject data = JsonConvert . DeserializeObject < JObject > ( outputResult . Replace ( " \r \n " , "" ) , this . SerializerSettings ) ;
330
- IList contentTypes = ( IList ) data [ "content_types" ] ;
328
+ var data = JsonSerializer . Deserialize < JsonElement > ( outputResult ) ;
329
+ var contentTypes = data . GetProperty ( "content_types" ) . EnumerateArray ( ) . ToList ( ) ;
331
330
return contentTypes ;
332
331
}
333
332
catch ( Exception ex )
@@ -336,7 +335,7 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
336
335
}
337
336
}
338
337
339
- private async Task < JObject > GetLivePreviewData ( )
338
+ private async Task < JsonElement > GetLivePreviewData ( )
340
339
{
341
340
342
341
Dictionary < String , object > headerAll = new Dictionary < string , object > ( ) ;
@@ -368,8 +367,8 @@ private async Task<JObject> GetLivePreviewData()
368
367
{
369
368
HttpRequestHandler RequestHandler = new HttpRequestHandler ( this ) ;
370
369
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 ) ;
371
- JObject data = JsonConvert . DeserializeObject < JObject > ( outputResult . Replace ( " \r \n " , "" ) , this . SerializerSettings ) ;
372
- return ( JObject ) data [ "entry" ] ;
370
+ var data = JsonSerializer . Deserialize < JsonElement > ( outputResult ) ;
371
+ return data . GetProperty ( "entry" ) ;
373
372
}
374
373
catch ( Exception ex )
375
374
{
@@ -784,7 +783,7 @@ private async Task<SyncStack> GetResultAsync(string Init = "false", SyncType Syn
784
783
{
785
784
HttpRequestHandler requestHandler = new HttpRequestHandler ( this ) ;
786
785
string js = await requestHandler . ProcessRequest ( _SyncUrl , _LocalHeaders , mainJson , Branch : this . Config . Branch , timeout : this . Config . Timeout , proxy : this . Config . Proxy ) ;
787
- SyncStack stackSyncOutput = JsonConvert . DeserializeObject < SyncStack > ( js ) ;
786
+ SyncStack stackSyncOutput = JsonSerializer . Deserialize < SyncStack > ( js ) ;
788
787
return stackSyncOutput ;
789
788
}
790
789
catch ( Exception ex )
0 commit comments