C
C#8mo ago
cray.io

Issues deserializing data using System.Text.Json when object fields are not known at runtime.

I have a bit of an interesting problem deserializing from an external API using System.Text.Json. One of the endpoints essentially returns an array of key-value pairs, but the fields are not pre-determined and the type of the value in the KV pair can be either of bool, string, datetime, etc.. How would you go about deserializing this data? I thought of ArrayList but thats not performant, deprecated, and just bad in general. Dictionary<string, object> forces the user to perform their own parsing e.g. bool.Parse(dict['key'].ToString()). Or I can just use a JsonNode/JsonElement and leave it completely up to them to handle. Any thoughts or opinions? Anything I'm missing? .Net 6 btw.
1 Reply
Joschi
Joschi8mo ago
Can you share a few example json snippets?