I am trying to write a function that handles empty values for the datatype Byte
.
The reader is reading in a JSON string and serializing to my models.
One of the models has a Byte
property.
If the reader encounters a blank or empty string for a Byte, then it should convert it to Null.
But whenever the function is used, I get this error:
System.InvalidOperationException: 'Cannot get the value of a token type 'String' as a number.'
Here is my function:
public override Byte? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var @byte = reader.GetByte();
if (string.IsNullOrWhiteSpace(@byte.ToString()))
{
return null;
}
return Byte.Parse(@byte.ToString());
}
I am not sure why it is giving me the error or how to fix?
Thanks!