How do I deserialize NuGet.NuGetVersion into a class in C#?

I'm a beginner, just writing code for an application for my work and I was trying to deserialize a class, but encountered an error. I have a class named Nuget, that looks like this:

public class Nuget
{
    public string? Name { get; set; }
    public string? Id { get; set; }
    public NuGetVersion Version { get; set; }
    public string? PackageSource { get; set; }
    public System.DateTimeOffset BlobCreation { get; set; }
}

Now I'm using .Net 6, with System.Text.Json in the method that tries to deserialize a file. When I try to do it I receive the following error message:

Encountered exception:

System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'NuGet.Versioning.NuGetVersion'. Path: $.Version | LineNumber: 0 | BytePositionInLine: 65..

Why would NuGetVersion need any constuctor?

The deserializer method is just your usual code.

            try
            {
                Nuget? deserializedObject = JsonSerializer.Deserialize<Nuget>(DirectoryManager.ReadResourceFromExternal(filePath));
                if (deserializedObject is null) throw new ArgumentNullException("deserializedObject");

                return deserializedObject;
            }
            catch (Exception ex)
            {
                LogManager.CatchLog(ex, new string[] {
                $"Loading the requested file of {filePath } is not possible\n",
                "Please check the filepath"
            });
            }

The ReadResourceFromExternal method returns the decrypted file in a string format.

    public static string ReadResourceFromExternal(string filePath) {
        using Stream stream = new MemoryStream(buffer: DAPIManager.Decrypt(filePath));
        using StreamReader reader = new(stream);
        return reader.ReadToEnd();
    }

So far, I'm just trying to understand why NuGet.Version is not acceptable, since it has a {get;set;}, isn't this a parameterless constructor?

I was pondering if I should just have a string instead and convert the NuGet.Version manually.

Or am I misunderstanding the issue here?



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation