I'm trying to deserialize JSON such as this:
{ "Parameters": [ 1, "2", true, false, null ] }
using System.Text.Json serializer. Target class looks like this:
public class Payload {
public object[] Parameters { get; set; }
}
Parameters are always primitive values like numbers, strings, booleans, etc.
But looks like System.Text.Json populates my Parameters array with
JsonElement values instead of plain scalar values. Here is the code sample:
var payload = new Payload {
Parameters = new object[] {
1, "2", true, false, null
}
};
var json = JsonSerializer.Serialize(payload);
// result: {"Parameters":[1,"2",true,false,null]}
var deserialized = JsonSerializer.Deserialize<Payload>(json);
// result: deserialized.Parameters are all `JsonElement` values
The code that consumes the Payload class doesn't depend on System.Text.Json,
it is serializer-agnostic. Is there a way to deserialize the array of objects
using System.Text.Json and get back plain scalar values instead of JsonElements?
JsonConverter<object>?