public override object Deserialize(Stream stream, Type type, DeserializeStringSpanDelegate deserializer) { var fromPool = false; if (!(stream is MemoryStream ms)) { fromPool = true; if (stream.CanSeek) stream.Position = 0; ms = stream.CopyToNewMemoryStream(); } return Deserialize(ms, fromPool, type, deserializer); }
public override async Task <object> DeserializeAsync(Stream stream, Type type, DeserializeStringSpanDelegate deserializer) { var fromPool = false; if (!(stream is MemoryStream ms)) { fromPool = true; if (stream.CanSeek) { stream.Position = 0; } ms = await stream.CopyToNewMemoryStreamAsync(); } return(Deserialize(ms, fromPool, type, deserializer)); }
private static object Deserialize(MemoryStream ms, bool fromPool, Type type, DeserializeStringSpanDelegate deserializer) { var bytes = ms.GetBufferAsBytes(); var utf8 = CharPool.GetBuffer(Encoding.UTF8.GetCharCount(bytes, 0, (int) ms.Length)); try { var charsWritten = Encoding.UTF8.GetChars(bytes, 0, (int) ms.Length, utf8, 0); var ret = deserializer(type, new ReadOnlySpan<char>(utf8, 0, charsWritten)); return ret; } finally { CharPool.ReleaseBufferToPool(ref utf8); if (fromPool) ms.Dispose(); } }
private static object Deserialize(MemoryStream ms, bool fromPool, Type type, DeserializeStringSpanDelegate deserializer) { var bytes = ms.GetBufferAsBytes(); var charPool = ArrayPool <char> .Shared; var utf8 = charPool.Rent(Encoding.UTF8.GetCharCount(bytes, 0, (int)ms.Length)); try { var charsWritten = Encoding.UTF8.GetChars(bytes, 0, (int)ms.Length, utf8, 0); var ret = deserializer(type, new ReadOnlySpan <char>(utf8, 0, charsWritten).WithoutBom()); return(ret); } finally { charPool.Return(utf8); if (fromPool) { ms.Dispose(); } } }
private static object Deserialize(MemoryStream memoryStream, bool fromPool, Type type, DeserializeStringSpanDelegate deserializer) { var bytes = memoryStream.GetBufferAsSpan().WithoutBom(); var chars = CharPool.GetBuffer(Encoding.UTF8.GetCharCount(bytes)); try { var charsWritten = Encoding.UTF8.GetChars(bytes, chars); ReadOnlySpan <char> charsSpan = chars; var ret = deserializer(type, charsSpan.Slice(0, charsWritten)); return(ret); } finally { CharPool.ReleaseBufferToPool(ref chars); if (fromPool) { memoryStream.Dispose(); } } }