public override byte[] ToUtf8ByteArray() { if (buffer == null) { return(emptyBytes); } return(ByteArrayUtil.FastCloneWithResize(buffer, offset)); }
public static T Deserialize <T>(Stream stream, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = JsonSerializer.DefaultResolver; } #if NETSTANDARD && !NET45 var ms = stream as MemoryStream; if (ms != null) { ArraySegment <byte> buf2; if (ms.TryGetBuffer(out buf2)) { // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf2.Array, buf2.Offset).GetCurrentJsonToken(); if (token == JsonToken.Number) { var buf3 = new byte[buf2.Count]; Buffer.BlockCopy(buf2.Array, buf2.Offset, buf3, 0, buf3.Length); return(Deserialize <T>(buf3, 0, resolver)); } return(Deserialize <T>(buf2.Array, buf2.Offset, resolver)); } } #endif { var buf = MemoryPool.GetBuffer(); var len = FillFromStream(stream, ref buf); // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf).GetCurrentJsonToken(); if (token == JsonToken.Number) { buf = ByteArrayUtil.FastCloneWithResize(buf, len); } return(Deserialize <T>(buf, resolver)); } }
public static async System.Threading.Tasks.Task <T> DeserializeAsync <T>(Stream stream, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = JsonSerializer.DefaultResolver; } var buffer = BufferPool.Default.Rent(); var buf = buffer; try { int length = 0; int read; while ((read = await stream.ReadAsync(buf, length, buf.Length - length).ConfigureAwait(false)) > 0) { length += read; if (length == buf.Length) { ByteArrayUtil.FastResize(ref buf, length * 2); } } // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf).GetCurrentJsonToken(); if (token == JsonToken.Number) { buf = ByteArrayUtil.FastCloneWithResize(buf, length); } return(Deserialize <T>(buf, resolver)); } finally { BufferPool.Default.Return(buffer); } }