我有这个jsonconverter,它需要将给定的属性值转换为小数或长整数,具体取决于值 - 但我似乎无法确定属性值何时是小数或长整数,因为 tokentype 只能检测数字.. . 我该如何解决这个问题?
public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
IDictionary<string, object> output = new Dictionary<string, object>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
string propertyName = reader.GetString();
reader.Read();
object? propertyValue = null;
switch (reader.TokenType)
{
case JsonTokenType.Number:
propertyValue = reader.GetInt64(); // or could be a decimal for where I should reader.GetDecimal()
break;
case JsonTokenType.String:
if (reader.TryGetDateTime(out DateTime value))
{
propertyValue = value;
}
else
{
propertyValue = reader.GetString();
}
break;
case JsonTokenType.True:
case JsonTokenType.False:
propertyValue = reader.GetBoolean();
break;
}
output.Add(propertyName, propertyValue);
}
return output;
}