JsonSerializer не использует внутренний конструктор во время десериализации ⇐ C#
-
Гость
JsonSerializer не использует внутренний конструктор во время десериализации
I'm writing a library to access a web API used to manage data in the backend system. The library will also provide classes with logic to properly manage the data. For this reason most of the classes cannot have public setters for properties or a public constructor that accepts all the properties. Instead I'm trying to use a constructor with internal access modifier that can set all the properties.
When trying to deserialize a JSON string using System.Text.Json it will ignore the internal constructor - it will either use some other constructor or throw an exception. I've also tried annotating the internal constructor with [JsonConstructor] but it does nothing.
Example class with internal constructor:
public class Entry { public int Id { get; } public string Name { get; set; } public string Value { get; set; } [JsonConstructor] internal Entry(int id, string name, string value) { this.Id = id; this.Name = name; this.Value = value; } } Suppose the user can use the library to get an Entry from the backend, modify its name and value, and update it. User should not be able to change the Id property or create a new Entry.
Example deserialization:
var json = "{\"id\": 1337, \"name\": \"TestEntry\", \"value\": \"TestValue\"}"; var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); var entry = JsonSerializer.Deserialize(json, options); Calling JsonSerializer.Deserialize will throw System.NotSupportedException - Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported:
A possible solution is to just make the constructor public and annotate it with [Obsolete("Message", true)] attribute - when trying to use this constructor the compiler will throw and error, making the constructor unusable. But I feel this is not the most elegant solution and it also misuses the [Obsolete] attribute.
Is there any other way of solving this problem?
Источник: https://stackoverflow.com/questions/702 ... ialization
I'm writing a library to access a web API used to manage data in the backend system. The library will also provide classes with logic to properly manage the data. For this reason most of the classes cannot have public setters for properties or a public constructor that accepts all the properties. Instead I'm trying to use a constructor with internal access modifier that can set all the properties.
When trying to deserialize a JSON string using System.Text.Json it will ignore the internal constructor - it will either use some other constructor or throw an exception. I've also tried annotating the internal constructor with [JsonConstructor] but it does nothing.
Example class with internal constructor:
public class Entry { public int Id { get; } public string Name { get; set; } public string Value { get; set; } [JsonConstructor] internal Entry(int id, string name, string value) { this.Id = id; this.Name = name; this.Value = value; } } Suppose the user can use the library to get an Entry from the backend, modify its name and value, and update it. User should not be able to change the Id property or create a new Entry.
Example deserialization:
var json = "{\"id\": 1337, \"name\": \"TestEntry\", \"value\": \"TestValue\"}"; var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); var entry = JsonSerializer.Deserialize(json, options); Calling JsonSerializer.Deserialize will throw System.NotSupportedException - Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported:
A possible solution is to just make the constructor public and annotate it with [Obsolete("Message", true)] attribute - when trying to use this constructor the compiler will throw and error, making the constructor unusable. But I feel this is not the most elegant solution and it also misuses the [Obsolete] attribute.
Is there any other way of solving this problem?
Источник: https://stackoverflow.com/questions/702 ... ialization
Мобильная версия