C
C#β€’7mo ago
Will Pittenger

βœ… How to deserialize JSON in .NET when my base class is generic?

I have a concrete class, ClientEventInfo derived from a generic. So I tried to deserialize a JSON file with System.Text.Json.JsonSerializer.Deserialize<ClientEventInfo>("url"), but I get System.TypeInitializationException. Seems the generic can't be initialized. Is there a way around it? I don't know what's going on here.
9 Replies
softmek
softmekβ€’7mo ago
When attempting to directly deserialize a derived generic class like ClientEventInfo using System.Text.Json.JsonSerializer, a TypeInitializationException might arise due to complexities in initializing such derived generic types. To overcome this, you can create a non-generic wrapper class for deserialization or use custom deserialization methods to handle the process, avoiding direct deserialization of the derived generic type. Alternatively, consider refactoring the classes or employing third-party libraries like Newtonsoft.Json (Json.NET) which often handle derived generic types more seamlessly during deserialization, offering a smoother and exception-free deserialization process. You can aswell desrialize to ViewModel Class..
Will Pittenger
Will Pittengerβ€’7mo ago
I've been reconfiguring the code to evaluate the JsonElements myself. The simple deserialization couldn't handle the complex nature of what I was loading. The more I looked at it, I needed more data types. (ClientEventInfo isn't that complex.) So some of the other event info classes will be only abstract so derived types can add more fields. I'll start with a Type enum to decide which abstract base class is involved. Each of those declares a series of subtypes as more enums. Eventually, I'll know the correct type. The problem might actually have the lack of parameterless constructors. I didn't design the system to be compatible with JSON. This is only for a demo version of the app.
softmek
softmekβ€’7mo ago
You're reconfiguring the code to handle complex JSON structures by evaluating JsonElements and introducing a more intricate class hierarchy. Using an enum to discern abstract base classes and their derived types allows for a more nuanced representation of various event information types. This adjustment accommodates additional fields in the derived types while addressing the lack of parameterless constructors, likely impacting JSON serialization/deserialization processes. Even though the system wasn't initially designed for JSON compatibility, this restructuring aligns well with the project's requirements for a demo application, providing a more tailored approach to handle diverse data structures.
Will Pittenger
Will Pittengerβ€’7mo ago
Mostly, I probably just needed to talk about the problem. /maybe someone in a similar situation will do the same thing.
softmek
softmekβ€’7mo ago
πŸ‘Œ sure, Appreciated @Will Pittenger That’s mind blowing, mastering your craft!!
Will Pittenger
Will Pittengerβ€’7mo ago
😁 Started programming as a 15-year-old in 1983 on my Dad's Apple ][+ with AppleSoft. I have been wondering if XML would be a better choice for my sample data.
softmek
softmekβ€’7mo ago
haha, that would be an interesting bit, i would love to chop it in my techniical field
Will Pittenger
Will Pittengerβ€’7mo ago
???
Accord
Accordβ€’7mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.