Skip to content

feat: Refactor Entry and EntryJsonConverter file #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Contentstack.Core/Internals/EntryJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Contentstack.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Contentstack.Core.Internals
{
[CSJsonConverter("EntryJsonConverter")]
public class EntryJsonConverter : JsonConverter<Entry>
{
public override Entry ReadJson(JsonReader reader, Type objectType, Entry existingValue, bool hasExistingValue, JsonSerializer serializer)
public override Entry Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
JObject jObject = JObject.Load(reader);
JsonSerializerSettings SerializerSettings = new JsonSerializerSettings();
JsonSerializer Serializer = JsonSerializer.Create(SerializerSettings);
Entry entry = jObject.ToObject<Entry>(Serializer);
entry.ParseObject(jObject);
return entry;
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
{
JsonElement root = document.RootElement;
Entry entry = JsonSerializer.Deserialize<Entry>(root.GetRawText(), options);
entry.ParseObject(root);
return entry;
}
}

public override void WriteJson(JsonWriter writer, Entry value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Entry value, JsonSerializerOptions options)
{

}
Expand Down
37 changes: 18 additions & 19 deletions Contentstack.Core/Models/Entry.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Markdig;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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.Internals;
using Contentstack.Core.Configuration;
Expand All @@ -24,7 +24,7 @@ public class Entry
private CachePolicy _CachePolicy;
private Dictionary<string, object> UrlQueries = new Dictionary<string, object>();
private bool _IsCachePolicySet;
private JObject jObject;
private JsonObject jsonObject;
private string _Url
{
get
Expand Down Expand Up @@ -169,38 +169,37 @@ internal Entry(string contentTypeName)
#region Internal Functions
internal static ContentstackException GetContentstackError(Exception ex)
{
Int32 errorCode = 0;
int errorCode = 0;
string errorMessage = string.Empty;
HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
ContentstackException contentstackError = new ContentstackException(ex);
Dictionary<string, object> errors = null;

try
{
System.Net.WebException webEx = (System.Net.WebException)ex;
WebException webEx = (WebException)ex;

using (var exResp = webEx.Response)
using (var stream = exResp.GetResponseStream())
using (var reader = new StreamReader(stream))
{
errorMessage = reader.ReadToEnd();
JObject data = JObject.Parse(errorMessage.Replace("\r\n", ""));
JsonObject data = JsonNode.Parse(errorMessage.Replace("\r\n", ""))?.AsObject();

JToken token = data["error_code"];
if (token != null)
errorCode = token.Value<int>();
if (data != null)
{
if (data.TryGetPropertyValue("error_code", out var token))
errorCode = token.GetValue<int>();

token = data["error_message"];
if (token != null)
errorMessage = token.Value<string>();
if (data.TryGetPropertyValue("error_message", out token))
errorMessage = token.GetValue<string>();

token = data["errors"];
if (token != null)
errors = token.ToObject<Dictionary<string, object>>();
if (data.TryGetPropertyValue("errors", out token))
errors = JsonSerializer.Deserialize<Dictionary<string, object>>(token.ToJsonString());

var response = exResp as HttpWebResponse;
if (response != null)
statusCode = response.StatusCode;
if (exResp is HttpWebResponse response)
statusCode = response.StatusCode;
}
}
}
catch
Expand Down Expand Up @@ -887,7 +886,7 @@ public JObject ToJson()
private Asset GetAsset(String key)
{

JObject assetObject = (JObject)jObject.GetValue(key);
JsonElement assetObject = (JObject)jObject.GetValue(key);
var asset = ContentTypeInstance.StackInstance.Asset();
asset.ParseObject(assetObject);
return asset;
Expand Down
Loading