I am building a custom JsonConverter, and i was wondering about the behavior of AsSpan().
public override int Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
{
return int.TryParse(Encoding.UTF8.GetString(reader.ValueSpan).AsSpan(),
System.Globalization.NumberStyles.Integer | System.Globalization.NumberStyles.AllowThousands,
System.Globalization.CultureInfo.InvariantCulture,
out int value) ? value : default;
}
Since Encoding.Utf8.GetString(...) returns a string, then AsSpan() is called, does that mean that first a string is allocated on the heap, then a span is built from it ? Or does the AsSpan() method "blocks" the string allocation and directly make it into a span ?
Thank you for the clarification