简体   繁体   中英

C# - Serialize all attributes of derived class in a list of base class

I need to JsonSerializer.Serialize(...) a class containig a list of a base class:

 // ----- Models ----- public class MainClass { [Key] public Guid Id { get; private set; } = Guid.NewGuid(); public List<BaseClass> Properties { get; set; } = new List<BaseClass>(); } public class BaseClass { [Key] public Guid Id { get; private set; } = Guid.NewGuid(); public string Name { get; set; } = string.Empty; } public class GenericDerivedClass<T>: BaseClass { public T? Value { get; set; } } // ----- Implementation ----- var main = new MainClass { Properties = new List<BaseClass> { new GenericDerivedClass<string> { Name = "SoundFile", Value = "Test.wav" }, new GenericDerivedClass<float> { Name = "Volume", Value = 1 }, new GenericDerivedClass<bool> { Name = "Autoplay", Value = false }, new GenericDerivedClass<bool> { Name = "Loop", Value = false }, } }; Console.WriteLine(JsonSerializer.Serialize(main, new JsonSerializerOptions { WriteIndented = true })); // ----- Output (JsonSerializer) ---- [ { "main": [ { "id": "ba348c86-aa86-45ea-8d21-a9beddd4368a", "properties": [ { "id": "a9f432d5-3916-4c1d-b44a-fd4b7d8fcb45", "name": "SoundFile", //"value": "Test.wav" <- I want this line here, but I cannot figure out how. }, { "id": "f585d863-b0d7-49b3-ad5c-0565171e6793", "name": "Volume" }, { "id": "197802f3-17cd-4c1f-90be-7ea643ee5d7d", "name": "Autoplay" }, { "id": "b90e3857-e497-4137-adeb-94b66293d375", "name": "Loop" } ] } ], } ]

The problem here is, that only the properties of the base class (BaseClass) are serialized (Id and Name). Is there a way to serialize the class "MainClass" with a List<BaseClass> containing the information (Value) of each GenericDerivedClass<T> ?

(Using List<object> instead of List<BaseClass> is not an option, since I cannot use primitive types.)

System.Text.Json does not support it by design . However there might be a workaround- if you add [JsonIgnore] before the Properties property and add a property such as

[JsonPropertyName] public IEnumerable<object> JsonProperties {get=> Items;}

Keep in mind that you still can't deserialize it

Oliver comment got me on the right track, thank you.

I'll do this now:

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-6-0#support-polymorphic-deserialization

It's a bit tideous, to go over all "propertynames" in a switch, but it seems to do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM