11using System ;
2+ using System . Collections ;
23using 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 ;
95using System . Linq ;
10- using System . Threading . Tasks ;
116using 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 ;
1511using Contentstack . Core . Interfaces ;
12+ using Contentstack . Core . Internals ;
13+ using Contentstack . Core . Models ;
14+ using Microsoft . Extensions . Options ;
1615
1716namespace Contentstack . Core
1817{
@@ -24,7 +23,16 @@ public class ContentstackClient
2423 /// <summary>
2524 /// Gets or sets the settings that should be used for deserialization.
2625 /// </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+ } ;
2836
2937 #region Internal Variables
3038
@@ -35,7 +43,6 @@ internal string StackApiKey
3543 }
3644 private ContentstackOptions _options ;
3745
38- internal JsonSerializer Serializer => JsonSerializer . Create ( SerializerSettings ) ;
3946 internal string _SyncUrl
4047 {
4148 get
@@ -133,10 +140,6 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
133140 throw new InvalidOperationException ( "Add PreviewToken or ManagementToken in LivePreviewConfig" ) ;
134141 }
135142 }
136- this . SerializerSettings . DateParseHandling = DateParseHandling . None ;
137- this . SerializerSettings . DateFormatHandling = DateFormatHandling . IsoDateFormat ;
138- this . SerializerSettings . DateTimeZoneHandling = DateTimeZoneHandling . Utc ;
139- this . SerializerSettings . NullValueHandling = NullValueHandling . Ignore ;
140143
141144 foreach ( Type t in CSJsonConverterAttribute . GetCustomAttribute ( typeof ( CSJsonConverterAttribute ) ) )
142145 {
@@ -209,22 +212,18 @@ internal static ContentstackException GetContentstackError(Exception ex)
209212 using ( var reader = new StreamReader ( stream ) )
210213 {
211214 errorMessage = reader . ReadToEnd ( ) ;
212- JObject data = JObject . Parse ( errorMessage . Replace ( " \r \n " , "" ) ) ;
215+ var data = JsonSerializer . Deserialize < JsonElement > ( errorMessage ) ;
213216
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 ( ) ;
217219
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 ( ) ;
221222
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 ( ) ) ;
225225
226- var response = exResp as HttpWebResponse ;
227- if ( response != null )
226+ if ( exResp is HttpWebResponse response )
228227 statusCode = response . StatusCode ;
229228 }
230229 }
@@ -326,8 +325,8 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
326325 {
327326 HttpRequestHandler RequestHandler = new HttpRequestHandler ( this ) ;
328327 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 ( ) ;
331330 return contentTypes ;
332331 }
333332 catch ( Exception ex )
@@ -336,7 +335,7 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
336335 }
337336 }
338337
339- private async Task < JObject > GetLivePreviewData ( )
338+ private async Task < JsonElement > GetLivePreviewData ( )
340339 {
341340
342341 Dictionary < String , object > headerAll = new Dictionary < string , object > ( ) ;
@@ -368,8 +367,8 @@ private async Task<JObject> GetLivePreviewData()
368367 {
369368 HttpRequestHandler RequestHandler = new HttpRequestHandler ( this ) ;
370369 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" ) ;
373372 }
374373 catch ( Exception ex )
375374 {
@@ -786,7 +785,7 @@ private async Task<SyncStack> GetResultAsync(string Init = "false", SyncType Syn
786785 {
787786 HttpRequestHandler requestHandler = new HttpRequestHandler ( this ) ;
788787 string js = await requestHandler . ProcessRequest ( _SyncUrl , _LocalHeaders , mainJson , Branch : this . Config . Branch , timeout : this . Config . Timeout , proxy : this . Config . Proxy ) ;
789- SyncStack stackSyncOutput = JsonConvert . DeserializeObject < SyncStack > ( js ) ;
788+ SyncStack stackSyncOutput = JsonSerializer . Deserialize < SyncStack > ( js ) ;
790789 return stackSyncOutput ;
791790 }
792791 catch ( Exception ex )
0 commit comments