11using System ;
2- using System . Collections ;
32using 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 ;
59using System . Linq ;
6- using System . Net ;
7- using System . Text . Json ;
8- using System . Text . Json . Serialization ;
910using System . Threading . Tasks ;
10- using Contentstack . Core . Configuration ;
11+ using System . Net ;
12+ using System . IO ;
13+ using System . Collections ;
14+ using Contentstack . Utils ;
1115using Contentstack . Core . Interfaces ;
12- using Contentstack . Core . Internals ;
13- using Contentstack . Core . Models ;
14- using Microsoft . Extensions . Options ;
1516
1617namespace 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 )
0 commit comments