✅ How to deal with different JSON types
I have an API endpoint and depending on the query it can return a list of objects or an object as such:
Query for multiple
Query for single
For now I'm only allowing queries for multiple but would like to support both.
Is there a way to support this?
6 Replies
Oof, malformed JSON is never fun to deal with...
You'll need a custom converter: https://stackoverflow.com/a/59430729/6042255
I'm using polymorphic deserialization as well 😦
Already tried a custom converter, wont work in combination with it
I found this
GitHub
Combining 'JsonDerivedType' attribute with a custom 'JsonConverter'...
Issue summary The polymorphic JSON deserialization in System.Text.Json has some limitations when combined with custom JSON converters. This is documented here: Polymorphism using type discriminator...
Oh I see in the thread you linked you can use it on specific properties, I'll try that out
Woo, that works 🙂 thx
$close
Use the /close command to mark a forum thread as answered