/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/SignalR/common/Shared/ReusableUtf8JsonWriter.cs
70 строк
2 KB
Pranav K
Fixup whitespace issues from top-level statement change (#39161)
23 дек 2021, 09:35
Не верифицирован
23 дек 2021, 09:35
c65dac7
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Buffers; using System.Text.Encodings.Web; using System.Text.Json; namespace Microsoft.AspNetCore.Internal; internal sealed class ReusableUtf8JsonWriter { [ThreadStatic] private static ReusableUtf8JsonWriter? _cachedInstance; private readonly Utf8JsonWriter _writer; #if DEBUG private bool _inUse; #endif public ReusableUtf8JsonWriter(IBufferWriter<byte> stream) { _writer = new Utf8JsonWriter(stream, new JsonWriterOptions() { #if !DEBUG SkipValidation = true, #endif Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }); } public static ReusableUtf8JsonWriter Get(IBufferWriter<byte> stream) { var writer = _cachedInstance; if (writer == null) { writer = new ReusableUtf8JsonWriter(stream); } // Taken off the thread static _cachedInstance = null; #if DEBUG if (writer._inUse) { throw new InvalidOperationException("The writer wasn't returned!"); } writer._inUse = true; #endif writer._writer.Reset(stream); return writer; } public static void Return(ReusableUtf8JsonWriter writer) { _cachedInstance = writer; writer._writer.Reset(); #if DEBUG writer._inUse = false; #endif } public Utf8JsonWriter GetJsonWriter() { return _writer; } }