calico-crusade / mangadex-sharp

A C# API for Mangadex.org
MIT License
7 stars 1 forks source link

Issue with DTO and Deserializing JSON #1

Closed H97-Git closed 1 year ago

H97-Git commented 1 year ago

Hello, I'm facing an issue while trying to integrate this repository into my project, due to the deprecation of the previous implementation I was using aelariane/mangadex-sharp. As part of my project's architecture (trying to) adhering to clean architecture principles, I am unable to directly reference the repository in the front end.

To work around this, I've attempted to create a DTO.

Here's the sequence of events:

I copied the contents of the "models" folder from the repository into my project. I addressed compilation errors as they arose during this process.

However, I'm encountering two major issues:

The JSON deserialization using System.Text.Json results in an empty object. Additionally, I'm experiencing an error when using to Newtonsoft.Json.

I'm seeking assistance and guidance to resolve these issues. Any help or suggestions on how to properly integrate the repository, create the required DTO, and successfully deserialize JSON data would be greatly appreciated. Thank you!

Please let me know if you need further information or code samples to better understand the problem.

Newtonsoft.Json.JsonSerializationException: Could not create an instance of type MangaHunter.Contracts.Mangadex.Models.Relationships.IRelationship. Type is an interface or abstract class and cannot be instantiated. Path 'MangadexDto.Data.Relationships[0].Id', line 1, position 219.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at MangaHunter.BlazorServer.Common.Services.HttpClients.ApiClient.SendHandleResponse[T](HttpRequestMessage httpRequestMessage)

chat.openai.com/d8319a3e

calico-crusade commented 1 year ago

Just to make something clear, there is no relationship between aelariane/mangadex-sharp and this repo. I didn't realize someone had already used the name "mangadex-sharp" (that's my bad). That being the case, there will be no overlap in how they are written and used:

I can already tell one major issue you're going to run into is that the other library uses Newtonsoft.Json where as my library uses newer System.Text.Json which are not compatible with each other. You can read more about the migration here. There were a few things I had to tweak about the serialization system to get it to work correctly with the MangaDex models. You can find the code for these tweaks in this folder (look for any class that ends with "parser"). The relationships objects are handled via this class and have somewhat complex logic to ensure it works correctly.

Also, as far as I can tell, the other repo does not have a nuget package that you can take advantage of, where as mine does. You should strive to use the nuget package where-ever possible in order to get any updates or bug fixes that I release. You shouldn't need to copy any of the code from my repo if you use the nuget package, but it also won't be a drop in replacement for the older repo either.

Let me know if this helped, if not, you can reach out to me via my discord server or message me directly and I can see if I can assist you better there.

calico-crusade commented 1 year ago

Closing as this isn't necessarily an issue with MD#.