I have a class with a method that serializes an object into a string and then saves it as a JSON file and another method which deserializes that file back into its original object. The serialization method functions perfectly and outputs a file with all the data in the expected JSON format.
However, on deserialization, I get the following error:
Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Octopus.Client.Model.PersistenceSettingsResource'
After researching this, I found that the most common solution is to add [JsonConstructor] public PackagedProject() { }
to the class I'm trying to deserialize to. Not only does this result in the same error, but I'm pretty sure I've used this exact method before without having to add a constructor, though I can't say for certain my memory isn't betraying me.
Here are the relevant parts of my code, only concerned with the class definition and file serialization and deserialization:
public class PackagedProject
{
[JsonConstructor]
public PackagedProject() { }
public ProjectResource ProjectResource { get; set; }
public VariableSetResource VariableSetResource { get; set; }
}
public class OctopusService
{
public OctopusService(IConfiguration config)
{
var filename = "projects.json";
SerializeProjects(packagedProject, filename);
var deserializedProjects = DeserializedProjects(filename);
}
public void SerializeProjects(List<PackagedProject> projects, string filename)
{
var jsonString = JsonSerializer.Serialize(projects, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(filename, jsonString);
}
public List<PackagedProject> DeserializedProjects(string filename)
{
var currentDir = Directory.GetCurrentDirectory();
var fileText = File.ReadAllText(currentDir + "/" + filename);
var deserializedProjects = JsonSerializer.Deserialize<List<PackagedProject>>(fileText);
return deserializedProjects;
}
}
Additionally, the end of the error message is Type 'Octopus.Client.Model.PersistenceSettingsResource
. I'm not sure what this means, but my best guess is that the Octopus.Client
is the class type which is having trouble deserializing, but that doesn't make sense because I've specified that I want it to deserialize to List<PackagedProject>
, not an Octopus.Client
model.