Description
I am trying to use the new System.Text.Json to Serialize and Deserialize messages based on the JSONRPC2.0 specification(https://www.jsonrpc.org/specification). In the specification there are JSON properties that may be omitted, for example the params property.
The params property value could be any JSON value, which I think maps good to the JsonElement type.
However, when serializing, and the params property is omitted, an InvalidOperationException occurs. Is this by design? Or would it be a good idea to treat the ValuKind.Undefined the same as a null value with IgnoreNullValues=true? Perhaps a new setting IgnoreUndefinedValues=true?
I created a simple example program to reproduce this issue:
class Program
{
public static void Main(string[] args)
{
var request = new Request();
var json = JsonSerializer.Serialize(request, new JsonSerializerOptions() {IgnoreNullValues = true});
}
}
class Request
{
public string Method { get; set; } = "Test";
public JsonElement Params { get; set; }
}
Using JsonElement? or object instead works, but I think that using JsonElement will be best if possible. Because, If using object instead, I must unbox it to a JsonElement after deserializing.