< Summary

Information
Class: System.Text.Json.JsonSerializer
Assembly: System.Text.Json
File(s): File 1: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Helpers.cs
File 2: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Document.cs
File 3: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Element.cs
File 4: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandleMetadata.cs
File 5: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandlePropertyName.cs
File 6: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Node.cs
File 7: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Pipe.cs
File 8: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Span.cs
File 9: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Stream.cs
File 10: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs
File 11: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Utf8JsonReader.cs
File 12: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.ByteArray.cs
File 13: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Document.cs
File 14: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Element.cs
File 15: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.HandleMetadata.cs
File 16: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Node.cs
File 17: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Pipe.cs
File 18: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Stream.cs
File 19: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.String.cs
File 20: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Utf8JsonWriter.cs
Line coverage
2%
Covered lines: 39
Uncovered lines: 1501
Coverable lines: 1540
Total lines: 4929
Line coverage: 2.5%
Branch coverage
1%
Covered branches: 4
Total branches: 368
Branch coverage: 1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
File 1: .cctor()50%22100%
File 1: GetTypeInfo(...)75%44100%
File 1: GetTypeInfo(...)100%110%
File 1: GetTypeInfo(...)0%220%
File 1: ValidateInputType(...)0%440%
File 1: IsValidNumberHandlingValue(...)100%11100%
File 1: IsValidCreationHandlingValue(...)0%220%
File 1: IsValidUnmappedMemberHandlingValue(...)0%220%
File 1: UnboxOnRead(...)0%880%
File 1: ThrowUnableToCastValue(System.Object)0%220%
File 1: UnboxOnWrite(...)0%440%
File 1: GetOrAddListTypeInfoForRootLevelValueMode(...)0%220%
File 1: GetOrAddListTypeInfoForArrayMode(...)0%440%
File 2: Deserialize(...)100%110%
File 2: Deserialize(...)100%110%
File 2: Deserialize(...)100%110%
File 2: Deserialize(...)100%110%
File 2: Deserialize(...)100%110%
File 3: Deserialize(...)100%110%
File 3: Deserialize(...)100%110%
File 3: Deserialize(...)100%110%
File 3: Deserialize(...)100%110%
File 3: Deserialize(...)100%110%
File 4: .cctor()100%11100%
File 4: TryReadMetadata(...)0%86860%
File 4: IsMetadataPropertyName(...)0%880%
File 4: GetMetadataPropertyName(...)0%28280%
File 4: TryHandleReferenceFromJsonElement(...)0%20200%
File 4: TryHandleReferenceFromJsonNode(...)0%16160%
File 4: ReadAsStringMetadataValue(System.Text.Json.Nodes.JsonNode)0%880%
File 4: ValidateMetadataForObjectConverter(...)0%220%
File 4: ValidateMetadataForArrayConverter(...)0%880%
File 4: ResolveReferenceId(...)100%110%
File 5: LookupProperty(...)0%12120%
File 5: GetPropertyName(...)0%660%
File 5: CreateExtensionDataProperty(...)0%32320%
File 6: Deserialize(...)100%110%
File 6: Deserialize(...)100%110%
File 6: Deserialize(...)100%110%
File 6: Deserialize(...)100%110%
File 6: Deserialize(...)100%110%
File 6: ReadFromNode(...)0%220%
File 6: ReadFromNodeAsObject(...)0%220%
File 7: DeserializeAsync(...)100%110%
File 7: DeserializeAsync(...)100%110%
File 7: DeserializeAsync(...)100%110%
File 7: DeserializeAsync(...)100%110%
File 7: DeserializeAsync(...)100%11100%
File 7: DeserializeAsyncEnumerable(...)100%110%
File 7: DeserializeAsyncEnumerable(...)100%110%
File 7: DeserializeAsyncEnumerable(...)100%110%
File 7: DeserializeAsyncEnumerable(...)100%110%
File 7: DeserializeAsyncEnumerableCore(...)0%220%
File 7: CreateAsyncEnumerableFromArray()0%10100%
File 8: Deserialize(...)100%110%
File 8: Deserialize(...)100%110%
File 8: Deserialize(...)100%110%
File 8: Deserialize(...)100%110%
File 8: Deserialize(...)100%110%
File 8: ReadFromSpan(...)0%440%
File 8: ReadFromSpanAsObject(...)0%440%
File 9: DeserializeAsync(...)100%110%
File 9: Deserialize(...)100%110%
File 9: DeserializeAsync(...)100%11100%
File 9: Deserialize(...)100%110%
File 9: DeserializeAsync(...)100%110%
File 9: DeserializeAsync(...)100%110%
File 9: Deserialize(...)100%110%
File 9: Deserialize(...)100%110%
File 9: DeserializeAsync(...)100%110%
File 9: Deserialize(...)100%110%
File 9: DeserializeAsyncEnumerable(...)100%110%
File 9: DeserializeAsyncEnumerable(...)100%110%
File 9: DeserializeAsyncEnumerable(...)100%110%
File 9: DeserializeAsyncEnumerable(...)100%110%
File 9: DeserializeAsyncEnumerableCore(...)0%220%
File 9: CreateAsyncEnumerableFromArray()0%10100%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: Deserialize(...)100%110%
File 10: ReadFromSpan(...)0%660%
File 10: ReadFromSpanAsObject(...)0%660%
File 11: Deserialize(...)100%110%
File 11: Deserialize(...)100%110%
File 11: Deserialize(...)100%110%
File 11: Deserialize(...)100%110%
File 11: Deserialize(...)100%110%
File 11: Read(...)0%220%
File 11: ReadAsObject(...)0%220%
File 11: GetReaderScopedToNextValue(...)0%44440%
File 12: SerializeToUtf8Bytes(...)100%110%
File 12: SerializeToUtf8Bytes(...)100%110%
File 12: SerializeToUtf8Bytes(...)100%110%
File 12: SerializeToUtf8Bytes(...)100%110%
File 12: SerializeToUtf8Bytes(...)100%110%
File 12: WriteBytes(...)100%110%
File 12: WriteBytesAsObject(...)100%110%
File 13: SerializeToDocument(...)100%110%
File 13: SerializeToDocument(...)100%110%
File 13: SerializeToDocument(...)100%110%
File 13: SerializeToDocument(...)100%110%
File 13: SerializeToDocument(...)100%110%
File 13: WriteDocument(...)100%110%
File 13: WriteDocumentAsObject(...)100%110%
File 14: SerializeToElement(...)100%110%
File 14: SerializeToElement(...)100%110%
File 14: SerializeToElement(...)100%110%
File 14: SerializeToElement(...)100%110%
File 14: SerializeToElement(...)100%110%
File 14: WriteElement(...)100%110%
File 14: WriteElementAsObject(...)100%110%
File 15: .cctor()100%11100%
File 15: WriteMetadataForObject(...)0%880%
File 15: WriteMetadataForCollection(...)100%110%
File 15: TryGetReferenceForValue(...)0%220%
File 16: SerializeToNode(...)100%110%
File 16: SerializeToNode(...)100%110%
File 16: SerializeToNode(...)100%110%
File 16: SerializeToNode(...)100%110%
File 16: SerializeToNode(...)100%110%
File 16: WriteNode(...)100%110%
File 16: WriteNodeAsObject(...)100%110%
File 17: SerializeAsync(...)100%110%
File 17: SerializeAsync(...)100%110%
File 17: SerializeAsync(...)100%110%
File 17: SerializeAsync(...)100%110%
File 17: SerializeAsync(...)100%110%
File 18: SerializeAsync(...)100%110%
File 18: Serialize(...)100%110%
File 18: SerializeAsync(...)100%110%
File 18: Serialize(...)100%110%
File 18: SerializeAsync(...)100%110%
File 18: Serialize(...)100%110%
File 18: SerializeAsync(...)100%110%
File 18: Serialize(...)100%110%
File 18: SerializeAsync(...)100%110%
File 18: Serialize(...)100%110%
File 19: Serialize(...)100%110%
File 19: Serialize(...)100%110%
File 19: Serialize(...)100%110%
File 19: Serialize(...)100%110%
File 19: Serialize(...)100%110%
File 19: WriteString(...)100%110%
File 19: WriteStringAsObject(...)100%110%
File 20: Serialize(...)100%110%
File 20: Serialize(...)100%110%
File 20: Serialize(...)100%110%
File 20: Serialize(...)100%110%
File 20: Serialize(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Helpers.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Text.Json.Serialization;
 8using System.Text.Json.Serialization.Converters;
 9using System.Text.Json.Serialization.Metadata;
 10
 11namespace System.Text.Json
 12{
 13    public static partial class JsonSerializer
 14    {
 15        internal const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might requi
 16        internal const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might re
 17
 18        /// <summary>
 19        /// Indicates whether unconfigured <see cref="JsonSerializerOptions"/> instances
 20        /// should be set to use the reflection-based <see cref="DefaultJsonTypeInfoResolver"/>.
 21        /// </summary>
 22        /// <remarks>
 23        /// The value of the property is backed by the "System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault"
 24        /// <see cref="AppContext"/> setting and defaults to <see langword="true"/> if unset.
 25        /// </remarks>
 26        [FeatureSwitchDefinition("System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault")]
 68227        public static bool IsReflectionEnabledByDefault { get; } =
 128            AppContext.TryGetSwitch(
 129                switchName: "System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault",
 130                isEnabled: out bool value)
 131            ? value : true;
 32
 33        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 34        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 35        private static JsonTypeInfo GetTypeInfo(JsonSerializerOptions? options, Type inputType)
 5592436        {
 5592437            Debug.Assert(inputType != null);
 38
 5592439            options ??= JsonSerializerOptions.Default;
 5592440            options.MakeReadOnly(populateMissingResolver: true);
 41
 42            // In order to improve performance of polymorphic root-level object serialization,
 43            // we bypass GetTypeInfoForRootType and cache JsonTypeInfo<object> in a dedicated property.
 44            // This lets any derived types take advantage of the cache in GetTypeInfoForRootType themselves.
 5592445            return inputType == JsonTypeInfo.ObjectType
 5592446                ? options.ObjectTypeInfo
 5592447                : options.GetTypeInfoForRootType(inputType);
 5592448        }
 49
 50        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 51        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 52        private static JsonTypeInfo<T> GetTypeInfo<T>(JsonSerializerOptions? options)
 053            => (JsonTypeInfo<T>)GetTypeInfo(options, typeof(T));
 54
 55        private static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type inputType)
 056        {
 057            Debug.Assert(context != null);
 058            Debug.Assert(inputType != null);
 59
 060            JsonTypeInfo? info = context.GetTypeInfo(inputType);
 061            if (info is null)
 062            {
 063                ThrowHelper.ThrowInvalidOperationException_NoMetadataForType(inputType, context);
 64            }
 65
 066            info.EnsureConfigured();
 067            return info;
 068        }
 69
 70        private static void ValidateInputType(object? value, Type inputType)
 071        {
 072            ArgumentNullException.ThrowIfNull(inputType);
 73
 074            if (value is not null)
 075            {
 076                Type runtimeType = value.GetType();
 077                if (!inputType.IsAssignableFrom(runtimeType))
 078                {
 079                    ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value);
 80                }
 081            }
 082        }
 83
 84        internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) =>
 68285            JsonHelpers.IsInRangeInclusive((int)handling, 0,
 68286                (int)(
 68287                JsonNumberHandling.Strict |
 68288                JsonNumberHandling.AllowReadingFromString |
 68289                JsonNumberHandling.WriteAsString |
 68290                JsonNumberHandling.AllowNamedFloatingPointLiterals));
 91
 92        internal static bool IsValidCreationHandlingValue(JsonObjectCreationHandling handling) =>
 093            handling is JsonObjectCreationHandling.Replace or JsonObjectCreationHandling.Populate;
 94
 95        internal static bool IsValidUnmappedMemberHandlingValue(JsonUnmappedMemberHandling handling) =>
 096            handling is JsonUnmappedMemberHandling.Skip or JsonUnmappedMemberHandling.Disallow;
 97
 98        [return: NotNullIfNotNull(nameof(value))]
 99        internal static T? UnboxOnRead<T>(object? value)
 0100        {
 0101            if (value is null)
 0102            {
 0103                if (default(T) is not null)
 0104                {
 105                    // Casting null values to a non-nullable struct throws NullReferenceException.
 0106                    ThrowUnableToCastValue(value);
 0107                }
 108
 0109                return default;
 110            }
 111
 0112            if (value is T typedValue)
 0113            {
 0114                return typedValue;
 115            }
 116
 0117            ThrowUnableToCastValue(value);
 0118            return default!;
 119
 120            static void ThrowUnableToCastValue(object? value)
 0121            {
 0122                if (value is null)
 0123                {
 0124                    ThrowHelper.ThrowInvalidOperationException_DeserializeUnableToAssignNull(declaredType: typeof(T));
 125                }
 126                else
 0127                {
 0128                    ThrowHelper.ThrowInvalidCastException_DeserializeUnableToAssignValue(typeOfValue: value.GetType(), d
 129                }
 130            }
 0131        }
 132
 133        [return: NotNullIfNotNull(nameof(value))]
 134        internal static T? UnboxOnWrite<T>(object? value)
 0135        {
 0136            if (default(T) is not null && value is null)
 0137            {
 138                // Casting null values to a non-nullable struct throws NullReferenceException.
 0139                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(typeof(T));
 140            }
 141
 0142            return (T?)value;
 0143        }
 144
 145        private static JsonTypeInfo<List<T?>> GetOrAddListTypeInfoForRootLevelValueMode<T>(JsonTypeInfo<T> elementTypeIn
 0146        {
 0147            if (elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo != null)
 0148            {
 0149                return (JsonTypeInfo<List<T?>>)elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo;
 150            }
 151
 0152            var converter = new RootLevelListConverter<T>(elementTypeInfo);
 0153            var listTypeInfo = new JsonTypeInfo<List<T?>>(converter, elementTypeInfo.Options)
 0154            {
 0155                ElementTypeInfo = elementTypeInfo,
 0156            };
 157
 0158            listTypeInfo.EnsureConfigured();
 0159            elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo = listTypeInfo;
 0160            return listTypeInfo;
 0161        }
 162
 163        private static JsonTypeInfo<List<T?>> GetOrAddListTypeInfoForArrayMode<T>(JsonTypeInfo<T> elementTypeInfo)
 0164        {
 0165            if (elementTypeInfo._asyncEnumerableArrayTypeInfo != null)
 0166            {
 0167                return (JsonTypeInfo<List<T?>>)elementTypeInfo._asyncEnumerableArrayTypeInfo;
 168            }
 169
 0170            var converter = new ListOfTConverter<List<T>, T>();
 0171            var listTypeInfo = new JsonTypeInfo<List<T?>>(converter, elementTypeInfo.Options)
 0172            {
 0173                CreateObject = static () => new List<T?>(),
 0174                ElementTypeInfo = elementTypeInfo,
 0175            };
 176
 0177            listTypeInfo.EnsureConfigured();
 0178            elementTypeInfo._asyncEnumerableArrayTypeInfo = listTypeInfo;
 0179            return listTypeInfo;
 0180        }
 181    }
 182}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Document.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.Text.Json.Serialization;
 6using System.Text.Json.Serialization.Metadata;
 7
 8namespace System.Text.Json
 9{
 10    public static partial class JsonSerializer
 11    {
 12        /// <summary>
 13        /// Converts the <see cref="JsonDocument"/> representing a single JSON value into a <typeparamref name="TValue"/
 14        /// </summary>
 15        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 16        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 17        /// <param name="document">The <see cref="JsonDocument"/> to convert.</param>
 18        /// <param name="options">Options to control the behavior during parsing.</param>
 19        /// <exception cref="System.ArgumentNullException">
 20        /// <paramref name="document"/> is <see langword="null"/>.
 21        /// </exception>
 22        /// <exception cref="JsonException">
 23        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 24        /// </exception>
 25        /// <exception cref="NotSupportedException">
 26        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 27        /// for <typeparamref name="TValue"/> or its serializable members.
 28        /// </exception>
 29        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 30        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 31        public static TValue? Deserialize<TValue>(this JsonDocument document, JsonSerializerOptions? options = null)
 032        {
 033            ArgumentNullException.ThrowIfNull(document);
 34
 035            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 036            ReadOnlySpan<byte> utf8Json = document.GetRootRawValue().Span;
 037            return ReadFromSpan(utf8Json, jsonTypeInfo);
 038        }
 39
 40        /// <summary>
 41        /// Converts the <see cref="JsonDocument"/> representing a single JSON value into a <paramref name="returnType"/
 42        /// </summary>
 43        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 44        /// <param name="document">The <see cref="JsonDocument"/> to convert.</param>
 45        /// <param name="returnType">The type of the object to convert to and return.</param>
 46        /// <param name="options">Options to control the behavior during parsing.</param>
 47        /// <exception cref="System.ArgumentNullException">
 48        /// <paramref name="document"/> or <paramref name="returnType"/> is <see langword="null"/>.
 49        /// </exception>
 50        /// <exception cref="JsonException">
 51        /// <paramref name="returnType"/> is not compatible with the JSON.
 52        /// </exception>
 53        /// <exception cref="NotSupportedException">
 54        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 55        /// for <paramref name="returnType"/> or its serializable members.
 56        /// </exception>
 57        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 58        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 59        public static object? Deserialize(this JsonDocument document, Type returnType, JsonSerializerOptions? options = 
 060        {
 061            ArgumentNullException.ThrowIfNull(document);
 062            ArgumentNullException.ThrowIfNull(returnType);
 63
 064            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 065            ReadOnlySpan<byte> utf8Json = document.GetRootRawValue().Span;
 066            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 067        }
 68
 69        /// <summary>
 70        /// Converts the <see cref="JsonDocument"/> representing a single JSON value into a <typeparamref name="TValue"/
 71        /// </summary>
 72        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 73        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 74        /// <param name="document">The <see cref="JsonDocument"/> to convert.</param>
 75        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 76        /// <exception cref="System.ArgumentNullException">
 77        /// <paramref name="document"/> is <see langword="null"/>.
 78        ///
 79        /// -or-
 80        ///
 81        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 82        /// </exception>
 83        /// <exception cref="JsonException">
 84        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 85        /// </exception>
 86        public static TValue? Deserialize<TValue>(this JsonDocument document, JsonTypeInfo<TValue> jsonTypeInfo)
 087        {
 088            ArgumentNullException.ThrowIfNull(document);
 089            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 90
 091            jsonTypeInfo.EnsureConfigured();
 092            ReadOnlySpan<byte> utf8Json = document.GetRootRawValue().Span;
 093            return ReadFromSpan(utf8Json, jsonTypeInfo);
 094        }
 95
 96        /// <summary>
 97        /// Converts the <see cref="JsonDocument"/> representing a single JSON value into an instance specified by the <
 98        /// </summary>
 99        /// <param name="document">The <see cref="JsonDocument"/> to convert.</param>
 100        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 101        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 102        /// <exception cref="System.ArgumentNullException">
 103        /// <paramref name="document"/> is <see langword="null"/>.
 104        ///
 105        /// -or-
 106        ///
 107        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 108        /// </exception>
 109        public static object? Deserialize(this JsonDocument document, JsonTypeInfo jsonTypeInfo)
 0110        {
 0111            ArgumentNullException.ThrowIfNull(document);
 0112            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 113
 0114            jsonTypeInfo.EnsureConfigured();
 0115            ReadOnlySpan<byte> utf8Json = document.GetRootRawValue().Span;
 0116            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 0117        }
 118
 119        /// <summary>
 120        /// Converts the <see cref="JsonDocument"/> representing a single JSON value into a <paramref name="returnType"/
 121        /// </summary>
 122        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 123        /// <param name="document">The <see cref="JsonDocument"/> to convert.</param>
 124        /// <param name="returnType">The type of the object to convert to and return.</param>
 125        /// <param name="context">A metadata provider for serializable types.</param>
 126        /// <exception cref="System.ArgumentNullException">
 127        /// <paramref name="document"/> is <see langword="null"/>.
 128        ///
 129        /// -or-
 130        ///
 131        /// <paramref name="returnType"/> is <see langword="null"/>.
 132        ///
 133        /// -or-
 134        ///
 135        /// <paramref name="context"/> is <see langword="null"/>.
 136        /// </exception>
 137        /// <exception cref="JsonException">
 138        /// The JSON is invalid.
 139        ///
 140        /// -or-
 141        ///
 142        /// <paramref name="returnType" /> is not compatible with the JSON.
 143        ///
 144        /// -or-
 145        ///
 146        /// There is remaining data in the string beyond a single JSON value.</exception>
 147        /// <exception cref="NotSupportedException">
 148        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 149        /// for <paramref name="returnType"/> or its serializable members.
 150        /// </exception>
 151        /// <exception cref="InvalidOperationException">
 152        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 153        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 154        /// </exception>
 155        public static object? Deserialize(this JsonDocument document, Type returnType, JsonSerializerContext context)
 0156        {
 0157            ArgumentNullException.ThrowIfNull(document);
 0158            ArgumentNullException.ThrowIfNull(returnType);
 0159            ArgumentNullException.ThrowIfNull(context);
 160
 0161            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0162            ReadOnlySpan<byte> utf8Json = document.GetRootRawValue().Span;
 0163            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 0164        }
 165    }
 166}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Element.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.Text.Json.Serialization;
 6using System.Text.Json.Serialization.Metadata;
 7
 8namespace System.Text.Json
 9{
 10    public static partial class JsonSerializer
 11    {
 12        /// <summary>
 13        /// Converts the <see cref="JsonElement"/> representing a single JSON value into a <typeparamref name="TValue"/>
 14        /// </summary>
 15        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 16        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 17        /// <param name="element">The <see cref="JsonElement"/> to convert.</param>
 18        /// <param name="options">Options to control the behavior during parsing.</param>
 19        /// <exception cref="JsonException">
 20        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 21        /// </exception>
 22        /// <exception cref="NotSupportedException">
 23        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 24        /// for <typeparamref name="TValue"/> or its serializable members.
 25        /// </exception>
 26        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 27        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 28        public static TValue? Deserialize<TValue>(this JsonElement element, JsonSerializerOptions? options = null)
 029        {
 030            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 031            ReadOnlySpan<byte> utf8Json = element.GetRawValue().Span;
 032            return ReadFromSpan(utf8Json, jsonTypeInfo);
 033        }
 34
 35        /// <summary>
 36        /// Converts the <see cref="JsonElement"/> representing a single JSON value into a <paramref name="returnType"/>
 37        /// </summary>
 38        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 39        /// <param name="element">The <see cref="JsonElement"/> to convert.</param>
 40        /// <param name="returnType">The type of the object to convert to and return.</param>
 41        /// <param name="options">Options to control the behavior during parsing.</param>
 42        /// <exception cref="System.ArgumentNullException">
 43        /// <paramref name="returnType"/> is <see langword="null"/>.
 44        /// </exception>
 45        /// <exception cref="JsonException">
 46        /// <paramref name="returnType"/> is not compatible with the JSON.
 47        /// </exception>
 48        /// <exception cref="NotSupportedException">
 49        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 50        /// for <paramref name="returnType"/> or its serializable members.
 51        /// </exception>
 52        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 53        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 54        public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerOptions? options = nu
 055        {
 056            ArgumentNullException.ThrowIfNull(returnType);
 57
 058            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 059            ReadOnlySpan<byte> utf8Json = element.GetRawValue().Span;
 060            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 061        }
 62
 63        /// <summary>
 64        /// Converts the <see cref="JsonElement"/> representing a single JSON value into a <typeparamref name="TValue"/>
 65        /// </summary>
 66        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 67        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 68        /// <param name="element">The <see cref="JsonElement"/> to convert.</param>
 69        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 70        /// <exception cref="System.ArgumentNullException">
 71        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 72        /// </exception>
 73        /// <exception cref="JsonException">
 74        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 75        /// </exception>
 76        /// <exception cref="NotSupportedException">
 77        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 78        /// for <typeparamref name="TValue"/> or its serializable members.
 79        /// </exception>
 80        public static TValue? Deserialize<TValue>(this JsonElement element, JsonTypeInfo<TValue> jsonTypeInfo)
 081        {
 082            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 83
 084            jsonTypeInfo.EnsureConfigured();
 085            ReadOnlySpan<byte> utf8Json = element.GetRawValue().Span;
 086            return ReadFromSpan(utf8Json, jsonTypeInfo);
 087        }
 88
 89        /// <summary>
 90        /// Converts the <see cref="JsonElement"/> representing a single JSON value into an instance specified by the <p
 91        /// </summary>
 92        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 93        /// <param name="element">The <see cref="JsonElement"/> to convert.</param>
 94        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 95        /// <exception cref="System.ArgumentNullException">
 96        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 97        /// </exception>
 98        public static object? Deserialize(this JsonElement element, JsonTypeInfo jsonTypeInfo)
 099        {
 0100            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 101
 0102            jsonTypeInfo.EnsureConfigured();
 0103            ReadOnlySpan<byte> utf8Json = element.GetRawValue().Span;
 0104            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 0105        }
 106
 107        /// <summary>
 108        /// Converts the <see cref="JsonElement"/> representing a single JSON value into a <paramref name="returnType"/>
 109        /// </summary>
 110        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 111        /// <param name="element">The <see cref="JsonElement"/> to convert.</param>
 112        /// <param name="returnType">The type of the object to convert to and return.</param>
 113        /// <param name="context">A metadata provider for serializable types.</param>
 114        /// <exception cref="System.ArgumentNullException">
 115        /// <paramref name="returnType"/> is <see langword="null"/>.
 116        ///
 117        /// -or-
 118        ///
 119        /// <paramref name="context"/> is <see langword="null"/>.
 120        /// </exception>
 121        /// <exception cref="JsonException">
 122        /// The JSON is invalid.
 123        ///
 124        /// -or-
 125        ///
 126        /// <paramref name="returnType" /> is not compatible with the JSON.
 127        ///
 128        /// -or-
 129        ///
 130        /// There is remaining data in the string beyond a single JSON value.</exception>
 131        /// <exception cref="NotSupportedException">
 132        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 133        /// for <paramref name="returnType"/> or its serializable members.
 134        /// </exception>
 135        /// <exception cref="InvalidOperationException">
 136        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 137        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 138        /// </exception>
 139        public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerContext context)
 0140        {
 0141            ArgumentNullException.ThrowIfNull(returnType);
 0142            ArgumentNullException.ThrowIfNull(context);
 143
 0144            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0145            ReadOnlySpan<byte> utf8Json = element.GetRawValue().Span;
 0146            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 0147        }
 148    }
 149}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandleMetadata.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Text.Json.Nodes;
 8using System.Text.Json.Serialization;
 9using System.Text.Json.Serialization.Metadata;
 10
 11namespace System.Text.Json
 12{
 13    public static partial class JsonSerializer
 14    {
 15        internal const string IdPropertyName = "$id";
 16        internal const string RefPropertyName = "$ref";
 17        internal const string TypePropertyName = "$type";
 18        internal const string ValuesPropertyName = "$values";
 19
 120        private static readonly byte[] s_idPropertyName = "$id"u8.ToArray();
 121        private static readonly byte[] s_refPropertyName = "$ref"u8.ToArray();
 122        private static readonly byte[] s_typePropertyName = "$type"u8.ToArray();
 123        private static readonly byte[] s_valuesPropertyName = "$values"u8.ToArray();
 24
 25        internal static bool TryReadMetadata(JsonConverter converter, JsonTypeInfo jsonTypeInfo, ref Utf8JsonReader read
 026        {
 027            Debug.Assert(state.Current.ObjectState == StackFrameObjectState.StartToken);
 028            Debug.Assert(state.Current.CanContainMetadata);
 29
 30            Utf8JsonReader checkpoint;
 031            bool allowOutOfOrderMetadata = jsonTypeInfo.Options.AllowOutOfOrderMetadataProperties;
 032            bool isReadingAheadOfNonMetadataProperties = false;
 33
 034            if (allowOutOfOrderMetadata && !reader.IsFinalBlock)
 035            {
 36                // When reading ahead for metadata properties, we need to make sure
 37                // that the entire object has been buffered so that we can roll back
 38                // the reader state when the read-ahead has been completed.
 039                checkpoint = reader;
 040                if (!checkpoint.TrySkipPartial())
 041                {
 042                    return false;
 43                }
 044            }
 45            else
 046            {
 47                // Checkpointing not used in this mode.
 048                checkpoint = default;
 049            }
 50
 051            while (true)
 052            {
 053                if (state.Current.PropertyState == StackFramePropertyState.None)
 054                {
 55                    // Read the property name.
 056                    if (!reader.Read())
 057                    {
 058                        Debug.Assert(!allowOutOfOrderMetadata, "Object must have already been buffered in this mode.");
 059                        return false;
 60                    }
 61
 062                    state.Current.PropertyState = StackFramePropertyState.ReadName;
 063                }
 64
 065                if (state.Current.PropertyState < StackFramePropertyState.Name)
 066                {
 067                    if (reader.TokenType == JsonTokenType.EndObject)
 068                    {
 69                        // Read the entire object while parsing for metadata.
 070                        goto Done;
 71                    }
 72
 73                    // We just read a property. The only valid next tokens are EndObject and PropertyName.
 074                    Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 75
 076                    if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Ref) != 0)
 077                    {
 78                        // No properties whatsoever should follow a $ref property.
 079                        ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(reader.GetUne
 80                    }
 81
 082                    ReadOnlySpan<byte> propertyName = reader.GetUnescapedSpan();
 083                    switch (state.Current.LatestMetadataPropertyName = GetMetadataPropertyName(propertyName, jsonTypeInf
 84                    {
 85                        case MetadataPropertyName.Id:
 086                            state.Current.JsonPropertyName = s_idPropertyName;
 87
 088                            if (state.ReferenceResolver is null)
 089                            {
 90                                // Found an $id property in a type that doesn't support reference preservation
 091                                ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(propertyName, ref state);
 92                            }
 093                            if ((state.Current.MetadataPropertyNames & (MetadataPropertyName.Id | MetadataPropertyName.R
 094                            {
 95                                // No $id or $ref properties should precede $id properties.
 096                                ThrowHelper.ThrowJsonException_MetadataIdCannotBeCombinedWithRef(propertyName, ref state
 97                            }
 098                            if (!converter.CanHaveMetadata)
 099                            {
 100                                // Should not be permitted unless the converter is capable of handling metadata.
 0101                                ThrowHelper.ThrowJsonException_MetadataCannotParsePreservedObjectIntoImmutable(converter
 102                            }
 103
 0104                            break;
 105
 106                        case MetadataPropertyName.Ref:
 0107                            state.Current.JsonPropertyName = s_refPropertyName;
 108
 0109                            if (state.ReferenceResolver is null)
 0110                            {
 111                                // Found a $ref property in a type that doesn't support reference preservation
 0112                                ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(propertyName, ref state);
 113                            }
 0114                            if (converter.IsValueType)
 0115                            {
 116                                // Should not be permitted if the converter is a struct.
 0117                                ThrowHelper.ThrowJsonException_MetadataInvalidReferenceToValueType(converter.Type!);
 118                            }
 0119                            if (state.Current.MetadataPropertyNames != 0 || isReadingAheadOfNonMetadataProperties)
 0120                            {
 121                                // No properties should precede a $ref property.
 0122                                ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(reade
 123                            }
 124
 0125                            break;
 126
 127                        case MetadataPropertyName.Type:
 0128                            state.Current.JsonPropertyName = jsonTypeInfo.PolymorphicTypeResolver?.CustomTypeDiscriminat
 129
 0130                            if (jsonTypeInfo.PolymorphicTypeResolver is null)
 0131                            {
 132                                // Found a $type property in a type that doesn't support polymorphism
 0133                                ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(propertyName, ref state);
 134                            }
 0135                            if (state.PolymorphicTypeDiscriminator != null)
 0136                            {
 137                                // Found a duplicate $type property.
 0138                                ThrowHelper.ThrowJsonException_DuplicateMetadataProperty(state.Current.JsonPropertyName)
 139                            }
 140
 0141                            break;
 142
 143                        case MetadataPropertyName.Values:
 0144                            state.Current.JsonPropertyName = s_valuesPropertyName;
 145
 0146                            if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0)
 0147                            {
 148                                // Found a duplicate $values property.
 0149                                ThrowHelper.ThrowJsonException_DuplicateMetadataProperty(s_valuesPropertyName);
 150                            }
 151
 0152                            if (isReadingAheadOfNonMetadataProperties)
 0153                            {
 154                                // Cannot combine a $values property with other non-metadata properties.
 0155                                ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, jsonTyp
 156                            }
 157
 0158                            break;
 159
 160                        default:
 0161                            Debug.Assert(state.Current.LatestMetadataPropertyName == MetadataPropertyName.None);
 162
 163                            // Encountered a non-metadata property
 0164                            if (allowOutOfOrderMetadata)
 0165                            {
 0166                                if (!isReadingAheadOfNonMetadataProperties)
 0167                                {
 168                                    // This is the first non-metadata property, checkpoint the current reader state.
 0169                                    isReadingAheadOfNonMetadataProperties = true;
 0170                                    checkpoint = reader;
 0171                                }
 172
 0173                                if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0)
 0174                                {
 175                                    // Cannot combine a $values property with other non-metadata properties.
 0176                                    ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, jso
 177                                }
 178
 0179                                if (IsMetadataPropertyName(propertyName, resolver: null))
 0180                                {
 181                                    // properties starting with '$' are reserved for metadata
 0182                                    ThrowHelper.ThrowUnexpectedMetadataException(propertyName, ref reader, ref state);
 183                                }
 184
 0185                                break;
 186                            }
 187                            else
 0188                            {
 189                                // Exit immediately if no read-ahead is enabled.
 0190                                goto Done;
 191                            }
 192                    }
 193
 0194                    state.Current.PropertyState = StackFramePropertyState.Name;
 0195                }
 196
 0197                if (state.Current.PropertyState < StackFramePropertyState.ReadValue)
 0198                {
 199                    // Read the property value.
 0200                    if (!reader.Read())
 0201                    {
 0202                        Debug.Assert(!allowOutOfOrderMetadata, "Object must have already been buffered in this mode.");
 0203                        return false;
 204                    }
 205
 0206                    state.Current.PropertyState = StackFramePropertyState.ReadValue;
 0207                }
 208
 0209                Debug.Assert(state.Current.PropertyState == StackFramePropertyState.ReadValue);
 210
 0211                switch (state.Current.LatestMetadataPropertyName)
 212                {
 213                    case MetadataPropertyName.Id:
 0214                        if (reader.TokenType != JsonTokenType.String)
 0215                        {
 0216                            ThrowHelper.ThrowJsonException_MetadataValueWasNotString(reader.TokenType);
 217                        }
 218
 0219                        if (state.ReferenceId != null)
 0220                        {
 0221                            ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_
 222                        }
 223
 0224                        state.ReferenceId = reader.GetString();
 0225                        break;
 226
 227                    case MetadataPropertyName.Ref:
 0228                        if (reader.TokenType != JsonTokenType.String)
 0229                        {
 0230                            ThrowHelper.ThrowJsonException_MetadataValueWasNotString(reader.TokenType);
 231                        }
 232
 0233                        if (state.ReferenceId != null)
 0234                        {
 0235                            ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_
 236                        }
 237
 0238                        state.ReferenceId = reader.GetString();
 0239                        break;
 240
 241                    case MetadataPropertyName.Type:
 0242                        Debug.Assert(state.PolymorphicTypeDiscriminator == null);
 243
 0244                        switch (reader.TokenType)
 245                        {
 246                            case JsonTokenType.String:
 0247                                state.PolymorphicTypeDiscriminator = reader.GetString();
 0248                                break;
 249                            case JsonTokenType.Number:
 0250                                state.PolymorphicTypeDiscriminator = reader.GetInt32();
 0251                                break;
 252                            default:
 0253                                ThrowHelper.ThrowJsonException_MetadataValueWasNotString(reader.TokenType);
 254                                break;
 255                        }
 256
 0257                        break;
 258
 259                    case MetadataPropertyName.Values:
 260
 0261                        if (reader.TokenType != JsonTokenType.StartArray)
 0262                        {
 0263                            ThrowHelper.ThrowJsonException_MetadataValuesInvalidToken(reader.TokenType);
 264                        }
 265
 0266                        if (allowOutOfOrderMetadata)
 0267                        {
 268                            // The $values property contains the collection payload,
 269                            // checkpoint the current reader and continue the read-ahead.
 0270                            Debug.Assert(!isReadingAheadOfNonMetadataProperties, "must have already verified if non-meta
 0271                            isReadingAheadOfNonMetadataProperties = true;
 0272                            checkpoint = reader;
 0273                            reader.SkipWithVerify();
 0274                        }
 275                        else
 0276                        {
 0277                            state.Current.MetadataPropertyNames |= MetadataPropertyName.Values;
 0278                            state.Current.PropertyState = StackFramePropertyState.None;
 0279                            goto Done; // "$values" property contains the nested payload, exit the metadata reader now.
 280                        }
 281
 0282                        break;
 283
 284                    default:
 0285                        Debug.Assert(state.Current.LatestMetadataPropertyName == MetadataPropertyName.None);
 0286                        Debug.Assert(allowOutOfOrderMetadata, "should only be reached if reading ahead is required.");
 0287                        reader.SkipWithVerify();
 288
 0289                        break;
 290                }
 291
 0292                state.Current.MetadataPropertyNames |= state.Current.LatestMetadataPropertyName;
 0293                state.Current.PropertyState = StackFramePropertyState.None;
 0294                state.Current.JsonPropertyName = null;
 0295            }
 296
 0297        Done:
 0298            if (state.Current.MetadataPropertyNames is MetadataPropertyName.Values)
 0299            {
 300                // Cannot have a $values property unless there are other metadata properties.
 0301                ThrowHelper.ThrowJsonException_MetadataStandaloneValuesProperty(ref state, s_valuesPropertyName);
 302            }
 303
 0304            if (isReadingAheadOfNonMetadataProperties)
 0305            {
 306                // Roll back the reader state to the first non-metadata property.
 0307                reader = checkpoint;
 0308            }
 309
 0310            return true;
 0311        }
 312
 313        internal static bool IsMetadataPropertyName(ReadOnlySpan<byte> propertyName, PolymorphicTypeResolver? resolver)
 0314        {
 0315            return
 0316                (propertyName.Length > 0 && propertyName[0] == '$') ||
 0317                (resolver?.CustomTypeDiscriminatorPropertyNameUtf8?.AsSpan().SequenceEqual(propertyName) == true);
 0318        }
 319
 320        internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan<byte> propertyName, PolymorphicTypeRes
 0321        {
 0322            if (propertyName.Length > 0 && propertyName[0] == '$')
 0323            {
 0324                switch (propertyName.Length)
 325                {
 0326                    case 3 when propertyName.SequenceEqual("$id"u8):
 0327                        return MetadataPropertyName.Id;
 328
 0329                    case 4 when propertyName.SequenceEqual("$ref"u8):
 0330                        return MetadataPropertyName.Ref;
 331
 0332                    case 5 when resolver?.CustomTypeDiscriminatorPropertyNameUtf8 is null && propertyName.SequenceEqual(
 0333                        return MetadataPropertyName.Type;
 334
 0335                    case 7 when propertyName.SequenceEqual("$values"u8):
 0336                        return MetadataPropertyName.Values;
 337                }
 0338            }
 339
 0340            if (resolver?.CustomTypeDiscriminatorPropertyNameUtf8 is byte[] customTypeDiscriminator &&
 0341                propertyName.SequenceEqual(customTypeDiscriminator))
 0342            {
 0343                return MetadataPropertyName.Type;
 344            }
 345
 0346            return MetadataPropertyName.None;
 0347        }
 348
 349        internal static bool TryHandleReferenceFromJsonElement(
 350            ref Utf8JsonReader reader,
 351            scoped ref ReadStack state,
 352            JsonElement element,
 353            [NotNullWhen(true)] out object? referenceValue)
 0354        {
 0355            bool refMetadataFound = false;
 0356            referenceValue = default;
 357
 0358            if (element.ValueKind == JsonValueKind.Object)
 0359            {
 0360                int propertyCount = 0;
 0361                foreach (JsonProperty property in element.EnumerateObject())
 0362                {
 0363                    propertyCount++;
 0364                    if (refMetadataFound)
 0365                    {
 366                        // There are more properties in an object with $ref.
 0367                        ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties();
 368                    }
 0369                    else if (property.EscapedNameEquals(s_idPropertyName))
 0370                    {
 0371                        if (state.ReferenceId != null)
 0372                        {
 0373                            ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_
 374                        }
 375
 0376                        if (property.Value.ValueKind != JsonValueKind.String)
 0377                        {
 0378                            ThrowHelper.ThrowJsonException_MetadataValueWasNotString(property.Value.ValueKind);
 379                        }
 380
 0381                        object boxedElement = element;
 0382                        state.ReferenceResolver.AddReference(property.Value.GetString()!, boxedElement);
 0383                        referenceValue = boxedElement;
 0384                        return true;
 385                    }
 0386                    else if (property.EscapedNameEquals(s_refPropertyName))
 0387                    {
 0388                        if (state.ReferenceId != null)
 0389                        {
 0390                            ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_
 391                        }
 392
 0393                        if (propertyCount > 1)
 0394                        {
 395                            // $ref was found but there were other properties before.
 0396                            ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties();
 397                        }
 398
 0399                        if (property.Value.ValueKind != JsonValueKind.String)
 0400                        {
 0401                            ThrowHelper.ThrowJsonException_MetadataValueWasNotString(property.Value.ValueKind);
 402                        }
 403
 0404                        referenceValue = state.ReferenceResolver.ResolveReference(property.Value.GetString()!);
 0405                        refMetadataFound = true;
 0406                    }
 0407                }
 0408            }
 409
 0410            return refMetadataFound;
 0411        }
 412
 413        internal static bool TryHandleReferenceFromJsonNode(
 414            ref Utf8JsonReader reader,
 415            scoped ref ReadStack state,
 416            JsonNode? jsonNode,
 417            [NotNullWhen(true)] out object? referenceValue)
 0418        {
 0419            bool refMetadataFound = false;
 0420            referenceValue = default;
 421
 0422            if (jsonNode is JsonObject jsonObject)
 0423            {
 0424                int propertyCount = 0;
 0425                foreach (KeyValuePair<string, JsonNode?> property in jsonObject)
 0426                {
 0427                    propertyCount++;
 0428                    if (refMetadataFound)
 0429                    {
 430                        // There are more properties in an object with $ref.
 0431                        ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties();
 432                    }
 0433                    else if (property.Key == "$id")
 0434                    {
 0435                        if (state.ReferenceId != null)
 0436                        {
 0437                            ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_
 438                        }
 439
 0440                        string referenceId = ReadAsStringMetadataValue(property.Value);
 0441                        state.ReferenceResolver.AddReference(referenceId, jsonNode);
 0442                        referenceValue = jsonNode;
 0443                        return true;
 444                    }
 0445                    else if (property.Key == "$ref")
 0446                    {
 0447                        if (state.ReferenceId != null)
 0448                        {
 0449                            ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_
 450                        }
 451
 0452                        if (propertyCount > 1)
 0453                        {
 454                            // $ref was found but there were other properties before.
 0455                            ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties();
 456                        }
 457
 0458                        string referenceId = ReadAsStringMetadataValue(property.Value);
 0459                        referenceValue = state.ReferenceResolver.ResolveReference(referenceId);
 0460                        refMetadataFound = true;
 0461                    }
 462
 463                    static string ReadAsStringMetadataValue(JsonNode? jsonNode)
 0464                    {
 0465                        if (jsonNode is JsonValue jsonValue &&
 0466                            jsonValue.TryGetValue(out string? value) &&
 0467                            value is not null)
 0468                        {
 0469                            return value;
 470                        }
 471
 0472                        JsonValueKind metadataValueKind = jsonNode?.GetValueKind() ?? JsonValueKind.Null;
 0473                        Debug.Assert(metadataValueKind != JsonValueKind.Undefined);
 0474                        ThrowHelper.ThrowJsonException_MetadataValueWasNotString(metadataValueKind);
 475                        return null!;
 0476                    }
 0477                }
 0478            }
 479
 0480            return refMetadataFound;
 0481        }
 482
 483        internal static void ValidateMetadataForObjectConverter(ref ReadStack state)
 0484        {
 0485            if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0)
 0486            {
 487                // Object converters do not support $values metadata.
 0488                ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(s_valuesPropertyName, ref state);
 489            }
 0490        }
 491
 492        internal static void ValidateMetadataForArrayConverter(JsonConverter converter, ref Utf8JsonReader reader, scope
 0493        {
 0494            switch (reader.TokenType)
 495            {
 496                case JsonTokenType.StartArray:
 0497                    Debug.Assert(state.Current.MetadataPropertyNames is MetadataPropertyName.None || state.Current.Metad
 0498                    break;
 499
 500                case JsonTokenType.EndObject:
 0501                    if (state.Current.MetadataPropertyNames != MetadataPropertyName.Ref)
 0502                    {
 503                        // Read the entire JSON object while parsing for metadata: for collection converters this is onl
 0504                        ThrowHelper.ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref state, converter.Type!);
 505                    }
 0506                    break;
 507
 508                default:
 0509                    Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 510                    // Do not tolerate non-metadata properties in collection converters.
 0511                    ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, converter.Type!, re
 512                    break;
 513            }
 0514        }
 515
 516        internal static T ResolveReferenceId<T>(ref ReadStack state)
 0517        {
 0518            Debug.Assert(!typeof(T).IsValueType);
 0519            Debug.Assert(state.ReferenceId != null);
 520
 0521            string referenceId = state.ReferenceId;
 0522            object value = state.ReferenceResolver.ResolveReference(referenceId);
 0523            state.ReferenceId = null;
 524
 525            try
 0526            {
 0527                return (T)value;
 528            }
 0529            catch (InvalidCastException)
 0530            {
 0531                ThrowHelper.ThrowInvalidOperationException_MetadataReferenceOfTypeCannotBeAssignedToType(
 0532                    referenceId, value.GetType(), typeof(T));
 533                return default!;
 534            }
 0535        }
 536    }
 537}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandlePropertyName.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Runtime.CompilerServices;
 7using System.Text.Json.Reflection;
 8using System.Text.Json.Serialization;
 9using System.Text.Json.Serialization.Metadata;
 10
 11namespace System.Text.Json
 12{
 13    public static partial class JsonSerializer
 14    {
 15        /// <summary>
 16        /// Lookup the property given its name (obtained from the reader) and return it.
 17        /// Also sets state.Current.JsonPropertyInfo to a non-null value.
 18        /// </summary>
 19        internal static JsonPropertyInfo LookupProperty(
 20            object? obj,
 21            ReadOnlySpan<byte> unescapedPropertyName,
 22            ref ReadStack state,
 23            JsonSerializerOptions options,
 24            out bool useExtensionProperty,
 25            bool createExtensionProperty = true)
 026        {
 027            JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;
 028            useExtensionProperty = false;
 29
 030            JsonPropertyInfo? jsonPropertyInfo = jsonTypeInfo.GetProperty(
 031                unescapedPropertyName,
 032                ref state.Current,
 033                out byte[] utf8PropertyName);
 34
 35            // Increment PropertyIndex so GetProperty() checks the next property first when called again.
 036            state.Current.PropertyIndex++;
 37
 38            // For case insensitive and missing property support of JsonPath, remember the value on the temporary stack.
 039            state.Current.JsonPropertyName = utf8PropertyName;
 40
 41            // Handle missing properties
 042            if (jsonPropertyInfo is null)
 043            {
 044                if (jsonTypeInfo.EffectiveUnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
 045                {
 046                    Debug.Assert(jsonTypeInfo.ExtensionDataProperty is null, "jsonTypeInfo.Configure() should have caugh
 047                    string stringPropertyName = Encoding.UTF8.GetString(unescapedPropertyName);
 048                    ThrowHelper.ThrowJsonException_UnmappedJsonProperty(jsonTypeInfo.Type, stringPropertyName);
 49                }
 50
 51                // Determine if we should use the extension property.
 052                if (jsonTypeInfo.ExtensionDataProperty is JsonPropertyInfo { HasGetter: true, HasSetter: true } dataExtP
 053                {
 054                    state.Current.JsonPropertyNameAsString = Encoding.UTF8.GetString(unescapedPropertyName);
 55
 056                    if (createExtensionProperty)
 057                    {
 058                        Debug.Assert(obj != null, "obj is null");
 059                        CreateExtensionDataProperty(obj, dataExtProperty, options);
 060                    }
 61
 062                    jsonPropertyInfo = dataExtProperty;
 063                    useExtensionProperty = true;
 064                }
 65                else
 066                {
 67                    // Populate with a placeholder value required by JsonPath calculations
 068                    jsonPropertyInfo = JsonPropertyInfo.s_missingProperty;
 069                }
 070            }
 71
 072            state.Current.JsonPropertyInfo = jsonPropertyInfo;
 073            state.Current.NumberHandling = jsonPropertyInfo.EffectiveNumberHandling;
 074            return jsonPropertyInfo;
 075        }
 76
 77        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 78        internal static ReadOnlySpan<byte> GetPropertyName(
 79            scoped ref ReadStack state,
 80            ref Utf8JsonReader reader,
 81            JsonSerializerOptions options,
 82            out bool isAlreadyReadMetadataProperty)
 083        {
 084            ReadOnlySpan<byte> propertyName = reader.GetUnescapedSpan();
 085            isAlreadyReadMetadataProperty = false;
 86
 087            if (state.Current.CanContainMetadata)
 088            {
 089                if (IsMetadataPropertyName(propertyName, state.Current.BaseJsonTypeInfo.PolymorphicTypeResolver))
 090                {
 091                    if (options.AllowOutOfOrderMetadataProperties)
 092                    {
 093                        isAlreadyReadMetadataProperty = true;
 094                    }
 95                    else
 096                    {
 097                        ThrowHelper.ThrowUnexpectedMetadataException(propertyName, ref reader, ref state);
 98                    }
 099                }
 0100            }
 101
 0102            return propertyName;
 0103        }
 104
 105        internal static void CreateExtensionDataProperty(
 106            object obj,
 107            JsonPropertyInfo jsonPropertyInfo,
 108            JsonSerializerOptions options)
 0109        {
 0110            Debug.Assert(jsonPropertyInfo != null);
 111
 0112            object? extensionData = jsonPropertyInfo.GetValueAsObject(obj);
 113
 114            // For IReadOnlyDictionary, if there's an existing non-null instance, we need to create a new mutable
 115            // Dictionary seeded with the existing contents so we can add the deserialized extension data to it.
 0116            bool isReadOnlyDictionary = jsonPropertyInfo.PropertyType == typeof(IReadOnlyDictionary<string, object>) ||
 0117                                        jsonPropertyInfo.PropertyType == typeof(IReadOnlyDictionary<string, JsonElement>
 118
 0119            if (extensionData == null || (isReadOnlyDictionary && extensionData != null))
 0120            {
 121                // Create the appropriate dictionary type. We already verified the types.
 122#if DEBUG
 0123                Type? underlyingIDictionaryType = jsonPropertyInfo.PropertyType.GetCompatibleGenericInterface(typeof(IDi
 0124                    ?? jsonPropertyInfo.PropertyType.GetCompatibleGenericInterface(typeof(IReadOnlyDictionary<,>));
 0125                Debug.Assert(underlyingIDictionaryType is not null);
 0126                Type[] genericArgs = underlyingIDictionaryType.GetGenericArguments();
 127
 0128                Debug.Assert(underlyingIDictionaryType.IsGenericType);
 0129                Debug.Assert(genericArgs.Length == 2);
 0130                Debug.Assert(genericArgs[0].UnderlyingSystemType == typeof(string));
 0131                Debug.Assert(
 0132                    genericArgs[1].UnderlyingSystemType == JsonTypeInfo.ObjectType ||
 0133                    genericArgs[1].UnderlyingSystemType == typeof(JsonElement) ||
 0134                    genericArgs[1].UnderlyingSystemType == typeof(Nodes.JsonNode));
 135#endif
 136
 0137                Func<object>? createObjectForExtensionDataProp = jsonPropertyInfo.JsonTypeInfo.CreateObject
 0138                    ?? jsonPropertyInfo.JsonTypeInfo.CreateObjectForExtensionDataProperty;
 139
 0140                if (createObjectForExtensionDataProp == null)
 0141                {
 142                    // Avoid a reference to the JsonNode type for trimming
 0143                    if (jsonPropertyInfo.PropertyType.FullName == JsonTypeInfo.JsonObjectTypeName)
 0144                    {
 0145                        ThrowHelper.ThrowInvalidOperationException_NodeJsonObjectCustomConverterNotAllowedOnExtensionPro
 146                    }
 147                    // For IReadOnlyDictionary<string, object> or IReadOnlyDictionary<string, JsonElement> interface typ
 148                    // create a Dictionary<TKey, TValue> instance seeded with any existing contents.
 0149                    else if (jsonPropertyInfo.PropertyType == typeof(IReadOnlyDictionary<string, object>))
 0150                    {
 0151                        if (extensionData != null)
 0152                        {
 0153                            var existing = (IReadOnlyDictionary<string, object>)extensionData;
 0154                            var newDict = new Dictionary<string, object>();
 0155                            foreach (KeyValuePair<string, object> kvp in existing)
 0156                            {
 0157                                newDict[kvp.Key] = kvp.Value;
 0158                            }
 0159                            extensionData = newDict;
 0160                        }
 161                        else
 0162                        {
 0163                            extensionData = new Dictionary<string, object>();
 0164                        }
 0165                        Debug.Assert(jsonPropertyInfo.Set != null);
 0166                        jsonPropertyInfo.Set(obj, extensionData);
 0167                        return;
 168                    }
 0169                    else if (jsonPropertyInfo.PropertyType == typeof(IReadOnlyDictionary<string, JsonElement>))
 0170                    {
 0171                        if (extensionData != null)
 0172                        {
 0173                            var existing = (IReadOnlyDictionary<string, JsonElement>)extensionData;
 0174                            var newDict = new Dictionary<string, JsonElement>();
 0175                            foreach (KeyValuePair<string, JsonElement> kvp in existing)
 0176                            {
 0177                                newDict[kvp.Key] = kvp.Value;
 0178                            }
 0179                            extensionData = newDict;
 0180                        }
 181                        else
 0182                        {
 0183                            extensionData = new Dictionary<string, JsonElement>();
 0184                        }
 0185                        Debug.Assert(jsonPropertyInfo.Set != null);
 0186                        jsonPropertyInfo.Set(obj, extensionData);
 0187                        return;
 188                    }
 189                    else
 0190                    {
 0191                        ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(jsonPropertyInfo.PropertyType);
 192                    }
 193                }
 194
 0195                extensionData = createObjectForExtensionDataProp();
 0196                Debug.Assert(jsonPropertyInfo.Set != null);
 0197                jsonPropertyInfo.Set(obj, extensionData);
 0198            }
 199
 200            // We don't add the value to the dictionary here because we need to support the read-ahead functionality for
 0201        }
 202    }
 203}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Node.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Nodes;
 7using System.Text.Json.Serialization;
 8using System.Text.Json.Serialization.Metadata;
 9
 10namespace System.Text.Json
 11{
 12    public static partial class JsonSerializer
 13    {
 14        /// <summary>
 15        /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <typeparamref name="TValue"/>.
 16        /// </summary>
 17        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 18        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 19        /// <param name="node">The <see cref="JsonNode"/> to convert.</param>
 20        /// <param name="options">Options to control the behavior during parsing.</param>
 21        /// <exception cref="JsonException">
 22        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 23        /// </exception>
 24        /// <exception cref="NotSupportedException">
 25        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 26        /// for <typeparamref name="TValue"/> or its serializable members.
 27        /// </exception>
 28        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 29        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 30        public static TValue? Deserialize<TValue>(this JsonNode? node, JsonSerializerOptions? options = null)
 031        {
 032            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 033            return ReadFromNode(node, jsonTypeInfo);
 034        }
 35
 36        /// <summary>
 37        /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <paramref name="returnType"/>.
 38        /// </summary>
 39        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 40        /// <param name="node">The <see cref="JsonNode"/> to convert.</param>
 41        /// <param name="returnType">The type of the object to convert to and return.</param>
 42        /// <param name="options">Options to control the behavior during parsing.</param>
 43        /// <exception cref="JsonException">
 44        /// <paramref name="returnType"/> is not compatible with the JSON.
 45        /// </exception>
 46        /// <exception cref="NotSupportedException">
 47        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 48        /// for <paramref name="returnType"/> or its serializable members.
 49        /// </exception>
 50        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 51        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 52        public static object? Deserialize(this JsonNode? node, Type returnType, JsonSerializerOptions? options = null)
 053        {
 054            ArgumentNullException.ThrowIfNull(returnType);
 55
 056            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 057            return ReadFromNodeAsObject(node, jsonTypeInfo);
 058        }
 59
 60        /// <summary>
 61        /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <typeparamref name="TValue"/>.
 62        /// </summary>
 63        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 64        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 65        /// <param name="node">The <see cref="JsonNode"/> to convert.</param>
 66        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 67        /// <exception cref="System.ArgumentNullException">
 68        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 69        /// </exception>
 70        /// <exception cref="JsonException">
 71        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 72        /// </exception>
 73        public static TValue? Deserialize<TValue>(this JsonNode? node, JsonTypeInfo<TValue> jsonTypeInfo)
 074        {
 075            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 76
 077            jsonTypeInfo.EnsureConfigured();
 078            return ReadFromNode(node, jsonTypeInfo);
 079        }
 80
 81        /// <summary>
 82        /// Converts the <see cref="JsonNode"/> representing a single JSON value into an instance specified by the <para
 83        /// </summary>
 84        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 85        /// <param name="node">The <see cref="JsonNode"/> to convert.</param>
 86        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 87        /// <exception cref="System.ArgumentNullException">
 88        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 89        /// </exception>
 90        public static object? Deserialize(this JsonNode? node, JsonTypeInfo jsonTypeInfo)
 091        {
 092            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 93
 094            jsonTypeInfo.EnsureConfigured();
 095            return ReadFromNodeAsObject(node, jsonTypeInfo);
 096        }
 97
 98        /// <summary>
 99        /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <paramref name="returnType"/>.
 100        /// </summary>
 101        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 102        /// <param name="node">The <see cref="JsonNode"/> to convert.</param>
 103        /// <param name="returnType">The type of the object to convert to and return.</param>
 104        /// <param name="context">A metadata provider for serializable types.</param>
 105        /// <exception cref="System.ArgumentNullException">
 106        /// <paramref name="returnType"/> is <see langword="null"/>.
 107        ///
 108        /// -or-
 109        ///
 110        /// <paramref name="context"/> is <see langword="null"/>.
 111        /// </exception>
 112        /// <exception cref="JsonException">
 113        /// The JSON is invalid.
 114        ///
 115        /// -or-
 116        ///
 117        /// <paramref name="returnType" /> is not compatible with the JSON.
 118        ///
 119        /// -or-
 120        ///
 121        /// There is remaining data in the string beyond a single JSON value.</exception>
 122        /// <exception cref="NotSupportedException">
 123        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 124        /// for <paramref name="returnType"/> or its serializable members.
 125        /// </exception>
 126        /// <exception cref="InvalidOperationException">
 127        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 128        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 129        /// </exception>
 130        public static object? Deserialize(this JsonNode? node, Type returnType, JsonSerializerContext context)
 0131        {
 0132            ArgumentNullException.ThrowIfNull(returnType);
 0133            ArgumentNullException.ThrowIfNull(context);
 134
 0135            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0136            return ReadFromNodeAsObject(node, jsonTypeInfo);
 0137        }
 138
 139        private static TValue? ReadFromNode<TValue>(JsonNode? node, JsonTypeInfo<TValue> jsonTypeInfo)
 0140        {
 0141            JsonSerializerOptions options = jsonTypeInfo.Options;
 142
 143            // For performance, share the same buffer across serialization and deserialization.
 0144            using var output = new PooledByteBufferWriter(options.DefaultBufferSize);
 0145            using (var writer = new Utf8JsonWriter(output, options.GetWriterOptions()))
 0146            {
 0147                if (node is null)
 0148                {
 0149                    writer.WriteNullValue();
 0150                }
 151                else
 0152                {
 0153                    node.WriteTo(writer, options);
 0154                }
 0155            }
 156
 0157            return ReadFromSpan(output.WrittenSpan, jsonTypeInfo);
 0158        }
 159
 160        private static object? ReadFromNodeAsObject(JsonNode? node, JsonTypeInfo jsonTypeInfo)
 0161        {
 0162            JsonSerializerOptions options = jsonTypeInfo.Options;
 163
 164            // For performance, share the same buffer across serialization and deserialization.
 0165            using var output = new PooledByteBufferWriter(options.DefaultBufferSize);
 0166            using (var writer = new Utf8JsonWriter(output, options.GetWriterOptions()))
 0167            {
 0168                if (node is null)
 0169                {
 0170                    writer.WriteNullValue();
 0171                }
 172                else
 0173                {
 0174                    node.WriteTo(writer, options);
 0175                }
 0176            }
 177
 0178            return ReadFromSpanAsObject(output.WrittenSpan, jsonTypeInfo);
 0179        }
 180    }
 181}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Pipe.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.IO.Pipelines;
 8using System.Runtime.CompilerServices;
 9using System.Text.Json.Serialization;
 10using System.Text.Json.Serialization.Metadata;
 11using System.Threading;
 12using System.Threading.Tasks;
 13
 14namespace System.Text.Json
 15{
 16    public static partial class JsonSerializer
 17    {
 18        /// <summary>
 19        /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 20        /// The PipeReader will be read to completion.
 21        /// </summary>
 22        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 23        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 24        /// <param name="utf8Json">JSON data to parse.</param>
 25        /// <param name="options">Options to control the behavior during reading.</param>
 26        /// <param name="cancellationToken">
 27        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 28        /// </param>
 29        /// <exception cref="System.ArgumentNullException">
 30        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 31        /// </exception>
 32        /// <exception cref="JsonException">
 33        /// The JSON is invalid,
 34        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 35        /// or when there is remaining data in the PipeReader.
 36        /// </exception>
 37        /// <exception cref="NotSupportedException">
 38        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 39        /// for <typeparamref name="TValue"/> or its serializable members.
 40        /// </exception>
 41        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 42        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 43        public static ValueTask<TValue?> DeserializeAsync<TValue>(
 44            PipeReader utf8Json,
 45            JsonSerializerOptions? options = null,
 46            CancellationToken cancellationToken = default)
 047        {
 048            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 49
 050            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 051            return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken);
 052        }
 53
 54        /// <summary>
 55        /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 56        /// The PipeReader will be read to completion.
 57        /// </summary>
 58        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 59        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 60        /// <param name="utf8Json">JSON data to parse.</param>
 61        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 62        /// <param name="cancellationToken">
 63        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 64        /// </param>
 65        /// <exception cref="System.ArgumentNullException">
 66        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 67        /// </exception>
 68        /// <exception cref="JsonException">
 69        /// The JSON is invalid,
 70        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 71        /// or when there is remaining data in the PipeReader.
 72        /// </exception>
 73        public static ValueTask<TValue?> DeserializeAsync<TValue>(
 74                PipeReader utf8Json,
 75                JsonTypeInfo<TValue> jsonTypeInfo,
 76                CancellationToken cancellationToken = default)
 077        {
 078            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 079            ArgumentNullException.ThrowIfNull(jsonTypeInfo, nameof(jsonTypeInfo));
 80
 081            jsonTypeInfo.EnsureConfigured();
 082            return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken);
 083        }
 84
 85        /// <summary>
 86        /// Reads the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref na
 87        /// The PipeReader will be read to completion.
 88        /// </summary>
 89        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 90        /// <param name="utf8Json">JSON data to parse.</param>
 91        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 92        /// <param name="cancellationToken">
 93        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 94        /// </param>
 95        /// <exception cref="System.ArgumentNullException">
 96        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 97        /// </exception>
 98        /// <exception cref="JsonException">
 99        /// The JSON is invalid,
 100        /// or when there is remaining data in the PipeReader.
 101        /// </exception>
 102        public static ValueTask<object?> DeserializeAsync(
 103                PipeReader utf8Json,
 104                JsonTypeInfo jsonTypeInfo,
 105                CancellationToken cancellationToken = default)
 0106        {
 0107            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 0108            ArgumentNullException.ThrowIfNull(jsonTypeInfo, nameof(jsonTypeInfo));
 109
 0110            jsonTypeInfo.EnsureConfigured();
 0111            return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
 0112        }
 113
 114        /// <summary>
 115        /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 116        /// The PipeReader will be read to completion.
 117        /// </summary>
 118        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 119        /// <param name="utf8Json">JSON data to parse.</param>
 120        /// <param name="returnType">The type of the object to convert to and return.</param>
 121        /// <param name="context">A metadata provider for serializable types.</param>
 122        /// <param name="cancellationToken">
 123        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 124        /// </param>
 125        /// <exception cref="System.ArgumentNullException">
 126        /// <paramref name="utf8Json"/>, <paramref name="returnType"/>, or <paramref name="context"/> is <see langword="
 127        /// </exception>
 128        /// <exception cref="JsonException">
 129        /// The JSON is invalid,
 130        /// the <paramref name="returnType"/> is not compatible with the JSON,
 131        /// or when there is remaining data in the PipeReader.
 132        /// </exception>
 133        /// <exception cref="NotSupportedException">
 134        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 135        /// for <paramref name="returnType"/> or its serializable members.
 136        /// </exception>
 137        /// <exception cref="InvalidOperationException">
 138        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/>
 139        /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>.
 140        /// </exception>
 141        public static ValueTask<object?> DeserializeAsync(
 142                PipeReader utf8Json,
 143                Type returnType,
 144                JsonSerializerContext context,
 145                CancellationToken cancellationToken = default)
 0146        {
 0147            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 0148            ArgumentNullException.ThrowIfNull(returnType, nameof(returnType));
 0149            ArgumentNullException.ThrowIfNull(context, nameof(context));
 150
 0151            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0152            return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
 0153        }
 154
 155        /// <summary>
 156        /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 157        /// The PipeReader will be read to completion.
 158        /// </summary>
 159        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 160        /// <param name="utf8Json">JSON data to parse.</param>
 161        /// <param name="returnType">The type of the object to convert to and return.</param>
 162        /// <param name="options">Options to control the behavior during reading.</param>
 163        /// <param name="cancellationToken">
 164        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 165        /// </param>
 166        /// <exception cref="System.ArgumentNullException">
 167        /// <paramref name="utf8Json"/> or <paramref name="returnType"/> is <see langword="null"/>.
 168        /// </exception>
 169        /// <exception cref="JsonException">
 170        /// The JSON is invalid,
 171        /// the <paramref name="returnType"/> is not compatible with the JSON,
 172        /// or when there is remaining data in the PipeReader.
 173        /// </exception>
 174        /// <exception cref="NotSupportedException">
 175        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 176        /// for <paramref name="returnType"/> or its serializable members.
 177        /// </exception>
 178        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 179        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 180        public static ValueTask<object?> DeserializeAsync(
 181               PipeReader utf8Json,
 182               Type returnType,
 183               JsonSerializerOptions? options = null,
 184               CancellationToken cancellationToken = default)
 27962185        {
 27962186            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 27962187            ArgumentNullException.ThrowIfNull(returnType, nameof(returnType));
 188
 27962189            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 27962190            return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
 27962191        }
 192
 193        /// <summary>
 194        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 195        /// that can be used to deserialize root-level JSON arrays in a streaming manner.
 196        /// </summary>
 197        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 198        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON array.</returns>
 199        /// <param name="utf8Json">JSON data to parse.</param>
 200        /// <param name="options">Options to control the behavior during reading.</param>
 201        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 202        /// <exception cref="System.ArgumentNullException">
 203        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 204        /// </exception>
 205        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 206        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 207        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 208                PipeReader utf8Json,
 209                JsonSerializerOptions? options = null,
 210                CancellationToken cancellationToken = default)
 0211        {
 0212            return DeserializeAsyncEnumerable<TValue>(utf8Json, topLevelValues: false, options, cancellationToken);
 0213        }
 214
 215        /// <summary>
 216        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 217        /// that can be used to deserialize root-level JSON arrays in a streaming manner.
 218        /// </summary>
 219        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 220        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON array.</returns>
 221        /// <param name="utf8Json">JSON data to parse.</param>
 222        /// <param name="jsonTypeInfo">Metadata about the element type to convert.</param>
 223        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 224        /// <exception cref="System.ArgumentNullException">
 225        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 226        /// </exception>
 227        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 228                PipeReader utf8Json,
 229                JsonTypeInfo<TValue> jsonTypeInfo,
 230                CancellationToken cancellationToken = default)
 0231        {
 0232            return DeserializeAsyncEnumerable(utf8Json, jsonTypeInfo, topLevelValues: false, cancellationToken);
 0233        }
 234
 235        /// <summary>
 236        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 237        /// that can be used to deserialize sequences of JSON values in a streaming manner.
 238        /// </summary>
 239        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 240        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON sequence.</returns>
 241        /// <param name="utf8Json">JSON data to parse.</param>
 242        /// <param name="jsonTypeInfo">Metadata about the element type to convert.</param>
 243        /// <param name="topLevelValues">Whether to deserialize from a sequence of top-level JSON values.</param>
 244        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 245        /// <exception cref="System.ArgumentNullException">
 246        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 247        /// </exception>
 248        /// <remarks>
 249        /// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the PipeReader as a sequenc
 250        /// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="T
 251        /// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the PipeReader as a JSON a
 252        /// attempts to serialize each element into <typeparamref name="TValue"/>.
 253        /// </remarks>
 254        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 255            PipeReader utf8Json,
 256            JsonTypeInfo<TValue> jsonTypeInfo,
 257            bool topLevelValues,
 258            CancellationToken cancellationToken = default)
 0259        {
 0260            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 0261            ArgumentNullException.ThrowIfNull(jsonTypeInfo, nameof(jsonTypeInfo));
 262
 0263            jsonTypeInfo.EnsureConfigured();
 0264            return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken);
 0265        }
 266
 267        /// <summary>
 268        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 269        /// that can be used to deserialize sequences of JSON values in a streaming manner.
 270        /// </summary>
 271        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 272        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON sequence.</returns>
 273        /// <param name="utf8Json">JSON data to parse.</param>
 274        /// <param name="topLevelValues"><see langword="true"/> to deserialize from a sequence of top-level JSON values,
 275        /// <param name="options">Options to control the behavior during reading.</param>
 276        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 277        /// <exception cref="System.ArgumentNullException">
 278        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 279        /// </exception>
 280        /// <remarks>
 281        /// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the PipeReader as a sequenc
 282        /// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="T
 283        /// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the PipeReader as a JSON a
 284        /// attempts to serialize each element into <typeparamref name="TValue"/>.
 285        /// </remarks>
 286        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 287        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 288        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 289            PipeReader utf8Json,
 290            bool topLevelValues,
 291            JsonSerializerOptions? options = null,
 292            CancellationToken cancellationToken = default)
 0293        {
 0294            ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
 295
 0296            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 0297            return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken);
 0298        }
 299
 300        private static IAsyncEnumerable<T?> DeserializeAsyncEnumerableCore<T>(
 301            PipeReader utf8Json,
 302            JsonTypeInfo<T> jsonTypeInfo,
 303            bool topLevelValues,
 304            CancellationToken cancellationToken)
 0305        {
 0306            Debug.Assert(jsonTypeInfo.IsConfigured);
 307
 308            JsonTypeInfo<List<T?>> listTypeInfo;
 0309            JsonReaderOptions readerOptions = jsonTypeInfo.Options.GetReaderOptions();
 0310            if (topLevelValues)
 0311            {
 0312                listTypeInfo = GetOrAddListTypeInfoForRootLevelValueMode(jsonTypeInfo);
 0313                readerOptions.AllowMultipleValues = true;
 0314            }
 315            else
 0316            {
 0317                listTypeInfo = GetOrAddListTypeInfoForArrayMode(jsonTypeInfo);
 0318            }
 319
 0320            return CreateAsyncEnumerableFromArray(utf8Json, listTypeInfo, readerOptions, cancellationToken);
 321
 322            static async IAsyncEnumerable<T?> CreateAsyncEnumerableFromArray(
 323                PipeReader utf8Json,
 324                JsonTypeInfo<List<T?>> listTypeInfo,
 325                JsonReaderOptions readerOptions,
 326                [EnumeratorCancellation] CancellationToken cancellationToken)
 0327            {
 0328                Debug.Assert(listTypeInfo.IsConfigured);
 329
 0330                ReadStack readStack = default;
 0331                readStack.Initialize(listTypeInfo, supportContinuation: true);
 0332                JsonReaderState jsonReaderState = new(readerOptions);
 0333                PipeReadBufferState bufferState = new(utf8Json);
 334
 335                try
 0336                {
 337                    bool success;
 338                    do
 0339                    {
 0340                        bufferState = await bufferState.ReadAsync(utf8Json, cancellationToken, fillBuffer: false).Config
 0341                        success = listTypeInfo.ContinueDeserialize<PipeReadBufferState, PipeReader>(
 0342                            ref bufferState,
 0343                            ref jsonReaderState,
 0344                            ref readStack,
 0345                            out List<T?>? _);
 346
 0347                        if (readStack.Current.ReturnValue is { } returnValue)
 0348                        {
 0349                            var list = (List<T?>)returnValue;
 0350                            foreach (T? item in list)
 0351                            {
 0352                                yield return item;
 0353                            }
 354
 0355                            list.Clear();
 0356                        }
 0357                    } while (!success);
 0358                }
 359                finally
 0360                {
 0361                    bufferState.Dispose();
 0362                }
 0363            }
 0364        }
 365    }
 366}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Span.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json
 10{
 11    public static partial class JsonSerializer
 12    {
 13        /// <summary>
 14        /// Parses the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 15        /// </summary>
 16        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 17        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 18        /// <param name="utf8Json">JSON text to parse.</param>
 19        /// <param name="options">Options to control the behavior during parsing.</param>
 20        /// <exception cref="JsonException">
 21        /// The JSON is invalid,
 22        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 23        /// or when there is remaining data in the Stream.
 24        /// </exception>
 25        /// <exception cref="NotSupportedException">
 26        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 27        /// for <typeparamref name="TValue"/> or its serializable members.
 28        /// </exception>
 29        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 30        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 31        public static TValue? Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerializerOptions? options = null)
 032        {
 033            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 034            return ReadFromSpan(utf8Json, jsonTypeInfo);
 035        }
 36
 37        /// <summary>
 38        /// Parses the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 39        /// </summary>
 40        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 41        /// <param name="utf8Json">JSON text to parse.</param>
 42        /// <param name="returnType">The type of the object to convert to and return.</param>
 43        /// <param name="options">Options to control the behavior during parsing.</param>
 44        /// <exception cref="System.ArgumentNullException">
 45        /// <paramref name="returnType"/> is <see langword="null"/>.
 46        /// </exception>
 47        /// <exception cref="JsonException">
 48        /// The JSON is invalid,
 49        /// <paramref name="returnType"/> is not compatible with the JSON,
 50        /// or when there is remaining data in the Stream.
 51        /// </exception>
 52        /// <exception cref="NotSupportedException">
 53        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 54        /// for <paramref name="returnType"/> or its serializable members.
 55        /// </exception>
 56        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 57        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 58        public static object? Deserialize(ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerOptions? options =
 059        {
 060            ArgumentNullException.ThrowIfNull(returnType);
 61
 062            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 063            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 064        }
 65
 66        /// <summary>
 67        /// Parses the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 68        /// </summary>
 69        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 70        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 71        /// <param name="utf8Json">JSON text to parse.</param>
 72        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 73        /// <exception cref="JsonException">
 74        /// The JSON is invalid,
 75        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 76        /// or when there is remaining data in the buffer.
 77        /// </exception>
 78        public static TValue? Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonTypeInfo<TValue> jsonTypeInfo)
 079        {
 080            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 81
 082            jsonTypeInfo.EnsureConfigured();
 083            return ReadFromSpan(utf8Json, jsonTypeInfo);
 084        }
 85
 86        /// <summary>
 87        /// Parses the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref n
 88        /// </summary>
 89        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 90        /// <param name="utf8Json">JSON text to parse.</param>
 91        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 92        /// <exception cref="JsonException">
 93        /// The JSON is invalid,
 94        /// or there is remaining data in the buffer.
 95        /// </exception>
 96        public static object? Deserialize(ReadOnlySpan<byte> utf8Json, JsonTypeInfo jsonTypeInfo)
 097        {
 098            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 99
 0100            jsonTypeInfo.EnsureConfigured();
 0101            return ReadFromSpanAsObject(utf8Json, jsonTypeInfo);
 0102        }
 103
 104        /// <summary>
 105        /// Parses the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 106        /// </summary>
 107        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 108        /// <param name="utf8Json">JSON text to parse.</param>
 109        /// <param name="returnType">The type of the object to convert to and return.</param>
 110        /// <param name="context">A metadata provider for serializable types.</param>
 111        /// <exception cref="System.ArgumentNullException">
 112        /// <paramref name="returnType"/> is <see langword="null"/>.
 113        /// </exception>
 114        /// <exception cref="JsonException">
 115        /// The JSON is invalid,
 116        /// <paramref name="returnType"/> is not compatible with the JSON,
 117        /// or when there is remaining data in the Stream.
 118        /// </exception>
 119        /// <exception cref="NotSupportedException">
 120        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 121        /// for <paramref name="returnType"/> or its serializable members.
 122        /// </exception>
 123        /// <exception cref="InvalidOperationException">
 124        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/>
 125        /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>.
 126        /// </exception>
 127        public static object? Deserialize(ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerContext context)
 0128        {
 0129            ArgumentNullException.ThrowIfNull(returnType);
 0130            ArgumentNullException.ThrowIfNull(context);
 131
 0132            return ReadFromSpanAsObject(utf8Json, GetTypeInfo(context, returnType));
 0133        }
 134
 135        private static TValue? ReadFromSpan<TValue>(ReadOnlySpan<byte> utf8Json, JsonTypeInfo<TValue> jsonTypeInfo, int?
 0136        {
 0137            Debug.Assert(jsonTypeInfo.IsConfigured);
 138
 0139            var readerState = new JsonReaderState(jsonTypeInfo.Options.GetReaderOptions());
 0140            var reader = new Utf8JsonReader(utf8Json, isFinalBlock: true, readerState);
 141
 0142            ReadStack state = default;
 0143            state.Initialize(jsonTypeInfo);
 144
 0145            TValue? value = jsonTypeInfo.Deserialize(ref reader, ref state);
 146
 147            // The reader should have thrown if we have remaining bytes, unless AllowMultipleValues is true.
 0148            Debug.Assert(reader.BytesConsumed == (actualByteCount ?? utf8Json.Length) || reader.CurrentState.Options.All
 0149            return value;
 0150        }
 151
 152        private static object? ReadFromSpanAsObject(ReadOnlySpan<byte> utf8Json, JsonTypeInfo jsonTypeInfo, int? actualB
 0153        {
 0154            Debug.Assert(jsonTypeInfo.IsConfigured);
 155
 0156            var readerState = new JsonReaderState(jsonTypeInfo.Options.GetReaderOptions());
 0157            var reader = new Utf8JsonReader(utf8Json, isFinalBlock: true, readerState);
 158
 0159            ReadStack state = default;
 0160            state.Initialize(jsonTypeInfo);
 161
 0162            object? value = jsonTypeInfo.DeserializeAsObject(ref reader, ref state);
 163
 164            // The reader should have thrown if we have remaining bytes, unless AllowMultipleValues is true.
 0165            Debug.Assert(reader.BytesConsumed == (actualByteCount ?? utf8Json.Length) || reader.CurrentState.Options.All
 0166            return value;
 0167        }
 168    }
 169}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Stream.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.IO;
 8using System.Runtime.CompilerServices;
 9using System.Text.Json.Serialization;
 10using System.Text.Json.Serialization.Metadata;
 11using System.Threading;
 12using System.Threading.Tasks;
 13
 14namespace System.Text.Json
 15{
 16    public static partial class JsonSerializer
 17    {
 18        /// <summary>
 19        /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 20        /// The Stream will be read to completion.
 21        /// </summary>
 22        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 23        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 24        /// <param name="utf8Json">JSON data to parse.</param>
 25        /// <param name="options">Options to control the behavior during reading.</param>
 26        /// <param name="cancellationToken">
 27        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 28        /// </param>
 29        /// <exception cref="System.ArgumentNullException">
 30        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 31        /// </exception>
 32        /// <exception cref="JsonException">
 33        /// The JSON is invalid,
 34        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 35        /// or when there is remaining data in the Stream.
 36        /// </exception>
 37        /// <exception cref="NotSupportedException">
 38        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 39        /// for <typeparamref name="TValue"/> or its serializable members.
 40        /// </exception>
 41        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 42        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 43        public static ValueTask<TValue?> DeserializeAsync<TValue>(
 44            Stream utf8Json,
 45            JsonSerializerOptions? options = null,
 46            CancellationToken cancellationToken = default)
 047        {
 048            ArgumentNullException.ThrowIfNull(utf8Json);
 49
 050            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 051            return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken);
 052        }
 53
 54        /// <summary>
 55        /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 56        /// The Stream will be read to completion.
 57        /// </summary>
 58        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 59        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 60        /// <param name="utf8Json">JSON data to parse.</param>
 61        /// <param name="options">Options to control the behavior during reading.</param>
 62        /// <exception cref="System.ArgumentNullException">
 63        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 64        /// </exception>
 65        /// <exception cref="JsonException">
 66        /// The JSON is invalid,
 67        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 68        /// or when there is remaining data in the Stream.
 69        /// </exception>
 70        /// <exception cref="NotSupportedException">
 71        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 72        /// for <typeparamref name="TValue"/> or its serializable members.
 73        /// </exception>
 74        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 75        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 76        public static TValue? Deserialize<TValue>(
 77            Stream utf8Json,
 78            JsonSerializerOptions? options = null)
 079        {
 080            ArgumentNullException.ThrowIfNull(utf8Json);
 81
 082            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 083            return jsonTypeInfo.Deserialize(utf8Json);
 084        }
 85
 86        /// <summary>
 87        /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 88        /// The Stream will be read to completion.
 89        /// </summary>
 90        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 91        /// <param name="utf8Json">JSON data to parse.</param>
 92        /// <param name="returnType">The type of the object to convert to and return.</param>
 93        /// <param name="options">Options to control the behavior during reading.</param>
 94        /// <param name="cancellationToken">
 95        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 96        /// </param>
 97        /// <exception cref="System.ArgumentNullException">
 98        /// <paramref name="utf8Json"/> or <paramref name="returnType"/> is <see langword="null"/>.
 99        /// </exception>
 100        /// <exception cref="JsonException">
 101        /// The JSON is invalid,
 102        /// the <paramref name="returnType"/> is not compatible with the JSON,
 103        /// or when there is remaining data in the Stream.
 104        /// </exception>
 105        /// <exception cref="NotSupportedException">
 106        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 107        /// for <paramref name="returnType"/> or its serializable members.
 108        /// </exception>
 109        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 110        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 111        public static ValueTask<object?> DeserializeAsync(
 112            Stream utf8Json,
 113            Type returnType,
 114            JsonSerializerOptions? options = null,
 115            CancellationToken cancellationToken = default)
 27962116        {
 27962117            ArgumentNullException.ThrowIfNull(utf8Json);
 27962118            ArgumentNullException.ThrowIfNull(returnType);
 119
 27962120            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 27962121            return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
 27962122        }
 123
 124        /// <summary>
 125        /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 126        /// The Stream will be read to completion.
 127        /// </summary>
 128        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 129        /// <param name="utf8Json">JSON data to parse.</param>
 130        /// <param name="returnType">The type of the object to convert to and return.</param>
 131        /// <param name="options">Options to control the behavior during reading.</param>
 132        /// <exception cref="System.ArgumentNullException">
 133        /// <paramref name="utf8Json"/> or <paramref name="returnType"/> is <see langword="null"/>.
 134        /// </exception>
 135        /// <exception cref="JsonException">
 136        /// The JSON is invalid,
 137        /// the <paramref name="returnType"/> is not compatible with the JSON,
 138        /// or when there is remaining data in the Stream.
 139        /// </exception>
 140        /// <exception cref="NotSupportedException">
 141        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 142        /// for <paramref name="returnType"/> or its serializable members.
 143        /// </exception>
 144        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 145        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 146        public static object? Deserialize(
 147            Stream utf8Json,
 148            Type returnType,
 149            JsonSerializerOptions? options = null)
 0150        {
 0151            ArgumentNullException.ThrowIfNull(utf8Json);
 0152            ArgumentNullException.ThrowIfNull(returnType);
 153
 0154            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 0155            return jsonTypeInfo.DeserializeAsObject(utf8Json);
 0156        }
 157
 158        /// <summary>
 159        /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 160        /// The Stream will be read to completion.
 161        /// </summary>
 162        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 163        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 164        /// <param name="utf8Json">JSON data to parse.</param>
 165        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 166        /// <param name="cancellationToken">
 167        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 168        /// </param>
 169        /// <exception cref="System.ArgumentNullException">
 170        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 171        /// </exception>
 172        /// <exception cref="JsonException">
 173        /// The JSON is invalid,
 174        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 175        /// or when there is remaining data in the Stream.
 176        /// </exception>
 177        public static ValueTask<TValue?> DeserializeAsync<TValue>(
 178            Stream utf8Json,
 179            JsonTypeInfo<TValue> jsonTypeInfo,
 180            CancellationToken cancellationToken = default)
 0181        {
 0182            ArgumentNullException.ThrowIfNull(utf8Json);
 0183            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 184
 0185            jsonTypeInfo.EnsureConfigured();
 0186            return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken);
 0187        }
 188
 189        /// <summary>
 190        /// Reads the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref na
 191        /// The Stream will be read to completion.
 192        /// </summary>
 193        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 194        /// <param name="utf8Json">JSON data to parse.</param>
 195        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 196        /// <param name="cancellationToken">
 197        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 198        /// </param>
 199        /// <exception cref="System.ArgumentNullException">
 200        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 201        /// </exception>
 202        /// <exception cref="JsonException">
 203        /// The JSON is invalid,
 204        /// or when there is remaining data in the Stream.
 205        /// </exception>
 206        public static ValueTask<object?> DeserializeAsync(
 207            Stream utf8Json,
 208            JsonTypeInfo jsonTypeInfo,
 209            CancellationToken cancellationToken = default)
 0210        {
 0211            ArgumentNullException.ThrowIfNull(utf8Json);
 0212            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 213
 0214            jsonTypeInfo.EnsureConfigured();
 0215            return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
 0216        }
 217
 218        /// <summary>
 219        /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
 220        /// The Stream will be read to completion.
 221        /// </summary>
 222        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 223        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 224        /// <param name="utf8Json">JSON data to parse.</param>
 225        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 226        /// <exception cref="System.ArgumentNullException">
 227        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 228        /// </exception>
 229        /// <exception cref="JsonException">
 230        /// The JSON is invalid,
 231        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 232        /// or when there is remaining data in the Stream.
 233        /// </exception>
 234        public static TValue? Deserialize<TValue>(
 235            Stream utf8Json,
 236            JsonTypeInfo<TValue> jsonTypeInfo)
 0237        {
 0238            ArgumentNullException.ThrowIfNull(utf8Json);
 0239            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 240
 0241            jsonTypeInfo.EnsureConfigured();
 0242            return jsonTypeInfo.Deserialize(utf8Json);
 0243        }
 244
 245        /// <summary>
 246        /// Reads the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref na
 247        /// The Stream will be read to completion.
 248        /// </summary>
 249        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 250        /// <param name="utf8Json">JSON data to parse.</param>
 251        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 252        /// <exception cref="System.ArgumentNullException">
 253        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 254        /// </exception>
 255        /// <exception cref="JsonException">
 256        /// The JSON is invalid,
 257        /// or when there is remaining data in the Stream.
 258        /// </exception>
 259        public static object? Deserialize(
 260            Stream utf8Json,
 261            JsonTypeInfo jsonTypeInfo)
 0262        {
 0263            ArgumentNullException.ThrowIfNull(utf8Json);
 0264            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 265
 0266            jsonTypeInfo.EnsureConfigured();
 0267            return jsonTypeInfo.DeserializeAsObject(utf8Json);
 0268        }
 269
 270        /// <summary>
 271        /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 272        /// The Stream will be read to completion.
 273        /// </summary>
 274        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 275        /// <param name="utf8Json">JSON data to parse.</param>
 276        /// <param name="returnType">The type of the object to convert to and return.</param>
 277        /// <param name="context">A metadata provider for serializable types.</param>
 278        /// <param name="cancellationToken">
 279        /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation.
 280        /// </param>
 281        /// <exception cref="System.ArgumentNullException">
 282        /// <paramref name="utf8Json"/>, <paramref name="returnType"/>, or <paramref name="context"/> is <see langword="
 283        /// </exception>
 284        /// <exception cref="JsonException">
 285        /// The JSON is invalid,
 286        /// the <paramref name="returnType"/> is not compatible with the JSON,
 287        /// or when there is remaining data in the Stream.
 288        /// </exception>
 289        /// <exception cref="NotSupportedException">
 290        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 291        /// for <paramref name="returnType"/> or its serializable members.
 292        /// </exception>
 293        /// <exception cref="InvalidOperationException">
 294        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/>
 295        /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>.
 296        /// </exception>
 297        public static ValueTask<object?> DeserializeAsync(
 298            Stream utf8Json,
 299            Type returnType,
 300            JsonSerializerContext context,
 301            CancellationToken cancellationToken = default)
 0302        {
 0303            ArgumentNullException.ThrowIfNull(utf8Json);
 0304            ArgumentNullException.ThrowIfNull(returnType);
 0305            ArgumentNullException.ThrowIfNull(context);
 306
 0307            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0308            return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
 0309        }
 310
 311        /// <summary>
 312        /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
 313        /// The Stream will be read to completion.
 314        /// </summary>
 315        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 316        /// <param name="utf8Json">JSON data to parse.</param>
 317        /// <param name="returnType">The type of the object to convert to and return.</param>
 318        /// <param name="context">A metadata provider for serializable types.</param>
 319        /// <exception cref="System.ArgumentNullException">
 320        /// <paramref name="utf8Json"/>, <paramref name="returnType"/>, or <paramref name="context"/> is <see langword="
 321        /// </exception>
 322        /// <exception cref="JsonException">
 323        /// The JSON is invalid,
 324        /// the <paramref name="returnType"/> is not compatible with the JSON,
 325        /// or when there is remaining data in the Stream.
 326        /// </exception>
 327        /// <exception cref="NotSupportedException">
 328        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 329        /// for <paramref name="returnType"/> or its serializable members.
 330        /// </exception>
 331        /// <exception cref="InvalidOperationException">
 332        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/>
 333        /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>.
 334        /// </exception>
 335        public static object? Deserialize(
 336            Stream utf8Json,
 337            Type returnType,
 338            JsonSerializerContext context)
 0339        {
 0340            ArgumentNullException.ThrowIfNull(utf8Json);
 0341            ArgumentNullException.ThrowIfNull(returnType);
 0342            ArgumentNullException.ThrowIfNull(context);
 343
 0344            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0345            return jsonTypeInfo.DeserializeAsObject(utf8Json);
 0346        }
 347
 348        /// <summary>
 349        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 350        /// that can be used to deserialize root-level JSON arrays in a streaming manner.
 351        /// </summary>
 352        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 353        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON array.</returns>
 354        /// <param name="utf8Json">JSON data to parse.</param>
 355        /// <param name="options">Options to control the behavior during reading.</param>
 356        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 357        /// <exception cref="System.ArgumentNullException">
 358        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 359        /// </exception>
 360        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 361        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 362        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 363            Stream utf8Json,
 364            JsonSerializerOptions? options = null,
 365            CancellationToken cancellationToken = default)
 0366        {
 0367            return DeserializeAsyncEnumerable<TValue>(utf8Json, topLevelValues: false, options, cancellationToken);
 0368        }
 369
 370        /// <summary>
 371        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 372        /// that can be used to deserialize sequences of JSON values in a streaming manner.
 373        /// </summary>
 374        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 375        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON sequence.</returns>
 376        /// <param name="utf8Json">JSON data to parse.</param>
 377        /// <param name="topLevelValues"><see langword="true"/> to deserialize from a sequence of top-level JSON values,
 378        /// <param name="options">Options to control the behavior during reading.</param>
 379        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 380        /// <exception cref="System.ArgumentNullException">
 381        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 382        /// </exception>
 383        /// <remarks>
 384        /// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the stream as a sequence of
 385        /// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="T
 386        /// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the stream as a JSON array
 387        /// attempts to serialize each element into <typeparamref name="TValue"/>.
 388        /// </remarks>
 389        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 390        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 391        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 392            Stream utf8Json,
 393            bool topLevelValues,
 394            JsonSerializerOptions? options = null,
 395            CancellationToken cancellationToken = default)
 0396        {
 0397            ArgumentNullException.ThrowIfNull(utf8Json);
 398
 0399            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 0400            return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken);
 0401        }
 402
 403        /// <summary>
 404        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 405        /// that can be used to deserialize root-level JSON arrays in a streaming manner.
 406        /// </summary>
 407        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 408        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON array.</returns>
 409        /// <param name="utf8Json">JSON data to parse.</param>
 410        /// <param name="jsonTypeInfo">Metadata about the element type to convert.</param>
 411        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 412        /// <exception cref="System.ArgumentNullException">
 413        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 414        /// </exception>
 415        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 416            Stream utf8Json,
 417            JsonTypeInfo<TValue> jsonTypeInfo,
 418            CancellationToken cancellationToken = default)
 0419        {
 0420            return DeserializeAsyncEnumerable(utf8Json, jsonTypeInfo, topLevelValues: false, cancellationToken);
 0421        }
 422
 423        /// <summary>
 424        /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" />
 425        /// that can be used to deserialize sequences of JSON values in a streaming manner.
 426        /// </summary>
 427        /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam>
 428        /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON sequence.</returns>
 429        /// <param name="utf8Json">JSON data to parse.</param>
 430        /// <param name="jsonTypeInfo">Metadata about the element type to convert.</param>
 431        /// <param name="topLevelValues">Whether to deserialize from a sequence of top-level JSON values.</param>
 432        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 433        /// <exception cref="System.ArgumentNullException">
 434        /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 435        /// </exception>
 436        /// <remarks>
 437        /// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the stream as a sequence of
 438        /// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="T
 439        /// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the stream as a JSON array
 440        /// attempts to serialize each element into <typeparamref name="TValue"/>.
 441        /// </remarks>
 442        public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
 443            Stream utf8Json,
 444            JsonTypeInfo<TValue> jsonTypeInfo,
 445            bool topLevelValues,
 446            CancellationToken cancellationToken = default)
 0447        {
 0448            ArgumentNullException.ThrowIfNull(utf8Json);
 0449            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 450
 0451            jsonTypeInfo.EnsureConfigured();
 0452            return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken);
 0453        }
 454
 455        private static IAsyncEnumerable<T?> DeserializeAsyncEnumerableCore<T>(
 456            Stream utf8Json,
 457            JsonTypeInfo<T> jsonTypeInfo,
 458            bool topLevelValues,
 459            CancellationToken cancellationToken)
 0460        {
 0461            Debug.Assert(jsonTypeInfo.IsConfigured);
 462
 463            JsonTypeInfo<List<T?>> listTypeInfo;
 0464            JsonReaderOptions readerOptions = jsonTypeInfo.Options.GetReaderOptions();
 0465            if (topLevelValues)
 0466            {
 0467                listTypeInfo = GetOrAddListTypeInfoForRootLevelValueMode(jsonTypeInfo);
 0468                readerOptions.AllowMultipleValues = true;
 0469            }
 470            else
 0471            {
 0472                listTypeInfo = GetOrAddListTypeInfoForArrayMode(jsonTypeInfo);
 0473            }
 474
 0475            return CreateAsyncEnumerableFromArray(utf8Json, listTypeInfo, readerOptions, cancellationToken);
 476
 477            static async IAsyncEnumerable<T?> CreateAsyncEnumerableFromArray(
 478                Stream utf8Json,
 479                JsonTypeInfo<List<T?>> listTypeInfo,
 480                JsonReaderOptions readerOptions,
 481                [EnumeratorCancellation] CancellationToken cancellationToken)
 0482            {
 0483                Debug.Assert(listTypeInfo.IsConfigured);
 484
 0485                ReadStack readStack = default;
 0486                readStack.Initialize(listTypeInfo, supportContinuation: true);
 0487                JsonReaderState jsonReaderState = new(readerOptions);
 488                // Note: The StreamReadBufferState ctor rents pooled buffers.
 0489                StreamReadBufferState bufferState = new StreamReadBufferState(listTypeInfo.Options.DefaultBufferSize);
 490
 491                try
 0492                {
 493                    bool success;
 494                    do
 0495                    {
 0496                        bufferState = await bufferState.ReadAsync(utf8Json, cancellationToken, fillBuffer: false).Config
 0497                        success = listTypeInfo.ContinueDeserialize<StreamReadBufferState, Stream>(
 0498                            ref bufferState,
 0499                            ref jsonReaderState,
 0500                            ref readStack,
 0501                            out List<T?>? _);
 502
 0503                        if (readStack.Current.ReturnValue is { } returnValue)
 0504                        {
 0505                            var list = (List<T?>)returnValue;
 0506                            foreach (T? item in list)
 0507                            {
 0508                                yield return item;
 0509                            }
 510
 0511                            list.Clear();
 0512                        }
 513
 0514                    } while (!success);
 0515                }
 516                finally
 0517                {
 0518                    bufferState.Dispose();
 0519                }
 0520            }
 0521        }
 522    }
 523}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Text.Json.Serialization;
 8using System.Text.Json.Serialization.Metadata;
 9
 10namespace System.Text.Json
 11{
 12    /// <summary>
 13    /// Provides functionality to serialize objects or value types to JSON and
 14    /// deserialize JSON into objects or value types.
 15    /// </summary>
 16    public static partial class JsonSerializer
 17    {
 18        /// <summary>
 19        /// Parses the text representing a single JSON value into a <typeparamref name="TValue"/>.
 20        /// </summary>
 21        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 22        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 23        /// <param name="json">JSON text to parse.</param>
 24        /// <param name="options">Options to control the behavior during parsing.</param>
 25        /// <exception cref="System.ArgumentNullException">
 26        /// <paramref name="json"/> is <see langword="null"/>.
 27        /// </exception>
 28        /// <exception cref="JsonException">
 29        /// The JSON is invalid.
 30        ///
 31        /// -or-
 32        ///
 33        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 34        ///
 35        /// -or-
 36        ///
 37        /// There is remaining data in the string beyond a single JSON value.</exception>
 38        /// <exception cref="NotSupportedException">
 39        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 40        /// for <typeparamref name="TValue"/> or its serializable members.
 41        /// </exception>
 42        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 43        /// UTF-8 methods since the implementation natively uses UTF-8.
 44        /// </remarks>
 45        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 46        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 47        public static TValue? Deserialize<TValue>([StringSyntax(StringSyntaxAttribute.Json)] string json, JsonSerializer
 048        {
 049            ArgumentNullException.ThrowIfNull(json);
 50
 051            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 052            return ReadFromSpan(json.AsSpan(), jsonTypeInfo);
 053        }
 54
 55        /// <summary>
 56        /// Parses the text representing a single JSON value into an instance of the type specified by a generic type pa
 57        /// </summary>
 58        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 59        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 60        /// <param name="json">The JSON text to parse.</param>
 61        /// <param name="options">Options to control the behavior during parsing.</param>
 62        /// <exception cref="JsonException">
 63        /// The JSON is invalid.
 64        ///
 65        /// -or-
 66        ///
 67        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 68        ///
 69        /// -or-
 70        ///
 71        /// There is remaining data in the span beyond a single JSON value.</exception>
 72        /// <exception cref="NotSupportedException">
 73        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 74        /// for <typeparamref name="TValue"/> or its serializable members.
 75        /// </exception>
 76        /// <remarks>Using a UTF-16 span is not as efficient as using the
 77        /// UTF-8 methods since the implementation natively uses UTF-8.
 78        /// </remarks>
 79        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 80        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 81        public static TValue? Deserialize<TValue>([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, Js
 082        {
 083            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 084            return ReadFromSpan(json, jsonTypeInfo);
 085        }
 86
 87        /// <summary>
 88        /// Parses the text representing a single JSON value into a <paramref name="returnType"/>.
 89        /// </summary>
 90        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 91        /// <param name="json">JSON text to parse.</param>
 92        /// <param name="returnType">The type of the object to convert to and return.</param>
 93        /// <param name="options">Options to control the behavior during parsing.</param>
 94        /// <exception cref="System.ArgumentNullException">
 95        /// <paramref name="json"/> or <paramref name="returnType"/> is <see langword="null"/>.
 96        /// </exception>
 97        /// <exception cref="JsonException">
 98        /// The JSON is invalid.
 99        ///
 100        /// -or-
 101        ///
 102        /// <paramref name="returnType"/> is not compatible with the JSON.
 103        ///
 104        /// -or-
 105        ///
 106        /// There is remaining data in the string beyond a single JSON value.</exception>
 107        /// <exception cref="NotSupportedException">
 108        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 109        /// for <paramref name="returnType"/> or its serializable members.
 110        /// </exception>
 111        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 112        /// UTF-8 methods since the implementation natively uses UTF-8.
 113        /// </remarks>
 114        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 115        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 116        public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] string json, Type returnType, JsonS
 0117        {
 0118            ArgumentNullException.ThrowIfNull(json);
 0119            ArgumentNullException.ThrowIfNull(returnType);
 120
 0121            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 0122            return ReadFromSpanAsObject(json.AsSpan(), jsonTypeInfo);
 0123        }
 124
 125        /// <summary>
 126        /// Parses the text representing a single JSON value into an instance of a specified type.
 127        /// </summary>
 128        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 129        /// <param name="json">The JSON text to parse.</param>
 130        /// <param name="returnType">The type of the object to convert to and return.</param>
 131        /// <param name="options">Options to control the behavior during parsing.</param>
 132        /// <exception cref="System.ArgumentNullException">
 133        /// <paramref name="returnType"/> is <see langword="null"/>.
 134        /// </exception>
 135        /// <exception cref="JsonException">
 136        /// The JSON is invalid.
 137        ///
 138        /// -or-
 139        ///
 140        /// <paramref name="returnType"/> is not compatible with the JSON.
 141        ///
 142        /// -or-
 143        ///
 144        /// There is remaining data in the span beyond a single JSON value.</exception>
 145        /// <exception cref="NotSupportedException">
 146        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 147        /// for <paramref name="returnType"/> or its serializable members.
 148        /// </exception>
 149        /// <remarks>Using a UTF-16 span is not as efficient as using the
 150        /// UTF-8 methods since the implementation natively uses UTF-8.
 151        /// </remarks>
 152        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 153        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 154        public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, Type retur
 0155        {
 0156            ArgumentNullException.ThrowIfNull(returnType);
 157
 158            // default/null span is treated as empty
 159
 0160            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 0161            return ReadFromSpanAsObject(json, jsonTypeInfo);
 0162        }
 163
 164        /// <summary>
 165        /// Parses the text representing a single JSON value into a <typeparamref name="TValue"/>.
 166        /// </summary>
 167        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 168        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 169        /// <param name="json">JSON text to parse.</param>
 170        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 171        /// <exception cref="System.ArgumentNullException">
 172        /// <paramref name="json"/> is <see langword="null"/>.
 173        ///
 174        /// -or-
 175        ///
 176        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 177        /// </exception>
 178        /// <exception cref="JsonException">
 179        /// The JSON is invalid.
 180        ///
 181        /// -or-
 182        ///
 183        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 184        ///
 185        /// -or-
 186        ///
 187        /// There is remaining data in the string beyond a single JSON value.</exception>
 188        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 189        /// UTF-8 methods since the implementation natively uses UTF-8.
 190        /// </remarks>
 191        public static TValue? Deserialize<TValue>([StringSyntax(StringSyntaxAttribute.Json)] string json, JsonTypeInfo<T
 0192        {
 0193            ArgumentNullException.ThrowIfNull(json);
 0194            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 195
 0196            jsonTypeInfo.EnsureConfigured();
 0197            return ReadFromSpan(json.AsSpan(), jsonTypeInfo);
 0198        }
 199
 200        /// <summary>
 201        /// Parses the text representing a single JSON value into a <typeparamref name="TValue"/>.
 202        /// </summary>
 203        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 204        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 205        /// <param name="json">JSON text to parse.</param>
 206        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 207        /// <exception cref="System.ArgumentNullException">
 208        /// <paramref name="json"/> is <see langword="null"/>.
 209        ///
 210        /// -or-
 211        ///
 212        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 213        /// </exception>
 214        /// <exception cref="JsonException">
 215        /// The JSON is invalid.
 216        ///
 217        /// -or-
 218        ///
 219        /// <typeparamref name="TValue" /> is not compatible with the JSON.
 220        ///
 221        /// -or-
 222        ///
 223        /// There is remaining data in the string beyond a single JSON value.</exception>
 224        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 225        /// UTF-8 methods since the implementation natively uses UTF-8.
 226        /// </remarks>
 227        public static TValue? Deserialize<TValue>([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, Js
 0228        {
 0229            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 230
 0231            jsonTypeInfo.EnsureConfigured();
 0232            return ReadFromSpan(json, jsonTypeInfo);
 0233        }
 234
 235        /// <summary>
 236        /// Parses the text representing a single JSON value into an instance specified by the <paramref name="jsonTypeI
 237        /// </summary>
 238        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 239        /// <param name="json">JSON text to parse.</param>
 240        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 241        /// <exception cref="System.ArgumentNullException">
 242        /// <paramref name="json"/> is <see langword="null"/>.
 243        ///
 244        /// -or-
 245        ///
 246        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 247        /// </exception>
 248        /// <exception cref="JsonException">
 249        /// The JSON is invalid.
 250        ///
 251        /// -or-
 252        ///
 253        /// There is remaining data in the string beyond a single JSON value.</exception>
 254        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 255        /// UTF-8 methods since the implementation natively uses UTF-8.
 256        /// </remarks>
 257        public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] string json, JsonTypeInfo jsonTypeI
 0258        {
 0259            ArgumentNullException.ThrowIfNull(json);
 0260            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 261
 0262            jsonTypeInfo.EnsureConfigured();
 0263            return ReadFromSpanAsObject(json.AsSpan(), jsonTypeInfo);
 0264        }
 265
 266        /// <summary>
 267        /// Parses the text representing a single JSON value into an instance specified by the <paramref name="jsonTypeI
 268        /// </summary>
 269        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 270        /// <param name="json">JSON text to parse.</param>
 271        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 272        /// <exception cref="System.ArgumentNullException">
 273        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 274        /// </exception>
 275        /// <exception cref="JsonException">
 276        /// The JSON is invalid.
 277        ///
 278        /// -or-
 279        ///
 280        /// There is remaining data in the string beyond a single JSON value.</exception>
 281        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 282        /// UTF-8 methods since the implementation natively uses UTF-8.
 283        /// </remarks>
 284        public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, JsonTypeIn
 0285        {
 0286            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 287
 0288            jsonTypeInfo.EnsureConfigured();
 0289            return ReadFromSpanAsObject(json, jsonTypeInfo);
 0290        }
 291
 292        /// <summary>
 293        /// Parses the text representing a single JSON value into a <paramref name="returnType"/>.
 294        /// </summary>
 295        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 296        /// <param name="json">JSON text to parse.</param>
 297        /// <param name="returnType">The type of the object to convert to and return.</param>
 298        /// <param name="context">A metadata provider for serializable types.</param>
 299        /// <exception cref="System.ArgumentNullException">
 300        /// <paramref name="json"/> or <paramref name="returnType"/> is <see langword="null"/>.
 301        ///
 302        /// -or-
 303        ///
 304        /// <paramref name="context"/> is <see langword="null"/>.
 305        /// </exception>
 306        /// <exception cref="JsonException">
 307        /// The JSON is invalid.
 308        ///
 309        /// -or-
 310        ///
 311        /// <paramref name="returnType" /> is not compatible with the JSON.
 312        ///
 313        /// -or-
 314        ///
 315        /// There is remaining data in the string beyond a single JSON value.</exception>
 316        /// <exception cref="NotSupportedException">
 317        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 318        /// for <paramref name="returnType"/> or its serializable members.
 319        /// </exception>
 320        /// <exception cref="InvalidOperationException">
 321        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 322        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 323        /// </exception>
 324        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 325        /// UTF-8 methods since the implementation natively uses UTF-8.
 326        /// </remarks>
 327        public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] string json, Type returnType, JsonS
 0328        {
 0329            ArgumentNullException.ThrowIfNull(json);
 0330            ArgumentNullException.ThrowIfNull(returnType);
 0331            ArgumentNullException.ThrowIfNull(context);
 332
 0333            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0334            return ReadFromSpanAsObject(json.AsSpan(), jsonTypeInfo);
 0335        }
 336
 337        /// <summary>
 338        /// Parses the text representing a single JSON value into a <paramref name="returnType"/>.
 339        /// </summary>
 340        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 341        /// <param name="json">JSON text to parse.</param>
 342        /// <param name="returnType">The type of the object to convert to and return.</param>
 343        /// <param name="context">A metadata provider for serializable types.</param>
 344        /// <exception cref="System.ArgumentNullException">
 345        /// <paramref name="json"/> or <paramref name="returnType"/> is <see langword="null"/>.
 346        ///
 347        /// -or-
 348        ///
 349        /// <paramref name="context"/> is <see langword="null"/>.
 350        /// </exception>
 351        /// <exception cref="JsonException">
 352        /// The JSON is invalid.
 353        ///
 354        /// -or-
 355        ///
 356        /// <paramref name="returnType" /> is not compatible with the JSON.
 357        ///
 358        /// -or-
 359        ///
 360        /// There is remaining data in the string beyond a single JSON value.</exception>
 361        /// <exception cref="NotSupportedException">
 362        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 363        /// for <paramref name="returnType"/> or its serializable members.
 364        /// </exception>
 365        /// <exception cref="InvalidOperationException">
 366        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 367        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 368        /// </exception>
 369        /// <remarks>Using a <see cref="string"/> is not as efficient as using the
 370        /// UTF-8 methods since the implementation natively uses UTF-8.
 371        /// </remarks>
 372        public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, Type retur
 0373        {
 0374            ArgumentNullException.ThrowIfNull(returnType);
 0375            ArgumentNullException.ThrowIfNull(context);
 376
 0377            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
 0378            return ReadFromSpanAsObject(json, jsonTypeInfo);
 0379        }
 380
 381        private static TValue? ReadFromSpan<TValue>(ReadOnlySpan<char> json, JsonTypeInfo<TValue> jsonTypeInfo)
 0382        {
 0383            Debug.Assert(jsonTypeInfo.IsConfigured);
 0384            byte[]? tempArray = null;
 385
 386            // For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold.
 0387            Span<byte> utf8 =
 0388                // Use stack memory
 0389                json.Length <= (JsonConstants.StackallocByteThreshold / JsonConstants.MaxExpansionFactorWhileTranscoding
 0390                // Use a pooled array
 0391                json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorW
 0392                // Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per curr
 0393                // and by using a normal alloc we can avoid the Clear().
 0394                new byte[JsonReaderHelper.GetUtf8ByteCount(json)];
 395
 396            try
 0397            {
 0398                int actualByteCount = JsonReaderHelper.GetUtf8FromText(json, utf8);
 0399                utf8 = utf8.Slice(0, actualByteCount);
 0400                return ReadFromSpan(utf8, jsonTypeInfo, actualByteCount);
 401            }
 402            finally
 0403            {
 0404                if (tempArray != null)
 0405                {
 0406                    utf8.Clear();
 0407                    ArrayPool<byte>.Shared.Return(tempArray);
 0408                }
 0409            }
 0410        }
 411
 412        private static object? ReadFromSpanAsObject(ReadOnlySpan<char> json, JsonTypeInfo jsonTypeInfo)
 0413        {
 0414            Debug.Assert(jsonTypeInfo.IsConfigured);
 0415            byte[]? tempArray = null;
 416
 417            // For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold.
 0418            Span<byte> utf8 =
 0419                // Use stack memory
 0420                json.Length <= (JsonConstants.StackallocByteThreshold / JsonConstants.MaxExpansionFactorWhileTranscoding
 0421                // Use a pooled array
 0422                json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorW
 0423                // Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per curr
 0424                // and by using a normal alloc we can avoid the Clear().
 0425                new byte[JsonReaderHelper.GetUtf8ByteCount(json)];
 426
 427            try
 0428            {
 0429                int actualByteCount = JsonReaderHelper.GetUtf8FromText(json, utf8);
 0430                utf8 = utf8.Slice(0, actualByteCount);
 0431                return ReadFromSpanAsObject(utf8, jsonTypeInfo, actualByteCount);
 432            }
 433            finally
 0434            {
 0435                if (tempArray != null)
 0436                {
 0437                    utf8.Clear();
 0438                    ArrayPool<byte>.Shared.Return(tempArray);
 0439                }
 0440            }
 0441        }
 442    }
 443}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Utf8JsonReader.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Text.Json.Serialization;
 8using System.Text.Json.Serialization.Metadata;
 9
 10namespace System.Text.Json
 11{
 12    public static partial class JsonSerializer
 13    {
 14        /// <summary>
 15        /// Reads one JSON value (including objects or arrays) from the provided reader into a <typeparamref name="TValu
 16        /// </summary>
 17        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 18        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 19        /// <param name="reader">The reader to read.</param>
 20        /// <param name="options">Options to control the serializer behavior during reading.</param>
 21        /// <exception cref="JsonException">
 22        /// The JSON is invalid,
 23        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 24        /// or a value could not be read from the reader.
 25        /// </exception>
 26        /// <exception cref="ArgumentException">
 27        ///   <paramref name="reader"/> is using unsupported options.
 28        /// </exception>
 29        /// <exception cref="NotSupportedException">
 30        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 31        /// for <typeparamref name="TValue"/> or its serializable members.
 32        /// </exception>
 33        /// <remarks>
 34        ///   <para>
 35        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 36        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 37        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 38        ///     the start of the value.
 39        ///   </para>
 40        ///
 41        ///   <para>
 42        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 43        ///     final token in the JSON value. If an exception is thrown, the reader is reset to
 44        ///     the state it was in when the method was called.
 45        ///   </para>
 46        ///
 47        ///   <para>
 48        ///     This method makes a copy of the data the reader acted on, so there is no caller
 49        ///     requirement to maintain data integrity beyond the return of this method.
 50        ///   </para>
 51        ///
 52        ///   <para>
 53        ///     The <see cref="JsonReaderOptions"/> used to create the instance of the <see cref="Utf8JsonReader"/> take
 54        ///     Hence, <see cref="JsonReaderOptions.AllowTrailingCommas"/>, <see cref="JsonReaderOptions.MaxDepth"/>, an
 55        ///   </para>
 56        /// </remarks>
 57        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 58        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 59        public static TValue? Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions? options = null)
 060        {
 061            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 062            return Read<TValue>(ref reader, jsonTypeInfo);
 063        }
 64
 65        /// <summary>
 66        /// Reads one JSON value (including objects or arrays) from the provided reader into a <paramref name="returnTyp
 67        /// </summary>
 68        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 69        /// <param name="reader">The reader to read.</param>
 70        /// <param name="returnType">The type of the object to convert to and return.</param>
 71        /// <param name="options">Options to control the serializer behavior during reading.</param>
 72        /// <exception cref="ArgumentNullException">
 73        /// <paramref name="returnType"/> is <see langword="null"/>.
 74        /// </exception>
 75        /// <exception cref="JsonException">
 76        /// The JSON is invalid,
 77        /// <paramref name="returnType"/> is not compatible with the JSON,
 78        /// or a value could not be read from the reader.
 79        /// </exception>
 80        /// <exception cref="ArgumentException">
 81        ///   <paramref name="reader"/> is using unsupported options.
 82        /// </exception>
 83        /// <exception cref="NotSupportedException">
 84        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 85        /// for <paramref name="returnType"/> or its serializable members.
 86        /// </exception>
 87        /// <remarks>
 88        ///   <para>
 89        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 90        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 91        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 92        ///     the start of the value.
 93        ///   </para>
 94        ///
 95        ///   <para>
 96        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 97        ///     final token in the JSON value. If an exception is thrown, the reader is reset to
 98        ///     the state it was in when the method was called.
 99        ///   </para>
 100        ///
 101        ///   <para>
 102        ///     This method makes a copy of the data the reader acted on, so there is no caller
 103        ///     requirement to maintain data integrity beyond the return of this method.
 104        ///   </para>
 105        ///   <para>
 106        ///     The <see cref="JsonReaderOptions"/> used to create the instance of the <see cref="Utf8JsonReader"/> take
 107        ///     Hence, <see cref="JsonReaderOptions.AllowTrailingCommas"/>, <see cref="JsonReaderOptions.MaxDepth"/>, an
 108        ///   </para>
 109        /// </remarks>
 110        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 111        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 112        public static object? Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions? options = n
 0113        {
 0114            ArgumentNullException.ThrowIfNull(returnType);
 115
 0116            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
 0117            return ReadAsObject(ref reader, jsonTypeInfo);
 0118        }
 119
 120        /// <summary>
 121        /// Reads one JSON value (including objects or arrays) from the provided reader into a <typeparamref name="TValu
 122        /// </summary>
 123        /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
 124        /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
 125        /// <param name="reader">The reader to read.</param>
 126        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 127        /// <exception cref="JsonException">
 128        /// The JSON is invalid,
 129        /// <typeparamref name="TValue"/> is not compatible with the JSON,
 130        /// or a value could not be read from the reader.
 131        /// </exception>
 132        /// <exception cref="ArgumentException">
 133        ///   <paramref name="reader"/> is using unsupported options.
 134        /// </exception>
 135        /// <remarks>
 136        ///   <para>
 137        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 138        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 139        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 140        ///     the start of the value.
 141        ///   </para>
 142        ///
 143        ///   <para>
 144        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 145        ///     final token in the JSON value. If an exception is thrown, the reader is reset to
 146        ///     the state it was in when the method was called.
 147        ///   </para>
 148        ///
 149        ///   <para>
 150        ///     This method makes a copy of the data the reader acted on, so there is no caller
 151        ///     requirement to maintain data integrity beyond the return of this method.
 152        ///   </para>
 153        ///
 154        ///   <para>
 155        ///     The <see cref="JsonReaderOptions"/> used to create the instance of the <see cref="Utf8JsonReader"/> take
 156        ///     Hence, <see cref="JsonReaderOptions.AllowTrailingCommas"/>, <see cref="JsonReaderOptions.MaxDepth"/>, an
 157        ///   </para>
 158        /// </remarks>
 159        public static TValue? Deserialize<TValue>(ref Utf8JsonReader reader, JsonTypeInfo<TValue> jsonTypeInfo)
 0160        {
 0161            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 162
 0163            jsonTypeInfo.EnsureConfigured();
 0164            return Read(ref reader, jsonTypeInfo);
 0165        }
 166
 167        /// <summary>
 168        /// Reads one JSON value (including objects or arrays) from the provided reader into an instance specified by th
 169        /// </summary>
 170        /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
 171        /// <param name="reader">The reader to read.</param>
 172        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 173        /// <exception cref="JsonException">
 174        /// The JSON is invalid,
 175        /// <paramref name="jsonTypeInfo"/> is not compatible with the JSON,
 176        /// or a value could not be read from the reader.
 177        /// </exception>
 178        /// <exception cref="ArgumentException">
 179        ///   <paramref name="reader"/> is using unsupported options.
 180        /// </exception>
 181        /// <remarks>
 182        ///   <para>
 183        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 184        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 185        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 186        ///     the start of the value.
 187        ///   </para>
 188        ///
 189        ///   <para>
 190        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 191        ///     final token in the JSON value. If an exception is thrown, the reader is reset to
 192        ///     the state it was in when the method was called.
 193        ///   </para>
 194        ///
 195        ///   <para>
 196        ///     This method makes a copy of the data the reader acted on, so there is no caller
 197        ///     requirement to maintain data integrity beyond the return of this method.
 198        ///   </para>
 199        ///
 200        ///   <para>
 201        ///     The <see cref="JsonReaderOptions"/> used to create the instance of the <see cref="Utf8JsonReader"/> take
 202        ///     Hence, <see cref="JsonReaderOptions.AllowTrailingCommas"/>, <see cref="JsonReaderOptions.MaxDepth"/>, an
 203        ///   </para>
 204        /// </remarks>
 205        public static object? Deserialize(ref Utf8JsonReader reader, JsonTypeInfo jsonTypeInfo)
 0206        {
 0207            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 208
 0209            jsonTypeInfo.EnsureConfigured();
 0210            return ReadAsObject(ref reader, jsonTypeInfo);
 0211        }
 212
 213        /// <summary>
 214        /// Reads one JSON value (including objects or arrays) from the provided reader into a <paramref name="returnTyp
 215        /// </summary>
 216        /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
 217        /// <param name="reader">The reader to read.</param>
 218        /// <param name="returnType">The type of the object to convert to and return.</param>
 219        /// <param name="context">A metadata provider for serializable types.</param>
 220        /// <exception cref="ArgumentNullException">
 221        /// <paramref name="returnType"/> or <paramref name="context"/> is <see langword="null"/>.
 222        /// </exception>
 223        /// <exception cref="JsonException">
 224        /// The JSON is invalid,
 225        /// <paramref name="returnType"/> is not compatible with the JSON,
 226        /// or a value could not be read from the reader.
 227        /// </exception>
 228        /// <exception cref="ArgumentException">
 229        ///   <paramref name="reader"/> is using unsupported options.
 230        /// </exception>
 231        /// <exception cref="NotSupportedException">
 232        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 233        /// for <paramref name="returnType"/> or its serializable members.
 234        /// </exception>
 235        /// <exception cref="InvalidOperationException">
 236        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/>
 237        /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>.
 238        /// </exception>
 239        /// <remarks>
 240        ///   <para>
 241        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
 242        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
 243        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
 244        ///     the start of the value.
 245        ///   </para>
 246        ///
 247        ///   <para>
 248        ///     Upon completion of this method, <paramref name="reader"/> will be positioned at the
 249        ///     final token in the JSON value. If an exception is thrown, the reader is reset to
 250        ///     the state it was in when the method was called.
 251        ///   </para>
 252        ///
 253        ///   <para>
 254        ///     This method makes a copy of the data the reader acted on, so there is no caller
 255        ///     requirement to maintain data integrity beyond the return of this method.
 256        ///   </para>
 257        ///   <para>
 258        ///     The <see cref="JsonReaderOptions"/> used to create the instance of the <see cref="Utf8JsonReader"/> take
 259        ///     Hence, <see cref="JsonReaderOptions.AllowTrailingCommas"/>, <see cref="JsonReaderOptions.MaxDepth"/>, an
 260        ///   </para>
 261        /// </remarks>
 262        public static object? Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerContext context)
 0263        {
 0264            ArgumentNullException.ThrowIfNull(returnType);
 0265            ArgumentNullException.ThrowIfNull(context);
 266
 0267            return ReadAsObject(ref reader, GetTypeInfo(context, returnType));
 0268        }
 269
 270        private static TValue? Read<TValue>(ref Utf8JsonReader reader, JsonTypeInfo<TValue> jsonTypeInfo)
 0271        {
 0272            Debug.Assert(jsonTypeInfo.IsConfigured);
 273
 0274            if (reader.CurrentState.Options.CommentHandling == JsonCommentHandling.Allow)
 0275            {
 0276                ThrowHelper.ThrowArgumentException_SerializerDoesNotSupportComments(nameof(reader));
 277            }
 278
 0279            ReadStack state = default;
 0280            state.Initialize(jsonTypeInfo);
 0281            Utf8JsonReader restore = reader;
 282
 283            try
 0284            {
 0285                Utf8JsonReader scopedReader = GetReaderScopedToNextValue(ref reader, ref state);
 0286                return jsonTypeInfo.Deserialize(ref scopedReader, ref state);
 287            }
 0288            catch (JsonException)
 0289            {
 0290                reader = restore;
 0291                throw;
 292            }
 0293        }
 294
 295        private static object? ReadAsObject(ref Utf8JsonReader reader, JsonTypeInfo jsonTypeInfo)
 0296        {
 0297            Debug.Assert(jsonTypeInfo.IsConfigured);
 298
 0299            if (reader.CurrentState.Options.CommentHandling == JsonCommentHandling.Allow)
 0300            {
 0301                ThrowHelper.ThrowArgumentException_SerializerDoesNotSupportComments(nameof(reader));
 302            }
 303
 0304            ReadStack state = default;
 0305            state.Initialize(jsonTypeInfo);
 0306            Utf8JsonReader restore = reader;
 307
 308            try
 0309            {
 0310                Utf8JsonReader scopedReader = GetReaderScopedToNextValue(ref reader, ref state);
 0311                return jsonTypeInfo.DeserializeAsObject(ref scopedReader, ref state);
 312            }
 0313            catch (JsonException)
 0314            {
 0315                reader = restore;
 0316                throw;
 317            }
 0318        }
 319
 320        private static Utf8JsonReader GetReaderScopedToNextValue(ref Utf8JsonReader reader, scoped ref ReadStack state)
 0321        {
 322            // Advances the provided reader, validating that it is pointing to a complete JSON value.
 323            // If successful, returns a new Utf8JsonReader that is scoped to the next value, reusing existing buffers.
 324
 0325            ReadOnlySpan<byte> valueSpan = default;
 0326            ReadOnlySequence<byte> valueSequence = default;
 327
 328            try
 0329            {
 0330                switch (reader.TokenType)
 331                {
 332                    // A new reader was created and has never been read,
 333                    // so we need to move to the first token.
 334                    // (or a reader has terminated and we're about to throw)
 335                    case JsonTokenType.None:
 336                    // Using a reader loop the caller has identified a property they wish to
 337                    // hydrate into a JsonDocument. Move to the value first.
 338                    case JsonTokenType.PropertyName:
 0339                        {
 0340                            if (!reader.Read())
 0341                            {
 0342                                ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.ExpectedOneCompleteTo
 343                            }
 0344                            break;
 345                        }
 346                }
 347
 0348                switch (reader.TokenType)
 349                {
 350                    // Any of the "value start" states are acceptable.
 351                    case JsonTokenType.StartObject:
 352                    case JsonTokenType.StartArray:
 0353                        long startingOffset = reader.TokenStartIndex;
 354
 0355                        if (!reader.TrySkip())
 0356                        {
 0357                            ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.NotEnoughData);
 358                        }
 359
 0360                        long totalLength = reader.BytesConsumed - startingOffset;
 0361                        ReadOnlySequence<byte> sequence = reader.OriginalSequence;
 362
 0363                        if (sequence.IsEmpty)
 0364                        {
 0365                            valueSpan = reader.OriginalSpan.Slice(
 0366                                checked((int)startingOffset),
 0367                                checked((int)totalLength));
 0368                        }
 369                        else
 0370                        {
 0371                            valueSequence = sequence.Slice(startingOffset, totalLength);
 0372                        }
 373
 0374                        Debug.Assert(reader.TokenType is JsonTokenType.EndObject or JsonTokenType.EndArray);
 0375                        break;
 376
 377                    // Single-token values
 378                    case JsonTokenType.Number:
 379                    case JsonTokenType.True:
 380                    case JsonTokenType.False:
 381                    case JsonTokenType.Null:
 0382                        if (reader.HasValueSequence)
 0383                        {
 0384                            valueSequence = reader.ValueSequence;
 0385                        }
 386                        else
 0387                        {
 0388                            valueSpan = reader.ValueSpan;
 0389                        }
 390
 0391                        break;
 392
 393                    // String's ValueSequence/ValueSpan omits the quotes, we need them back.
 394                    case JsonTokenType.String:
 0395                        ReadOnlySequence<byte> originalSequence = reader.OriginalSequence;
 396
 0397                        if (originalSequence.IsEmpty)
 0398                        {
 399                            // Since the quoted string fit in a ReadOnlySpan originally
 400                            // the contents length plus the two quotes can't overflow.
 0401                            int payloadLength = reader.ValueSpan.Length + 2;
 0402                            Debug.Assert(payloadLength > 1);
 403
 0404                            ReadOnlySpan<byte> readerSpan = reader.OriginalSpan;
 405
 0406                            Debug.Assert(
 0407                                readerSpan[(int)reader.TokenStartIndex] == (byte)'"',
 0408                                $"Calculated span starts with {readerSpan[(int)reader.TokenStartIndex]}");
 409
 0410                            Debug.Assert(
 0411                                readerSpan[(int)reader.TokenStartIndex + payloadLength - 1] == (byte)'"',
 0412                                $"Calculated span ends with {readerSpan[(int)reader.TokenStartIndex + payloadLength - 1]
 413
 0414                            valueSpan = readerSpan.Slice((int)reader.TokenStartIndex, payloadLength);
 0415                        }
 416                        else
 0417                        {
 0418                            long payloadLength = reader.HasValueSequence
 0419                                ? reader.ValueSequence.Length + 2
 0420                                : reader.ValueSpan.Length + 2;
 421
 0422                            valueSequence = originalSequence.Slice(reader.TokenStartIndex, payloadLength);
 0423                            Debug.Assert(
 0424                                valueSequence.First.Span[0] == (byte)'"',
 0425                                $"Calculated sequence starts with {valueSequence.First.Span[0]}");
 426
 0427                            Debug.Assert(
 0428                                valueSequence.ToArray()[payloadLength - 1] == (byte)'"',
 0429                                $"Calculated sequence ends with {valueSequence.ToArray()[payloadLength - 1]}");
 0430                        }
 431
 0432                        break;
 433
 434                    default:
 0435                        byte displayByte = reader.HasValueSequence
 0436                            ? reader.ValueSequence.First.Span[0]
 0437                            : reader.ValueSpan[0];
 438
 0439                        ThrowHelper.ThrowJsonReaderException(
 0440                            ref reader,
 0441                            ExceptionResource.ExpectedStartOfValueNotFound,
 0442                            displayByte);
 443
 444                        break;
 445                }
 0446            }
 0447            catch (JsonReaderException ex)
 0448            {
 449                // Re-throw with Path information.
 0450                ThrowHelper.ReThrowWithPath(ref state, ex);
 451            }
 452
 0453            Debug.Assert(!valueSpan.IsEmpty ^ !valueSequence.IsEmpty);
 454
 0455            return valueSpan.IsEmpty
 0456                ? new Utf8JsonReader(valueSequence, reader.CurrentState.Options)
 0457                : new Utf8JsonReader(valueSpan, reader.CurrentState.Options);
 0458        }
 459    }
 460}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.ByteArray.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json
 10{
 11    public static partial class JsonSerializer
 12    {
 13        /// <summary>
 14        /// Converts the provided value into a <see cref="byte"/> array.
 15        /// </summary>
 16        /// <returns>A UTF-8 representation of the value.</returns>
 17        /// <param name="value">The value to convert.</param>
 18        /// <param name="options">Options to control the conversion behavior.</param>
 19        /// <exception cref="NotSupportedException">
 20        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 21        /// for <typeparamref name="TValue"/> or its serializable members.
 22        /// </exception>
 23        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 24        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 25        public static byte[] SerializeToUtf8Bytes<TValue>(
 26            TValue value,
 27            JsonSerializerOptions? options = null)
 028        {
 029            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 030            return WriteBytes(value, jsonTypeInfo);
 031        }
 32
 33        /// <summary>
 34        /// Converts the provided value into a <see cref="byte"/> array.
 35        /// </summary>
 36        /// <returns>A UTF-8 representation of the value.</returns>
 37        /// <param name="value">The value to convert.</param>
 38        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 39        /// <param name="options">Options to control the conversion behavior.</param>
 40        /// <exception cref="ArgumentException">
 41        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 42        /// </exception>
 43        /// <exception cref="System.ArgumentNullException">
 44        /// <paramref name="inputType"/> is <see langword="null"/>.
 45        /// </exception>
 46        /// <exception cref="NotSupportedException">
 47        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 48        /// for <paramref name="inputType"/>  or its serializable members.
 49        /// </exception>
 50        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 51        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 52        public static byte[] SerializeToUtf8Bytes(
 53            object? value,
 54            Type inputType,
 55            JsonSerializerOptions? options = null)
 056        {
 057            ValidateInputType(value, inputType);
 058            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 059            return WriteBytesAsObject(value, jsonTypeInfo);
 060        }
 61
 62        /// <summary>
 63        /// Converts the provided value into a <see cref="byte"/> array.
 64        /// </summary>
 65        /// <returns>A UTF-8 representation of the value.</returns>
 66        /// <param name="value">The value to convert.</param>
 67        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 68        /// <exception cref="ArgumentNullException">
 69        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 70        /// </exception>
 71        public static byte[] SerializeToUtf8Bytes<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 072        {
 073            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 74
 075            jsonTypeInfo.EnsureConfigured();
 076            return WriteBytes(value, jsonTypeInfo);
 077        }
 78
 79        /// <summary>
 80        /// Converts the provided value into a <see cref="byte"/> array.
 81        /// </summary>
 82        /// <returns>A UTF-8 representation of the value.</returns>
 83        /// <param name="value">The value to convert.</param>
 84        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 85        /// <exception cref="ArgumentNullException">
 86        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 87        /// </exception>
 88        /// <exception cref="InvalidCastException">
 89        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 90        /// </exception>
 91        public static byte[] SerializeToUtf8Bytes(object? value, JsonTypeInfo jsonTypeInfo)
 092        {
 093            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 94
 095            jsonTypeInfo.EnsureConfigured();
 096            return WriteBytesAsObject(value, jsonTypeInfo);
 097        }
 98
 99        /// <summary>
 100        /// Converts the provided value into a <see cref="byte"/> array.
 101        /// </summary>
 102        /// <returns>A UTF-8 representation of the value.</returns>
 103        /// <param name="value">The value to convert.</param>
 104        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 105        /// <param name="context">A metadata provider for serializable types.</param>
 106        /// <exception cref="ArgumentException">
 107        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 108        /// </exception>
 109        /// <exception cref="System.ArgumentNullException">
 110        /// <paramref name="inputType"/> is <see langword="null"/>.
 111        /// </exception>
 112        /// <exception cref="NotSupportedException">
 113        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 114        /// for <paramref name="inputType"/>  or its serializable members.
 115        /// </exception>
 116        /// <exception cref="InvalidOperationException">
 117        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 118        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 119        /// </exception>
 120        public static byte[] SerializeToUtf8Bytes(object? value, Type inputType, JsonSerializerContext context)
 0121        {
 0122            ArgumentNullException.ThrowIfNull(context);
 123
 0124            ValidateInputType(value, inputType);
 0125            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 0126            return WriteBytesAsObject(value, jsonTypeInfo);
 0127        }
 128
 129        private static byte[] WriteBytes<TValue>(in TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 0130        {
 0131            Debug.Assert(jsonTypeInfo.IsConfigured);
 132
 0133            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 134
 135            try
 0136            {
 0137                jsonTypeInfo.Serialize(writer, value);
 0138                return output.WrittenSpan.ToArray();
 139            }
 140            finally
 0141            {
 0142                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0143            }
 0144        }
 145
 146        private static byte[] WriteBytesAsObject(object? value, JsonTypeInfo jsonTypeInfo)
 0147        {
 0148            Debug.Assert(jsonTypeInfo.IsConfigured);
 149
 0150            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 151
 152            try
 0153            {
 0154                jsonTypeInfo.SerializeAsObject(writer, value);
 0155                return output.WrittenSpan.ToArray();
 156            }
 157            finally
 0158            {
 0159                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0160            }
 0161        }
 162    }
 163}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Document.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json
 10{
 11    public static partial class JsonSerializer
 12    {
 13        /// <summary>
 14        /// Converts the provided value into a <see cref="JsonDocument"/>.
 15        /// </summary>
 16        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 17        /// <returns>A <see cref="JsonDocument"/> representation of the JSON value.</returns>
 18        /// <param name="value">The value to convert.</param>
 19        /// <param name="options">Options to control the conversion behavior.</param>
 20        /// <exception cref="NotSupportedException">
 21        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 22        /// for <typeparamref name="TValue"/> or its serializable members.
 23        /// </exception>
 24        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 25        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 26        public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonSerializerOptions? options = null)
 027        {
 028            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 029            return WriteDocument(value, jsonTypeInfo);
 030        }
 31
 32        /// <summary>
 33        /// Converts the provided value into a <see cref="JsonDocument"/>.
 34        /// </summary>
 35        /// <returns>A <see cref="JsonDocument"/> representation of the value.</returns>
 36        /// <param name="value">The value to convert.</param>
 37        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 38        /// <param name="options">Options to control the conversion behavior.</param>
 39        /// <exception cref="ArgumentException">
 40        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 41        /// </exception>
 42        /// <exception cref="NotSupportedException">
 43        /// <exception cref="ArgumentNullException">
 44        /// <paramref name="inputType"/> is <see langword="null"/>.
 45        /// </exception>
 46        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 47        /// for <paramref name="inputType"/>  or its serializable members.
 48        /// </exception>
 49        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 50        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 51        public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerOptions? options = n
 052        {
 053            ValidateInputType(value, inputType);
 054            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 055            return WriteDocumentAsObject(value, jsonTypeInfo);
 056        }
 57
 58        /// <summary>
 59        /// Converts the provided value into a <see cref="JsonDocument"/>.
 60        /// </summary>
 61        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 62        /// <returns>A <see cref="JsonDocument"/> representation of the value.</returns>
 63        /// <param name="value">The value to convert.</param>
 64        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 65        /// <exception cref="ArgumentNullException">
 66        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 67        /// </exception>
 68        public static JsonDocument SerializeToDocument<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 069        {
 070            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 71
 072            jsonTypeInfo.EnsureConfigured();
 073            return WriteDocument(value, jsonTypeInfo);
 074        }
 75
 76        /// <summary>
 77        /// Converts the provided value into a <see cref="JsonDocument"/>.
 78        /// </summary>
 79        /// <returns>A <see cref="JsonDocument"/> representation of the value.</returns>
 80        /// <param name="value">The value to convert.</param>
 81        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 82        /// <exception cref="ArgumentNullException">
 83        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 84        /// </exception>
 85        /// <exception cref="InvalidCastException">
 86        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 87        /// </exception>
 88        public static JsonDocument SerializeToDocument(object? value, JsonTypeInfo jsonTypeInfo)
 089        {
 090            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 91
 092            jsonTypeInfo.EnsureConfigured();
 093            return WriteDocumentAsObject(value, jsonTypeInfo);
 094        }
 95
 96        /// <summary>
 97        /// Converts the provided value into a <see cref="JsonDocument"/>.
 98        /// </summary>
 99        /// <returns>A <see cref="JsonDocument"/> representation of the value.</returns>
 100        /// <param name="value">The value to convert.</param>
 101        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 102        /// <param name="context">A metadata provider for serializable types.</param>
 103        /// <exception cref="NotSupportedException">
 104        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 105        /// for <paramref name="inputType"/> or its serializable members.
 106        /// </exception>
 107        /// <exception cref="InvalidOperationException">
 108        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 109        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 110        /// </exception>
 111        /// <exception cref="ArgumentNullException">
 112        /// <paramref name="inputType"/> or <paramref name="context"/> is <see langword="null"/>.
 113        /// </exception>
 114        public static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerContext context)
 0115        {
 0116            ArgumentNullException.ThrowIfNull(context);
 117
 0118            ValidateInputType(value, inputType);
 0119            return WriteDocumentAsObject(value, GetTypeInfo(context, inputType));
 0120        }
 121
 122        private static JsonDocument WriteDocument<TValue>(in TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 0123        {
 0124            Debug.Assert(jsonTypeInfo.IsConfigured);
 0125            JsonSerializerOptions options = jsonTypeInfo.Options;
 126
 127            // For performance, share the same buffer across serialization and deserialization.
 128            // The PooledByteBufferWriter is cleared and returned when JsonDocument.Dispose() is called.
 0129            PooledByteBufferWriter output = new(options.DefaultBufferSize);
 0130            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriter(options, output);
 131
 132            try
 0133            {
 0134                jsonTypeInfo.Serialize(writer, value);
 0135                return JsonDocument.ParseRented(output, options.GetDocumentOptions());
 136            }
 137            finally
 0138            {
 0139                Utf8JsonWriterCache.ReturnWriter(writer);
 0140            }
 0141        }
 142
 143        private static JsonDocument WriteDocumentAsObject(object? value, JsonTypeInfo jsonTypeInfo)
 0144        {
 0145            Debug.Assert(jsonTypeInfo.IsConfigured);
 0146            JsonSerializerOptions options = jsonTypeInfo.Options;
 147
 148            // For performance, share the same buffer across serialization and deserialization.
 149            // The PooledByteBufferWriter is cleared and returned when JsonDocument.Dispose() is called.
 0150            PooledByteBufferWriter output = new(options.DefaultBufferSize);
 0151            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriter(options, output);
 152
 153            try
 0154            {
 0155                jsonTypeInfo.SerializeAsObject(writer, value);
 0156                return JsonDocument.ParseRented(output, options.GetDocumentOptions());
 157            }
 158            finally
 0159            {
 0160                Utf8JsonWriterCache.ReturnWriter(writer);
 0161            }
 0162        }
 163    }
 164}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Element.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json
 10{
 11    public static partial class JsonSerializer
 12    {
 13        /// <summary>
 14        /// Converts the provided value into a <see cref="JsonElement"/>.
 15        /// </summary>
 16        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 17        /// <returns>A <see cref="JsonElement"/> representation of the JSON value.</returns>
 18        /// <param name="value">The value to convert.</param>
 19        /// <param name="options">Options to control the conversion behavior.</param>
 20        /// <exception cref="NotSupportedException">
 21        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 22        /// for <typeparamref name="TValue"/> or its serializable members.
 23        /// </exception>
 24        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 25        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 26        public static JsonElement SerializeToElement<TValue>(TValue value, JsonSerializerOptions? options = null)
 027        {
 028            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 029            return WriteElement(value, jsonTypeInfo);
 030        }
 31
 32        /// <summary>
 33        /// Converts the provided value into a <see cref="JsonElement"/>.
 34        /// </summary>
 35        /// <returns>A <see cref="JsonElement"/> representation of the value.</returns>
 36        /// <param name="value">The value to convert.</param>
 37        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 38        /// <param name="options">Options to control the conversion behavior.</param>
 39        /// <exception cref="ArgumentException">
 40        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 41        /// </exception>
 42        /// <exception cref="NotSupportedException">
 43        /// <exception cref="ArgumentNullException">
 44        /// <paramref name="inputType"/> is <see langword="null"/>.
 45        /// </exception>
 46        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 47        /// for <paramref name="inputType"/>  or its serializable members.
 48        /// </exception>
 49        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 50        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 51        public static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerOptions? options = nul
 052        {
 053            ValidateInputType(value, inputType);
 054            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 055            return WriteElementAsObject(value, jsonTypeInfo);
 056        }
 57
 58        /// <summary>
 59        /// Converts the provided value into a <see cref="JsonElement"/>.
 60        /// </summary>
 61        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 62        /// <returns>A <see cref="JsonElement"/> representation of the value.</returns>
 63        /// <param name="value">The value to convert.</param>
 64        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 65        /// <exception cref="ArgumentNullException">
 66        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 67        /// </exception>
 68        public static JsonElement SerializeToElement<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 069        {
 070            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 71
 072            jsonTypeInfo.EnsureConfigured();
 073            return WriteElement(value, jsonTypeInfo);
 074        }
 75
 76        /// <summary>
 77        /// Converts the provided value into a <see cref="JsonElement"/>.
 78        /// </summary>
 79        /// <returns>A <see cref="JsonElement"/> representation of the value.</returns>
 80        /// <param name="value">The value to convert.</param>
 81        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 82        /// <exception cref="ArgumentNullException">
 83        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 84        /// </exception>
 85        /// <exception cref="InvalidCastException">
 86        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 87        /// </exception>
 88        public static JsonElement SerializeToElement(object? value, JsonTypeInfo jsonTypeInfo)
 089        {
 090            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 91
 092            jsonTypeInfo.EnsureConfigured();
 093            return WriteElementAsObject(value, jsonTypeInfo);
 094        }
 95
 96        /// <summary>
 97        /// Converts the provided value into a <see cref="JsonElement"/>.
 98        /// </summary>
 99        /// <returns>A <see cref="JsonElement"/> representation of the value.</returns>
 100        /// <param name="value">The value to convert.</param>
 101        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 102        /// <param name="context">A metadata provider for serializable types.</param>
 103        /// <exception cref="NotSupportedException">
 104        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 105        /// for <paramref name="inputType"/> or its serializable members.
 106        /// </exception>
 107        /// <exception cref="InvalidOperationException">
 108        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 109        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 110        /// </exception>
 111        /// <exception cref="ArgumentNullException">
 112        /// <paramref name="inputType"/> or <paramref name="context"/> is <see langword="null"/>.
 113        /// </exception>
 114        public static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerContext context)
 0115        {
 0116            ArgumentNullException.ThrowIfNull(context);
 117
 0118            ValidateInputType(value, inputType);
 0119            JsonTypeInfo typeInfo = GetTypeInfo(context, inputType);
 0120            return WriteElementAsObject(value, typeInfo);
 0121        }
 122
 123        private static JsonElement WriteElement<TValue>(in TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 0124        {
 0125            Debug.Assert(jsonTypeInfo.IsConfigured);
 0126            JsonSerializerOptions options = jsonTypeInfo.Options;
 127
 0128            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 129
 130            try
 0131            {
 0132                jsonTypeInfo.Serialize(writer, value);
 0133                return JsonElement.Parse(output.WrittenSpan, options.GetDocumentOptions());
 134            }
 135            finally
 0136            {
 0137                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0138            }
 0139        }
 140
 141        private static JsonElement WriteElementAsObject(object? value, JsonTypeInfo jsonTypeInfo)
 0142        {
 0143            JsonSerializerOptions options = jsonTypeInfo.Options;
 0144            Debug.Assert(options != null);
 145
 0146            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 147
 148            try
 0149            {
 0150                jsonTypeInfo.SerializeAsObject(writer, value);
 0151                return JsonElement.Parse(output.WrittenSpan, options.GetDocumentOptions());
 152            }
 153            finally
 0154            {
 0155                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0156            }
 0157        }
 158    }
 159}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.HandleMetadata.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Text.Json.Serialization;
 6
 7namespace System.Text.Json
 8{
 9    public static partial class JsonSerializer
 10    {
 11        // Pre-encoded metadata properties.
 112        internal static readonly JsonEncodedText s_metadataId = JsonEncodedText.Encode(IdPropertyName, encoder: null);
 113        internal static readonly JsonEncodedText s_metadataRef = JsonEncodedText.Encode(RefPropertyName, encoder: null);
 114        internal static readonly JsonEncodedText s_metadataType = JsonEncodedText.Encode(TypePropertyName, encoder: null
 115        internal static readonly JsonEncodedText s_metadataValues = JsonEncodedText.Encode(ValuesPropertyName, encoder: 
 16
 17        internal static MetadataPropertyName WriteMetadataForObject(
 18            JsonConverter jsonConverter,
 19            ref WriteStack state,
 20            Utf8JsonWriter writer)
 021        {
 022            Debug.Assert(jsonConverter.CanHaveMetadata);
 023            Debug.Assert(!state.IsContinuation);
 024            Debug.Assert(state.CurrentContainsMetadata);
 25
 026            MetadataPropertyName writtenMetadata = MetadataPropertyName.None;
 27
 028            if (state.NewReferenceId != null)
 029            {
 030                writer.WriteString(s_metadataId, state.NewReferenceId);
 031                writtenMetadata |= MetadataPropertyName.Id;
 032                state.NewReferenceId = null;
 033            }
 34
 035            if (state.PolymorphicTypeDiscriminator is object discriminator)
 036            {
 037                Debug.Assert(state.PolymorphicTypeResolver != null);
 38
 039                JsonEncodedText propertyName =
 040                    state.PolymorphicTypeResolver.CustomTypeDiscriminatorPropertyNameJsonEncoded is JsonEncodedText cust
 041                    ? customPropertyName
 042                    : s_metadataType;
 43
 044                if (discriminator is string stringId)
 045                {
 046                    writer.WriteString(propertyName, stringId);
 047                }
 48                else
 049                {
 050                    Debug.Assert(discriminator is int);
 051                    writer.WriteNumber(propertyName, (int)discriminator);
 052                }
 53
 054                writtenMetadata |= MetadataPropertyName.Type;
 055                state.PolymorphicTypeDiscriminator = null;
 056            }
 57
 058            Debug.Assert(writtenMetadata != MetadataPropertyName.None);
 059            return writtenMetadata;
 060        }
 61
 62        internal static MetadataPropertyName WriteMetadataForCollection(
 63            JsonConverter jsonConverter,
 64            ref WriteStack state,
 65            Utf8JsonWriter writer)
 066        {
 67            // For collections with metadata, we nest the array payload within a JSON object.
 068            writer.WriteStartObject();
 069            MetadataPropertyName writtenMetadata = WriteMetadataForObject(jsonConverter, ref state, writer);
 070            writer.WritePropertyName(s_metadataValues); // property name containing nested array values.
 071            return writtenMetadata;
 072        }
 73
 74        /// <summary>
 75        /// Compute reference id for the next value to be serialized.
 76        /// </summary>
 77        internal static bool TryGetReferenceForValue(object currentValue, ref WriteStack state, Utf8JsonWriter writer)
 078        {
 079            Debug.Assert(state.NewReferenceId == null);
 80
 081            string referenceId = state.ReferenceResolver.GetReference(currentValue, out bool alreadyExists);
 082            Debug.Assert(referenceId != null);
 83
 084            if (alreadyExists)
 085            {
 86                // Instance already serialized, write as { "$ref" : "referenceId" }
 087                writer.WriteStartObject();
 088                writer.WriteString(s_metadataRef, referenceId);
 089                writer.WriteEndObject();
 90
 91                // clear out any polymorphism state.
 092                state.PolymorphicTypeDiscriminator = null;
 093                state.PolymorphicTypeResolver = null;
 094            }
 95            else
 096            {
 97                // New instance, store computed reference id in the state
 098                state.NewReferenceId = referenceId;
 099            }
 100
 0101            return alreadyExists;
 0102        }
 103    }
 104}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Node.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Nodes;
 7using System.Text.Json.Serialization;
 8using System.Text.Json.Serialization.Metadata;
 9
 10namespace System.Text.Json
 11{
 12    public static partial class JsonSerializer
 13    {
 14        /// <summary>
 15        /// Converts the provided value into a <see cref="JsonNode"/>.
 16        /// </summary>
 17        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 18        /// <returns>A <see cref="JsonNode"/> representation of the JSON value.</returns>
 19        /// <param name="value">The value to convert.</param>
 20        /// <param name="options">Options to control the conversion behavior.</param>
 21        /// <exception cref="NotSupportedException">
 22        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 23        /// for <typeparamref name="TValue"/> or its serializable members.
 24        /// </exception>
 25        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 26        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 27        public static JsonNode? SerializeToNode<TValue>(TValue value, JsonSerializerOptions? options = null)
 028        {
 029            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 030            return WriteNode(value, jsonTypeInfo);
 031        }
 32
 33        /// <summary>
 34        /// Converts the provided value into a <see cref="JsonNode"/>.
 35        /// </summary>
 36        /// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
 37        /// <param name="value">The value to convert.</param>
 38        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 39        /// <param name="options">Options to control the conversion behavior.</param>
 40        /// <exception cref="ArgumentException">
 41        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 42        /// </exception>
 43        /// <exception cref="NotSupportedException">
 44        /// <exception cref="ArgumentNullException">
 45        /// <paramref name="inputType"/> is <see langword="null"/>.
 46        /// </exception>
 47        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 48        /// for <paramref name="inputType"/>  or its serializable members.
 49        /// </exception>
 50        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 51        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 52        public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerOptions? options = null)
 053        {
 054            ValidateInputType(value, inputType);
 055            JsonTypeInfo typeInfo = GetTypeInfo(options, inputType);
 056            return WriteNodeAsObject(value, typeInfo);
 057        }
 58
 59        /// <summary>
 60        /// Converts the provided value into a <see cref="JsonNode"/>.
 61        /// </summary>
 62        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 63        /// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
 64        /// <param name="value">The value to convert.</param>
 65        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 66        /// <exception cref="ArgumentNullException">
 67        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 68        /// </exception>
 69        public static JsonNode? SerializeToNode<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 070        {
 071            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 72
 073            jsonTypeInfo.EnsureConfigured();
 074            return WriteNode(value, jsonTypeInfo);
 075        }
 76
 77        /// <summary>
 78        /// Converts the provided value into a <see cref="JsonNode"/>.
 79        /// </summary>
 80        /// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
 81        /// <param name="value">The value to convert.</param>
 82        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 83        /// <exception cref="ArgumentNullException">
 84        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 85        /// </exception>
 86        /// <exception cref="InvalidCastException">
 87        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 88        /// </exception>
 89        public static JsonNode? SerializeToNode(object? value, JsonTypeInfo jsonTypeInfo)
 090        {
 091            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 92
 093            jsonTypeInfo.EnsureConfigured();
 094            return WriteNodeAsObject(value, jsonTypeInfo);
 095        }
 96
 97        /// <summary>
 98        /// Converts the provided value into a <see cref="JsonNode"/>.
 99        /// </summary>
 100        /// <returns>A <see cref="JsonNode"/> representation of the value.</returns>
 101        /// <param name="value">The value to convert.</param>
 102        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 103        /// <param name="context">A metadata provider for serializable types.</param>
 104        /// <exception cref="NotSupportedException">
 105        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 106        /// for <paramref name="inputType"/> or its serializable members.
 107        /// </exception>
 108        /// <exception cref="InvalidOperationException">
 109        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 110        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 111        /// </exception>
 112        /// <exception cref="ArgumentNullException">
 113        /// <paramref name="inputType"/> or <paramref name="context"/> is <see langword="null"/>.
 114        /// </exception>
 115        public static JsonNode? SerializeToNode(object? value, Type inputType, JsonSerializerContext context)
 0116        {
 0117            ArgumentNullException.ThrowIfNull(context);
 118
 0119            ValidateInputType(value, inputType);
 0120            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 0121            return WriteNodeAsObject(value, jsonTypeInfo);
 0122        }
 123
 124        private static JsonNode? WriteNode<TValue>(in TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 0125        {
 0126            Debug.Assert(jsonTypeInfo.IsConfigured);
 0127            JsonSerializerOptions options = jsonTypeInfo.Options;
 128
 0129            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 130
 131            try
 0132            {
 0133                jsonTypeInfo.Serialize(writer, value);
 0134                return JsonNode.Parse(output.WrittenSpan, options.GetNodeOptions(), options.GetDocumentOptions());
 135            }
 136            finally
 0137            {
 0138                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0139            }
 0140        }
 141
 142        private static JsonNode? WriteNodeAsObject(object? value, JsonTypeInfo jsonTypeInfo)
 0143        {
 0144            Debug.Assert(jsonTypeInfo.IsConfigured);
 0145            JsonSerializerOptions options = jsonTypeInfo.Options;
 146
 0147            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 148
 149            try
 0150            {
 0151                jsonTypeInfo.SerializeAsObject(writer, value);
 0152                return JsonNode.Parse(output.WrittenSpan, options.GetNodeOptions(), options.GetDocumentOptions());
 153            }
 154            finally
 0155            {
 0156                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0157            }
 0158        }
 159    }
 160}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Pipe.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.IO.Pipelines;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace System.Text.Json
 12{
 13    public static partial class JsonSerializer
 14    {
 15        /// <summary>
 16        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.Pi
 17        /// </summary>
 18        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 19        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param>
 20        /// <param name="value">The value to convert.</param>
 21        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 22        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 23        /// <returns>A task that represents the asynchronous write operation.</returns>
 24        /// <exception cref="ArgumentNullException">
 25        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 26        /// </exception>
 27        public static Task SerializeAsync<TValue>(
 28            PipeWriter utf8Json,
 29            TValue value,
 30            JsonTypeInfo<TValue> jsonTypeInfo,
 31            CancellationToken cancellationToken = default)
 032        {
 033            ArgumentNullException.ThrowIfNull(utf8Json);
 034            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 35
 036            jsonTypeInfo.EnsureConfigured();
 037            return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken);
 038        }
 39
 40        /// <summary>
 41        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.Pi
 42        /// </summary>
 43        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 44        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param>
 45        /// <param name="value">The value to convert.</param>
 46        /// <param name="options">Options to control the conversion behavior.</param>
 47        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 48        /// <returns>A task that represents the asynchronous write operation.</returns>
 49        /// <exception cref="ArgumentNullException">
 50        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 51        /// </exception>
 52        /// <exception cref="NotSupportedException">
 53        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 54        /// for <typeparamref name="TValue"/> or its serializable members.
 55        /// </exception>
 56        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 57        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 58        public static Task SerializeAsync<TValue>(
 59            PipeWriter utf8Json,
 60            TValue value,
 61            JsonSerializerOptions? options = null,
 62            CancellationToken cancellationToken = default)
 063        {
 064            ArgumentNullException.ThrowIfNull(utf8Json);
 65
 066            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 067            return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken);
 068        }
 69
 70        /// <summary>
 71        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.Pi
 72        /// </summary>
 73        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param>
 74        /// <param name="value">The value to convert.</param>
 75        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 76        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 77        /// <returns>A task that represents the asynchronous write operation.</returns>
 78        /// <exception cref="ArgumentNullException">
 79        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 80        /// </exception>
 81        /// <exception cref="InvalidCastException">
 82        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 83        /// </exception>
 84        public static Task SerializeAsync(
 85            PipeWriter utf8Json,
 86            object? value,
 87            JsonTypeInfo jsonTypeInfo,
 88            CancellationToken cancellationToken = default)
 089        {
 090            ArgumentNullException.ThrowIfNull(utf8Json);
 091            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 92
 093            jsonTypeInfo.EnsureConfigured();
 094            return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken);
 095        }
 96
 97        /// <summary>
 98        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.Pi
 99        /// </summary>
 100        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param>
 101        /// <param name="value">The value to convert.</param>
 102        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 103        /// <param name="context">A metadata provider for serializable types.</param>
 104        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 105        /// <returns>A task that represents the asynchronous write operation.</returns>
 106        /// <exception cref="ArgumentException">
 107        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 108        /// </exception>
 109        /// <exception cref="ArgumentNullException">
 110        /// <paramref name="utf8Json"/>, <paramref name="inputType"/>, or <paramref name="context"/> is <see langword="n
 111        /// </exception>
 112        /// <exception cref="NotSupportedException">
 113        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 114        /// for <paramref name="inputType"/>  or its serializable members.
 115        /// </exception>
 116        public static Task SerializeAsync(
 117                PipeWriter utf8Json,
 118                object? value,
 119                Type inputType,
 120                JsonSerializerContext context,
 121                CancellationToken cancellationToken = default)
 0122        {
 0123            ArgumentNullException.ThrowIfNull(utf8Json);
 0124            ArgumentNullException.ThrowIfNull(context);
 125
 0126            ValidateInputType(value, inputType);
 0127            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 128
 0129            return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken);
 0130        }
 131
 132        /// <summary>
 133        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.Pi
 134        /// </summary>
 135        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param>
 136        /// <param name="value">The value to convert.</param>
 137        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 138        /// <param name="options">Options to control the conversion behavior.</param>
 139        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 140        /// <returns>A task that represents the asynchronous write operation.</returns>
 141        /// <exception cref="ArgumentException">
 142        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 143        /// </exception>
 144        /// <exception cref="ArgumentNullException">
 145        /// <paramref name="utf8Json"/> or <paramref name="inputType"/> is <see langword="null"/>.
 146        /// </exception>
 147        /// <exception cref="NotSupportedException">
 148        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 149        /// for <paramref name="inputType"/>  or its serializable members.
 150        /// </exception>
 151        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 152        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 153        public static Task SerializeAsync(
 154                PipeWriter utf8Json,
 155                object? value,
 156                Type inputType,
 157                JsonSerializerOptions? options = null,
 158                CancellationToken cancellationToken = default)
 0159        {
 0160            ArgumentNullException.ThrowIfNull(utf8Json);
 161
 0162            ValidateInputType(value, inputType);
 0163            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 164
 0165            return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken);
 0166        }
 167    }
 168}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Stream.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.IO;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace System.Text.Json
 12{
 13    public static partial class JsonSerializer
 14    {
 15        // We flush the Stream when the buffer is >=90% of capacity.
 16        // This threshold is a compromise between buffer utilization and minimizing cases where the buffer
 17        // needs to be expanded\doubled because it is not large enough to write the current property or element.
 18        // We check for flush after each JSON property and element is written to the buffer.
 19        // Once the buffer is expanded to contain the largest single element\property, a 90% threshold
 20        // means the buffer may be expanded a maximum of 4 times: 1-(1/(2^4))==.9375.
 21        internal const float FlushThreshold = .90f;
 22
 23        /// <summary>
 24        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 25        /// </summary>
 26        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 27        /// <returns>A task that represents the asynchronous write operation.</returns>
 28        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 29        /// <param name="value">The value to convert.</param>
 30        /// <param name="options">Options to control the conversion behavior.</param>
 31        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 32        /// <exception cref="ArgumentNullException">
 33        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 34        /// </exception>
 35        /// <exception cref="NotSupportedException">
 36        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 37        /// for <typeparamref name="TValue"/> or its serializable members.
 38        /// </exception>
 39        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 40        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 41        public static Task SerializeAsync<TValue>(
 42            Stream utf8Json,
 43            TValue value,
 44            JsonSerializerOptions? options = null,
 45            CancellationToken cancellationToken = default)
 046        {
 047            ArgumentNullException.ThrowIfNull(utf8Json);
 48
 049            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 050            return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken);
 051        }
 52
 53        /// <summary>
 54        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 55        /// </summary>
 56        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 57        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 58        /// <param name="value">The value to convert.</param>
 59        /// <param name="options">Options to control the conversion behavior.</param>
 60        /// <exception cref="ArgumentNullException">
 61        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 62        /// </exception>
 63        /// <exception cref="NotSupportedException">
 64        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 65        /// for <typeparamref name="TValue"/> or its serializable members.
 66        /// </exception>
 67        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 68        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 69        public static void Serialize<TValue>(
 70            Stream utf8Json,
 71            TValue value,
 72            JsonSerializerOptions? options = null)
 073        {
 074            ArgumentNullException.ThrowIfNull(utf8Json);
 75
 076            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 077            jsonTypeInfo.Serialize(utf8Json, value);
 078        }
 79
 80        /// <summary>
 81        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 82        /// </summary>
 83        /// <returns>A task that represents the asynchronous write operation.</returns>
 84        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 85        /// <param name="value">The value to convert.</param>
 86        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 87        /// <param name="options">Options to control the conversion behavior.</param>
 88        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 89        /// <exception cref="ArgumentException">
 90        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 91        /// </exception>
 92        /// <exception cref="ArgumentNullException">
 93        /// <paramref name="utf8Json"/> or <paramref name="inputType"/> is <see langword="null"/>.
 94        /// </exception>
 95        /// <exception cref="NotSupportedException">
 96        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 97        /// for <paramref name="inputType"/>  or its serializable members.
 98        /// </exception>
 99        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 100        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 101        public static Task SerializeAsync(
 102            Stream utf8Json,
 103            object? value,
 104            Type inputType,
 105            JsonSerializerOptions? options = null,
 106            CancellationToken cancellationToken = default)
 0107        {
 0108            ArgumentNullException.ThrowIfNull(utf8Json);
 109
 0110            ValidateInputType(value, inputType);
 0111            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 0112            return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken);
 0113        }
 114
 115        /// <summary>
 116        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 117        /// </summary>
 118        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 119        /// <param name="value">The value to convert.</param>
 120        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 121        /// <param name="options">Options to control the conversion behavior.</param>
 122        /// <exception cref="ArgumentException">
 123        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 124        /// </exception>
 125        /// <exception cref="ArgumentNullException">
 126        /// <paramref name="utf8Json"/> or <paramref name="inputType"/> is <see langword="null"/>.
 127        /// </exception>
 128        /// <exception cref="NotSupportedException">
 129        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 130        /// for <paramref name="inputType"/>  or its serializable members.
 131        /// </exception>
 132        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 133        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 134        public static void Serialize(
 135            Stream utf8Json,
 136            object? value,
 137            Type inputType,
 138            JsonSerializerOptions? options = null)
 0139        {
 0140            ArgumentNullException.ThrowIfNull(utf8Json);
 141
 0142            ValidateInputType(value, inputType);
 0143            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 0144            jsonTypeInfo.SerializeAsObject(utf8Json, value);
 0145        }
 146
 147        /// <summary>
 148        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 149        /// </summary>
 150        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 151        /// <returns>A task that represents the asynchronous write operation.</returns>
 152        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 153        /// <param name="value">The value to convert.</param>
 154        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 155        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 156        /// <exception cref="ArgumentNullException">
 157        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 158        /// </exception>
 159        public static Task SerializeAsync<TValue>(
 160            Stream utf8Json,
 161            TValue value,
 162            JsonTypeInfo<TValue> jsonTypeInfo,
 163            CancellationToken cancellationToken = default)
 0164        {
 0165            ArgumentNullException.ThrowIfNull(utf8Json);
 0166            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 167
 0168            jsonTypeInfo.EnsureConfigured();
 0169            return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken);
 0170        }
 171
 172        /// <summary>
 173        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 174        /// </summary>
 175        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 176        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 177        /// <param name="value">The value to convert.</param>
 178        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 179        /// <exception cref="ArgumentNullException">
 180        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 181        /// </exception>
 182        public static void Serialize<TValue>(
 183            Stream utf8Json,
 184            TValue value,
 185            JsonTypeInfo<TValue> jsonTypeInfo)
 0186        {
 0187            ArgumentNullException.ThrowIfNull(utf8Json);
 0188            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 189
 0190            jsonTypeInfo.EnsureConfigured();
 0191            jsonTypeInfo.Serialize(utf8Json, value);
 0192        }
 193
 194        /// <summary>
 195        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 196        /// </summary>
 197        /// <returns>A task that represents the asynchronous write operation.</returns>
 198        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 199        /// <param name="value">The value to convert.</param>
 200        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 201        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 202        /// <exception cref="ArgumentNullException">
 203        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 204        /// </exception>
 205        /// <exception cref="InvalidCastException">
 206        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 207        /// </exception>
 208        public static Task SerializeAsync(
 209            Stream utf8Json,
 210            object? value,
 211            JsonTypeInfo jsonTypeInfo,
 212            CancellationToken cancellationToken = default)
 0213        {
 0214            ArgumentNullException.ThrowIfNull(utf8Json);
 0215            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 216
 0217            jsonTypeInfo.EnsureConfigured();
 0218            return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken);
 0219        }
 220
 221        /// <summary>
 222        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 223        /// </summary>
 224        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 225        /// <param name="value">The value to convert.</param>
 226        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 227        /// <exception cref="ArgumentNullException">
 228        /// <paramref name="utf8Json"/> is <see langword="null"/>.
 229        /// </exception>
 230        /// <exception cref="InvalidCastException">
 231        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 232        /// </exception>
 233        public static void Serialize(
 234            Stream utf8Json,
 235            object? value,
 236            JsonTypeInfo jsonTypeInfo)
 0237        {
 0238            ArgumentNullException.ThrowIfNull(utf8Json);
 0239            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 240
 0241            jsonTypeInfo.EnsureConfigured();
 0242            jsonTypeInfo.SerializeAsObject(utf8Json, value);
 0243        }
 244
 245        /// <summary>
 246        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 247        /// </summary>
 248        /// <returns>A task that represents the asynchronous write operation.</returns>
 249        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 250        /// <param name="value">The value to convert.</param>
 251        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 252        /// <param name="context">A metadata provider for serializable types.</param>
 253        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can
 254        /// <exception cref="ArgumentException">
 255        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 256        /// </exception>
 257        /// <exception cref="ArgumentNullException">
 258        /// <paramref name="utf8Json"/>, <paramref name="inputType"/>, or <paramref name="context"/> is <see langword="n
 259        /// </exception>
 260        /// <exception cref="NotSupportedException">
 261        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 262        /// for <paramref name="inputType"/>  or its serializable members.
 263        /// </exception>
 264        public static Task SerializeAsync(
 265            Stream utf8Json,
 266            object? value,
 267            Type inputType,
 268            JsonSerializerContext context,
 269            CancellationToken cancellationToken = default)
 0270        {
 0271            ArgumentNullException.ThrowIfNull(utf8Json);
 0272            ArgumentNullException.ThrowIfNull(context);
 273
 0274            ValidateInputType(value, inputType);
 0275            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 0276            return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken);
 0277        }
 278
 279        /// <summary>
 280        /// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>.
 281        /// </summary>
 282        /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param>
 283        /// <param name="value">The value to convert.</param>
 284        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 285        /// <param name="context">A metadata provider for serializable types.</param>
 286        /// <exception cref="ArgumentException">
 287        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 288        /// </exception>
 289        /// <exception cref="ArgumentNullException">
 290        /// <paramref name="utf8Json"/>, <paramref name="inputType"/>, or <paramref name="context"/> is <see langword="n
 291        /// </exception>
 292        /// <exception cref="NotSupportedException">
 293        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 294        /// for <paramref name="inputType"/>  or its serializable members.
 295        /// </exception>
 296        public static void Serialize(
 297            Stream utf8Json,
 298            object? value,
 299            Type inputType,
 300            JsonSerializerContext context)
 0301        {
 0302            ArgumentNullException.ThrowIfNull(utf8Json);
 0303            ArgumentNullException.ThrowIfNull(context);
 304
 0305            ValidateInputType(value, inputType);
 0306            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 0307            jsonTypeInfo.SerializeAsObject(utf8Json, value);
 0308        }
 309    }
 310}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.String.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Serialization;
 7using System.Text.Json.Serialization.Metadata;
 8
 9namespace System.Text.Json
 10{
 11    public static partial class JsonSerializer
 12    {
 13        /// <summary>
 14        /// Converts the provided value into a <see cref="string"/>.
 15        /// </summary>
 16        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 17        /// <returns>A <see cref="string"/> representation of the value.</returns>
 18        /// <param name="value">The value to convert.</param>
 19        /// <param name="options">Options to control the conversion behavior.</param>
 20        /// <exception cref="NotSupportedException">
 21        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 22        /// for <typeparamref name="TValue"/> or its serializable members.
 23        /// </exception>
 24        /// <remarks>Using a <see cref="string"/> is not as efficient as using UTF-8
 25        /// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes{TValue}(TV
 26        /// and <see cref="SerializeAsync{TValue}(IO.Stream, TValue, JsonSerializerOptions?, Threading.CancellationToken
 27        /// </remarks>
 28        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 29        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 30        public static string Serialize<TValue>(TValue value, JsonSerializerOptions? options = null)
 031        {
 032            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 033            return WriteString(value, jsonTypeInfo);
 034        }
 35
 36        /// <summary>
 37        /// Converts the provided value into a <see cref="string"/>.
 38        /// </summary>
 39        /// <returns>A <see cref="string"/> representation of the value.</returns>
 40        /// <param name="value">The value to convert.</param>
 41        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 42        /// <param name="options">Options to control the conversion behavior.</param>
 43        /// <exception cref="ArgumentException">
 44        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 45        /// </exception>
 46        /// <exception cref="NotSupportedException">
 47        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 48        /// for <paramref name="inputType"/>  or its serializable members.
 49        /// </exception>
 50        /// <exception cref="ArgumentNullException">
 51        /// <paramref name="inputType"/> is <see langword="null"/>.
 52        /// </exception>
 53        /// <remarks>Using a <see cref="string"/> is not as efficient as using UTF-8
 54        /// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes(object?, T
 55        /// and <see cref="SerializeAsync(IO.Stream, object?, Type, JsonSerializerOptions?, Threading.CancellationToken)
 56        /// </remarks>
 57        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 58        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 59        public static string Serialize(
 60            object? value,
 61            Type inputType,
 62            JsonSerializerOptions? options = null)
 063        {
 064            ValidateInputType(value, inputType);
 065            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 066            return WriteStringAsObject(value, jsonTypeInfo);
 067        }
 68
 69        /// <summary>
 70        /// Converts the provided value into a <see cref="string"/>.
 71        /// </summary>
 72        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 73        /// <returns>A <see cref="string"/> representation of the value.</returns>
 74        /// <param name="value">The value to convert.</param>
 75        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 76        /// <exception cref="ArgumentNullException">
 77        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 78        /// </exception>
 79        /// <remarks>Using a <see cref="string"/> is not as efficient as using UTF-8
 80        /// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes{TValue}(TV
 81        /// and <see cref="SerializeAsync{TValue}(IO.Stream, TValue, JsonTypeInfo{TValue}, Threading.CancellationToken)"
 82        /// </remarks>
 83        public static string Serialize<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 084        {
 085            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 86
 087            jsonTypeInfo.EnsureConfigured();
 088            return WriteString(value, jsonTypeInfo);
 089        }
 90
 91        /// <summary>
 92        /// Converts the provided value into a <see cref="string"/>.
 93        /// </summary>
 94        /// <returns>A <see cref="string"/> representation of the value.</returns>
 95        /// <param name="value">The value to convert.</param>
 96        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 97        /// <exception cref="ArgumentNullException">
 98        /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 99        /// </exception>
 100        /// <exception cref="InvalidCastException">
 101        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 102        /// </exception>
 103        /// <remarks>Using a <see cref="string"/> is not as efficient as using UTF-8
 104        /// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes(object?, J
 105        /// and <see cref="SerializeAsync(IO.Stream, object?, JsonTypeInfo, Threading.CancellationToken)"/>.
 106        /// </remarks>
 107        public static string Serialize(object? value, JsonTypeInfo jsonTypeInfo)
 0108        {
 0109            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 110
 0111            jsonTypeInfo.EnsureConfigured();
 0112            return WriteStringAsObject(value, jsonTypeInfo);
 0113        }
 114
 115        /// <summary>
 116        /// Converts the provided value into a <see cref="string"/>.
 117        /// </summary>
 118        /// <returns>A <see cref="string"/> representation of the value.</returns>
 119        /// <param name="value">The value to convert.</param>
 120        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 121        /// <param name="context">A metadata provider for serializable types.</param>
 122        /// <exception cref="NotSupportedException">
 123        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 124        /// for <paramref name="inputType"/> or its serializable members.
 125        /// </exception>
 126        /// <exception cref="InvalidOperationException">
 127        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 128        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 129        /// </exception>
 130        /// <exception cref="ArgumentNullException">
 131        /// <paramref name="inputType"/> or <paramref name="context"/> is <see langword="null"/>.
 132        /// </exception>
 133        /// <remarks>Using a <see cref="string"/> is not as efficient as using UTF-8
 134        /// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes(object?, T
 135        /// and <see cref="SerializeAsync(IO.Stream, object?, Type, JsonSerializerContext, Threading.CancellationToken)"
 136        /// </remarks>
 137        public static string Serialize(object? value, Type inputType, JsonSerializerContext context)
 0138        {
 0139            ArgumentNullException.ThrowIfNull(context);
 140
 0141            ValidateInputType(value, inputType);
 0142            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 0143            return WriteStringAsObject(value, jsonTypeInfo);
 0144        }
 145
 146        private static string WriteString<TValue>(in TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 0147        {
 0148            Debug.Assert(jsonTypeInfo.IsConfigured);
 149
 0150            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 151
 152            try
 0153            {
 0154                jsonTypeInfo.Serialize(writer, value);
 0155                return JsonReaderHelper.TranscodeHelper(output.WrittenSpan);
 156            }
 157            finally
 0158            {
 0159                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0160            }
 0161        }
 162
 163        private static string WriteStringAsObject(object? value, JsonTypeInfo jsonTypeInfo)
 0164        {
 0165            Debug.Assert(jsonTypeInfo.IsConfigured);
 166
 0167            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out PooledByteBufferWr
 168
 169            try
 0170            {
 0171                jsonTypeInfo.SerializeAsObject(writer, value);
 0172                return JsonReaderHelper.TranscodeHelper(output.WrittenSpan);
 173            }
 174            finally
 0175            {
 0176                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 0177            }
 0178        }
 179    }
 180}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Write.Utf8JsonWriter.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.Text.Json.Serialization;
 6using System.Text.Json.Serialization.Metadata;
 7
 8namespace System.Text.Json
 9{
 10    public static partial class JsonSerializer
 11    {
 12        /// <summary>
 13        /// Writes one JSON value (including objects or arrays) to the provided writer.
 14        /// </summary>
 15        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 16        /// <param name="writer">The writer to write.</param>
 17        /// <param name="value">The value to convert and write.</param>
 18        /// <param name="options">Options to control the behavior.</param>
 19        /// <exception cref="ArgumentNullException">
 20        ///   <paramref name="writer"/> is <see langword="null"/>.
 21        /// </exception>
 22        /// <exception cref="NotSupportedException">
 23        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 24        /// for <typeparamref name="TValue"/> or its serializable members.
 25        /// </exception>
 26        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 27        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 28        public static void Serialize<TValue>(
 29            Utf8JsonWriter writer,
 30            TValue value,
 31            JsonSerializerOptions? options = null)
 032        {
 033            ArgumentNullException.ThrowIfNull(writer);
 34
 035            JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
 036            jsonTypeInfo.Serialize(writer, value);
 037        }
 38
 39        /// <summary>
 40        /// Writes one JSON value (including objects or arrays) to the provided writer.
 41        /// </summary>
 42        /// <param name="writer"></param>
 43        /// <param name="value">The value to convert and write.</param>
 44        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 45        /// <param name="options">Options to control the behavior.</param>
 46        /// <exception cref="ArgumentException">
 47        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 48        /// </exception>
 49        /// <exception cref="ArgumentNullException">
 50        /// <paramref name="writer"/> or <paramref name="inputType"/> is <see langword="null"/>.
 51        /// </exception>
 52        /// <exception cref="NotSupportedException">
 53        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 54        /// for <paramref name="inputType"/> or its serializable members.
 55        /// </exception>
 56        [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
 57        [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
 58        public static void Serialize(
 59            Utf8JsonWriter writer,
 60            object? value,
 61            Type inputType,
 62            JsonSerializerOptions? options = null)
 063        {
 064            ArgumentNullException.ThrowIfNull(writer);
 65
 066            ValidateInputType(value, inputType);
 067            JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType);
 068            jsonTypeInfo.SerializeAsObject(writer, value);
 069        }
 70
 71        /// <summary>
 72        /// Writes one JSON value (including objects or arrays) to the provided writer.
 73        /// </summary>
 74        /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
 75        /// <param name="writer">The writer to write.</param>
 76        /// <param name="value">The value to convert and write.</param>
 77        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 78        /// <exception cref="ArgumentNullException">
 79        ///   <paramref name="writer"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 80        /// </exception>
 81        public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonTypeInfo<TValue> jsonTypeInfo)
 082        {
 083            ArgumentNullException.ThrowIfNull(writer);
 084            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 85
 086            jsonTypeInfo.EnsureConfigured();
 087            jsonTypeInfo.Serialize(writer, value);
 088        }
 89
 90        /// <summary>
 91        /// Writes one JSON value (including objects or arrays) to the provided writer.
 92        /// </summary>
 93        /// <param name="writer">The writer to write.</param>
 94        /// <param name="value">The value to convert and write.</param>
 95        /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
 96        /// <exception cref="ArgumentNullException">
 97        ///   <paramref name="writer"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
 98        /// </exception>
 99        /// <exception cref="InvalidCastException">
 100        /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>.
 101        /// </exception>
 102        public static void Serialize(Utf8JsonWriter writer, object? value, JsonTypeInfo jsonTypeInfo)
 0103        {
 0104            ArgumentNullException.ThrowIfNull(writer);
 0105            ArgumentNullException.ThrowIfNull(jsonTypeInfo);
 106
 0107            jsonTypeInfo.EnsureConfigured();
 0108            jsonTypeInfo.SerializeAsObject(writer, value);
 0109        }
 110
 111        /// <summary>
 112        /// Writes one JSON value (including objects or arrays) to the provided writer.
 113        /// </summary>
 114        /// <param name="writer"></param>
 115        /// <param name="value">The value to convert and write.</param>
 116        /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
 117        /// <param name="context">A metadata provider for serializable types.</param>
 118        /// <exception cref="ArgumentException">
 119        /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>.
 120        /// </exception>
 121        /// <exception cref="ArgumentNullException">
 122        /// <paramref name="writer"/> or <paramref name="inputType"/> is <see langword="null"/>.
 123        /// </exception>
 124        /// <exception cref="NotSupportedException">
 125        /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
 126        /// for <paramref name="inputType"/> or its serializable members.
 127        /// </exception>
 128        /// <exception cref="InvalidOperationException">
 129        /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided
 130        /// <paramref name="context"/> returns <see langword="null"/> for the type to convert.
 131        /// </exception>
 132        public static void Serialize(Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerContext context
 0133        {
 0134            ArgumentNullException.ThrowIfNull(writer);
 0135            ArgumentNullException.ThrowIfNull(context);
 136
 0137            ValidateInputType(value, inputType);
 0138            JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType);
 0139            jsonTypeInfo.SerializeAsObject(writer, value);
 0140        }
 141    }
 142}

Methods/Properties

IsReflectionEnabledByDefault()
.cctor()
GetTypeInfo(System.Text.Json.JsonSerializerOptions,System.Type)
GetTypeInfo(System.Text.Json.JsonSerializerOptions)
GetTypeInfo(System.Text.Json.Serialization.JsonSerializerContext,System.Type)
ValidateInputType(System.Object,System.Type)
IsValidNumberHandlingValue(System.Text.Json.Serialization.JsonNumberHandling)
IsValidCreationHandlingValue(System.Text.Json.Serialization.JsonObjectCreationHandling)
IsValidUnmappedMemberHandlingValue(System.Text.Json.Serialization.JsonUnmappedMemberHandling)
UnboxOnRead(System.Object)
ThrowUnableToCastValue(System.Object)
UnboxOnWrite(System.Object)
GetOrAddListTypeInfoForRootLevelValueMode(System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<T>)
GetOrAddListTypeInfoForArrayMode(System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<T>)
Deserialize(System.Text.Json.JsonDocument,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.JsonDocument,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.JsonDocument,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.Text.Json.JsonDocument,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.Text.Json.JsonDocument,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
Deserialize(System.Text.Json.JsonElement,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.JsonElement,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.JsonElement,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.Text.Json.JsonElement,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.Text.Json.JsonElement,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
.cctor()
TryReadMetadata(System.Text.Json.Serialization.JsonConverter,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Text.Json.Utf8JsonReader&,System.Text.Json.ReadStack&)
IsMetadataPropertyName(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver)
GetMetadataPropertyName(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver)
TryHandleReferenceFromJsonElement(System.Text.Json.Utf8JsonReader&,System.Text.Json.ReadStack&,System.Text.Json.JsonElement,System.Object&)
TryHandleReferenceFromJsonNode(System.Text.Json.Utf8JsonReader&,System.Text.Json.ReadStack&,System.Text.Json.Nodes.JsonNode,System.Object&)
ReadAsStringMetadataValue(System.Text.Json.Nodes.JsonNode)
ValidateMetadataForObjectConverter(System.Text.Json.ReadStack&)
ValidateMetadataForArrayConverter(System.Text.Json.Serialization.JsonConverter,System.Text.Json.Utf8JsonReader&,System.Text.Json.ReadStack&)
ResolveReferenceId(System.Text.Json.ReadStack&)
LookupProperty(System.Object,System.ReadOnlySpan`1<System.Byte>,System.Text.Json.ReadStack&,System.Text.Json.JsonSerializerOptions,System.Boolean&,System.Boolean)
GetPropertyName(System.Text.Json.ReadStack&,System.Text.Json.Utf8JsonReader&,System.Text.Json.JsonSerializerOptions,System.Boolean&)
CreateExtensionDataProperty(System.Object,System.Text.Json.Serialization.Metadata.JsonPropertyInfo,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.Nodes.JsonNode,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.Nodes.JsonNode,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.Nodes.JsonNode,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.Text.Json.Nodes.JsonNode,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.Text.Json.Nodes.JsonNode,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
ReadFromNode(System.Text.Json.Nodes.JsonNode,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
ReadFromNodeAsObject(System.Text.Json.Nodes.JsonNode,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
DeserializeAsync(System.IO.Pipelines.PipeReader,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
DeserializeAsync(System.IO.Pipelines.PipeReader,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Threading.CancellationToken)
DeserializeAsync(System.IO.Pipelines.PipeReader,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Threading.CancellationToken)
DeserializeAsync(System.IO.Pipelines.PipeReader,System.Type,System.Text.Json.Serialization.JsonSerializerContext,System.Threading.CancellationToken)
DeserializeAsync(System.IO.Pipelines.PipeReader,System.Type,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Pipelines.PipeReader,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Pipelines.PipeReader,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Pipelines.PipeReader,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Boolean,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Pipelines.PipeReader,System.Boolean,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
DeserializeAsyncEnumerableCore(System.IO.Pipelines.PipeReader,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<T>,System.Boolean,System.Threading.CancellationToken)
CreateAsyncEnumerableFromArray()
Deserialize(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.JsonSerializerOptions)
Deserialize(System.ReadOnlySpan`1<System.Byte>,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.ReadOnlySpan`1<System.Byte>,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
ReadFromSpan(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Nullable`1<System.Int32>)
ReadFromSpanAsObject(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Nullable`1<System.Int32>)
DeserializeAsync(System.IO.Stream,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
Deserialize(System.IO.Stream,System.Text.Json.JsonSerializerOptions)
DeserializeAsync(System.IO.Stream,System.Type,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
Deserialize(System.IO.Stream,System.Type,System.Text.Json.JsonSerializerOptions)
DeserializeAsync(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Threading.CancellationToken)
DeserializeAsync(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Threading.CancellationToken)
Deserialize(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
DeserializeAsync(System.IO.Stream,System.Type,System.Text.Json.Serialization.JsonSerializerContext,System.Threading.CancellationToken)
Deserialize(System.IO.Stream,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
DeserializeAsyncEnumerable(System.IO.Stream,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Stream,System.Boolean,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Threading.CancellationToken)
DeserializeAsyncEnumerable(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Boolean,System.Threading.CancellationToken)
DeserializeAsyncEnumerableCore(System.IO.Stream,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<T>,System.Boolean,System.Threading.CancellationToken)
CreateAsyncEnumerableFromArray()
Deserialize(System.String,System.Text.Json.JsonSerializerOptions)
Deserialize(System.ReadOnlySpan`1<System.Char>,System.Text.Json.JsonSerializerOptions)
Deserialize(System.String,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.ReadOnlySpan`1<System.Char>,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.String,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.ReadOnlySpan`1<System.Char>,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.String,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.ReadOnlySpan`1<System.Char>,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.String,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
Deserialize(System.ReadOnlySpan`1<System.Char>,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
ReadFromSpan(System.ReadOnlySpan`1<System.Char>,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
ReadFromSpanAsObject(System.ReadOnlySpan`1<System.Char>,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.Text.Json.Utf8JsonReader&,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.JsonSerializerOptions)
Deserialize(System.Text.Json.Utf8JsonReader&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Deserialize(System.Text.Json.Utf8JsonReader&,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Deserialize(System.Text.Json.Utf8JsonReader&,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
Read(System.Text.Json.Utf8JsonReader&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
ReadAsObject(System.Text.Json.Utf8JsonReader&,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
GetReaderScopedToNextValue(System.Text.Json.Utf8JsonReader&,System.Text.Json.ReadStack&)
SerializeToUtf8Bytes(TValue,System.Text.Json.JsonSerializerOptions)
SerializeToUtf8Bytes(System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
SerializeToUtf8Bytes(TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
SerializeToUtf8Bytes(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeToUtf8Bytes(System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
WriteBytes(TValue&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
WriteBytesAsObject(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeToDocument(TValue,System.Text.Json.JsonSerializerOptions)
SerializeToDocument(System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
SerializeToDocument(TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
SerializeToDocument(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeToDocument(System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
WriteDocument(TValue&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
WriteDocumentAsObject(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeToElement(TValue,System.Text.Json.JsonSerializerOptions)
SerializeToElement(System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
SerializeToElement(TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
SerializeToElement(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeToElement(System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
WriteElement(TValue&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
WriteElementAsObject(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
.cctor()
WriteMetadataForObject(System.Text.Json.Serialization.JsonConverter,System.Text.Json.WriteStack&,System.Text.Json.Utf8JsonWriter)
WriteMetadataForCollection(System.Text.Json.Serialization.JsonConverter,System.Text.Json.WriteStack&,System.Text.Json.Utf8JsonWriter)
TryGetReferenceForValue(System.Object,System.Text.Json.WriteStack&,System.Text.Json.Utf8JsonWriter)
SerializeToNode(TValue,System.Text.Json.JsonSerializerOptions)
SerializeToNode(System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
SerializeToNode(TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
SerializeToNode(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeToNode(System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
WriteNode(TValue&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
WriteNodeAsObject(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeAsync(System.IO.Pipelines.PipeWriter,TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Threading.CancellationToken)
SerializeAsync(System.IO.Pipelines.PipeWriter,TValue,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
SerializeAsync(System.IO.Pipelines.PipeWriter,System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Threading.CancellationToken)
SerializeAsync(System.IO.Pipelines.PipeWriter,System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext,System.Threading.CancellationToken)
SerializeAsync(System.IO.Pipelines.PipeWriter,System.Object,System.Type,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
SerializeAsync(System.IO.Stream,TValue,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
Serialize(System.IO.Stream,TValue,System.Text.Json.JsonSerializerOptions)
SerializeAsync(System.IO.Stream,System.Object,System.Type,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)
Serialize(System.IO.Stream,System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
SerializeAsync(System.IO.Stream,TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>,System.Threading.CancellationToken)
Serialize(System.IO.Stream,TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
SerializeAsync(System.IO.Stream,System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Threading.CancellationToken)
Serialize(System.IO.Stream,System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
SerializeAsync(System.IO.Stream,System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext,System.Threading.CancellationToken)
Serialize(System.IO.Stream,System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
Serialize(TValue,System.Text.Json.JsonSerializerOptions)
Serialize(System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
Serialize(TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Serialize(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Serialize(System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)
WriteString(TValue&,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
WriteStringAsObject(System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Serialize(System.Text.Json.Utf8JsonWriter,TValue,System.Text.Json.JsonSerializerOptions)
Serialize(System.Text.Json.Utf8JsonWriter,System.Object,System.Type,System.Text.Json.JsonSerializerOptions)
Serialize(System.Text.Json.Utf8JsonWriter,TValue,System.Text.Json.Serialization.Metadata.JsonTypeInfo`1<TValue>)
Serialize(System.Text.Json.Utf8JsonWriter,System.Object,System.Text.Json.Serialization.Metadata.JsonTypeInfo)
Serialize(System.Text.Json.Utf8JsonWriter,System.Object,System.Type,System.Text.Json.Serialization.JsonSerializerContext)