<PackageReference Include="System.Text.Json" Version="9.0.5" />

JsonSerializer

public static class JsonSerializer
Provides functionality to serialize objects or value types to JSON and to deserialize JSON into objects or value types.
using System.Buffers; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Pipelines; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text.Json.Nodes; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; using System.Threading; using System.Threading.Tasks; namespace System.Text.Json { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class JsonSerializer { internal const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved."; internal const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications."; internal const string IdPropertyName = "$id"; internal const string RefPropertyName = "$ref"; internal const string TypePropertyName = "$type"; internal const string ValuesPropertyName = "$values"; private static readonly byte[] s_idPropertyName; private static readonly byte[] s_refPropertyName; private static readonly byte[] s_typePropertyName; private static readonly byte[] s_valuesPropertyName; internal static readonly JsonEncodedText s_metadataId; internal static readonly JsonEncodedText s_metadataRef; internal static readonly JsonEncodedText s_metadataType; internal static readonly JsonEncodedText s_metadataValues; internal const float FlushThreshold = 0.9; public static bool IsReflectionEnabledByDefault { get; } = !AppContext.TryGetSwitch("System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault", out bool isEnabled) || isEnabled; [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(1)] this JsonDocument document, JsonSerializerOptions options = null) { if (document == null) ThrowHelper.ThrowArgumentNullException("document"); JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return ReadFromSpan(document.GetRootRawValue().Span, typeInfo, null); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(this JsonDocument document, Type returnType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (document == null) ThrowHelper.ThrowArgumentNullException("document"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadFromSpanAsObject(document.GetRootRawValue().Span, typeInfo, null); } [return: System.Runtime.CompilerServices.Nullable(2)] public static TValue Deserialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(this JsonDocument document, JsonTypeInfo<TValue> jsonTypeInfo) { if (document == null) ThrowHelper.ThrowArgumentNullException("document"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpan(document.GetRootRawValue().Span, jsonTypeInfo, null); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(this JsonDocument document, JsonTypeInfo jsonTypeInfo) { if (document == null) ThrowHelper.ThrowArgumentNullException("document"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpanAsObject(document.GetRootRawValue().Span, jsonTypeInfo, null); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(this JsonDocument document, Type returnType, JsonSerializerContext context) { if (document == null) ThrowHelper.ThrowArgumentNullException("document"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); JsonTypeInfo typeInfo = GetTypeInfo(context, returnType); return ReadFromSpanAsObject(document.GetRootRawValue().Span, typeInfo, null); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>(this JsonElement element, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return ReadFromSpan(element.GetRawValue().Span, typeInfo, null); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static object Deserialize(this JsonElement element, [System.Runtime.CompilerServices.Nullable(1)] Type returnType, JsonSerializerOptions options = null) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadFromSpanAsObject(element.GetRawValue().Span, typeInfo, null); } [System.Runtime.CompilerServices.NullableContext(2)] public static TValue Deserialize<TValue>(this JsonElement element, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpan(element.GetRawValue().Span, jsonTypeInfo, null); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(this JsonElement element, JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpanAsObject(element.GetRawValue().Span, jsonTypeInfo, null); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(this JsonElement element, Type returnType, JsonSerializerContext context) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); JsonTypeInfo typeInfo = GetTypeInfo(context, returnType); return ReadFromSpanAsObject(element.GetRawValue().Span, typeInfo, null); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>(this JsonNode node, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return ReadFromNode(node, typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static object Deserialize(this JsonNode node, [System.Runtime.CompilerServices.Nullable(1)] Type returnType, JsonSerializerOptions options = null) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadFromNodeAsObject(node, typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] public static TValue Deserialize<TValue>(this JsonNode node, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromNode(node, jsonTypeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] public static object Deserialize(this JsonNode node, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromNodeAsObject(node, jsonTypeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Runtime.CompilerServices.Nullable(2)] this JsonNode node, Type returnType, JsonSerializerContext context) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); JsonTypeInfo typeInfo = GetTypeInfo(context, returnType); return ReadFromNodeAsObject(node, typeInfo); } private static TValue ReadFromNode<TValue>(JsonNode node, JsonTypeInfo<TValue> jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; using (PooledByteBufferWriter pooledByteBufferWriter = new PooledByteBufferWriter(options.DefaultBufferSize)) { using (Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(pooledByteBufferWriter, options.GetWriterOptions())) { if (node == null) utf8JsonWriter.WriteNullValue(); else node.WriteTo(utf8JsonWriter, options); } return ReadFromSpan(pooledByteBufferWriter.WrittenMemory.Span, jsonTypeInfo, null); } } private static object ReadFromNodeAsObject(JsonNode node, JsonTypeInfo jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; using (PooledByteBufferWriter pooledByteBufferWriter = new PooledByteBufferWriter(options.DefaultBufferSize)) { using (Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(pooledByteBufferWriter, options.GetWriterOptions())) { if (node == null) utf8JsonWriter.WriteNullValue(); else node.WriteTo(utf8JsonWriter, options); } return ReadFromSpanAsObject(pooledByteBufferWriter.WrittenMemory.Span, jsonTypeInfo, null); } } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static JsonDocument SerializeToDocument<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return WriteDocument(ref value, typeInfo); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static JsonDocument SerializeToDocument([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(options, inputType); return WriteDocumentAsObject(value, typeInfo); } public static JsonDocument SerializeToDocument<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteDocument(ref value, jsonTypeInfo); } public static JsonDocument SerializeToDocument([System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteDocumentAsObject(value, jsonTypeInfo); } public static JsonDocument SerializeToDocument([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); return WriteDocumentAsObject(value, GetTypeInfo(context, inputType)); } private static JsonDocument WriteDocument<TValue>([In] [System.Runtime.CompilerServices.IsReadOnly] ref TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; PooledByteBufferWriter pooledByteBufferWriter = new PooledByteBufferWriter(options.DefaultBufferSize); Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriter(options, pooledByteBufferWriter); try { jsonTypeInfo.Serialize(writer, ref value, (object)null); return JsonDocument.ParseRented(pooledByteBufferWriter, options.GetDocumentOptions()); } finally { Utf8JsonWriterCache.ReturnWriter(writer); } } private static JsonDocument WriteDocumentAsObject(object value, JsonTypeInfo jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; PooledByteBufferWriter pooledByteBufferWriter = new PooledByteBufferWriter(options.DefaultBufferSize); Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriter(options, pooledByteBufferWriter); try { jsonTypeInfo.SerializeAsObject(writer, value); return JsonDocument.ParseRented(pooledByteBufferWriter, options.GetDocumentOptions()); } finally { Utf8JsonWriterCache.ReturnWriter(writer); } } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static JsonElement SerializeToElement<TValue>([System.Runtime.CompilerServices.Nullable(1)] TValue value, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return WriteElement(ref value, typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static JsonElement SerializeToElement(object value, [System.Runtime.CompilerServices.Nullable(1)] Type inputType, JsonSerializerOptions options = null) { ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(options, inputType); return WriteElementAsObject(value, typeInfo); } public static JsonElement SerializeToElement<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteElement(ref value, jsonTypeInfo); } public static JsonElement SerializeToElement([System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteElementAsObject(value, jsonTypeInfo); } public static JsonElement SerializeToElement([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(context, inputType); return WriteElementAsObject(value, typeInfo); } private static JsonElement WriteElement<TValue>([In] [System.Runtime.CompilerServices.IsReadOnly] ref TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.Serialize(writer, ref value, (object)null); return JsonElement.ParseValue(bufferWriter.WrittenMemory.Span, options.GetDocumentOptions()); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } private static JsonElement WriteElementAsObject(object value, JsonTypeInfo jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.SerializeAsObject(writer, value); return JsonElement.ParseValue(bufferWriter.WrittenMemory.Span, options.GetDocumentOptions()); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static JsonNode SerializeToNode<TValue>([System.Runtime.CompilerServices.Nullable(1)] TValue value, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return WriteNode(ref value, typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static JsonNode SerializeToNode(object value, [System.Runtime.CompilerServices.Nullable(1)] Type inputType, JsonSerializerOptions options = null) { ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(options, inputType); return WriteNodeAsObject(value, typeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static JsonNode SerializeToNode<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteNode(ref value, jsonTypeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] public static JsonNode SerializeToNode(object value, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteNodeAsObject(value, jsonTypeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static JsonNode SerializeToNode([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(context, inputType); return WriteNodeAsObject(value, typeInfo); } private static JsonNode WriteNode<TValue>([In] [System.Runtime.CompilerServices.IsReadOnly] ref TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.Serialize(writer, ref value, (object)null); return JsonNode.Parse(bufferWriter.WrittenMemory.Span, options.GetNodeOptions(), options.GetDocumentOptions()); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } private static JsonNode WriteNodeAsObject(object value, JsonTypeInfo jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.SerializeAsObject(writer, value); return JsonNode.Parse(bufferWriter.WrittenMemory.Span, options.GetNodeOptions(), options.GetDocumentOptions()); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] private static JsonTypeInfo GetTypeInfo(JsonSerializerOptions options, Type inputType) { if (options == null) options = JsonSerializerOptions.Default; options.MakeReadOnly(true); if (!(inputType == JsonTypeInfo.ObjectType)) return options.GetTypeInfoForRootType(inputType, false); return options.ObjectTypeInfo; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] private static JsonTypeInfo<T> GetTypeInfo<T>(JsonSerializerOptions options) { return (JsonTypeInfo<T>)GetTypeInfo(options, typeof(T)); } private static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type inputType) { JsonTypeInfo typeInfo = context.GetTypeInfo(inputType); if (typeInfo == null) ThrowHelper.ThrowInvalidOperationException_NoMetadataForType(inputType, context); typeInfo.EnsureConfigured(); return typeInfo; } private static void ValidateInputType(object value, Type inputType) { if ((object)inputType == null) ThrowHelper.ThrowArgumentNullException("inputType"); if (value != null) { Type type = value.GetType(); if (!inputType.IsAssignableFrom(type)) ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value); } } internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) { return JsonHelpers.IsInRangeInclusive((int)handling, 0, 7); } internal static bool IsValidCreationHandlingValue(JsonObjectCreationHandling handling) { if ((uint)handling > 1) return false; return true; } internal static bool IsValidUnmappedMemberHandlingValue(JsonUnmappedMemberHandling handling) { if ((uint)handling > 1) return false; return true; } [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("value")] internal static T UnboxOnRead<T>(object value) { if (value == null) { if (default(T) != null) <UnboxOnRead>g__ThrowUnableToCastValue|50_0<T>(value); return default(T); } if (value is T) return (T)value; <UnboxOnRead>g__ThrowUnableToCastValue|50_0<T>(value); return default(T); } [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("value")] internal static T UnboxOnWrite<T>(object value) { if (default(T) != null && value == null) ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(typeof(T)); return (T)value; } internal static bool TryReadMetadata(JsonConverter converter, JsonTypeInfo jsonTypeInfo, ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state) { bool allowOutOfOrderMetadataProperties = jsonTypeInfo.Options.AllowOutOfOrderMetadataProperties; bool flag = false; Utf8JsonReader reader2; if (allowOutOfOrderMetadataProperties && !reader.IsFinalBlock) { reader2 = reader; if (!ref reader2.TrySkipPartial()) return false; } else reader2 = default(Utf8JsonReader); while (true) { if (state.Current.PropertyState == StackFramePropertyState.None) { if (!reader.Read()) return false; state.Current.PropertyState = StackFramePropertyState.ReadName; } if ((int)state.Current.PropertyState < 2) { if (reader.TokenType != JsonTokenType.EndObject) { if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Ref) != 0) ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(ref reader.GetUnescapedSpan(), ref state); ReadOnlySpan<byte> unescapedSpan = ref reader.GetUnescapedSpan(); switch (state.Current.LatestMetadataPropertyName = GetMetadataPropertyName(unescapedSpan, jsonTypeInfo.PolymorphicTypeResolver)) { case MetadataPropertyName.Id: state.Current.JsonPropertyName = s_idPropertyName; if (state.ReferenceResolver == null) ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(unescapedSpan, ref state); if ((state.Current.MetadataPropertyNames & (MetadataPropertyName.Id | MetadataPropertyName.Ref)) != 0) ThrowHelper.ThrowJsonException_MetadataIdCannotBeCombinedWithRef(unescapedSpan, ref state); if (!converter.CanHaveMetadata) ThrowHelper.ThrowJsonException_MetadataCannotParsePreservedObjectIntoImmutable(converter.Type); goto IL_0257; case MetadataPropertyName.Ref: state.Current.JsonPropertyName = s_refPropertyName; if (state.ReferenceResolver == null) ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(unescapedSpan, ref state); if (converter.IsValueType) ThrowHelper.ThrowJsonException_MetadataInvalidReferenceToValueType(converter.Type); if ((state.Current.MetadataPropertyNames != MetadataPropertyName.None) | flag) ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(ref reader.GetUnescapedSpan(), ref state); goto IL_0257; case MetadataPropertyName.Type: state.Current.JsonPropertyName = (jsonTypeInfo.PolymorphicTypeResolver?.CustomTypeDiscriminatorPropertyNameUtf8 ?? s_typePropertyName); if (jsonTypeInfo.PolymorphicTypeResolver == null) ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(unescapedSpan, ref state); if (state.PolymorphicTypeDiscriminator != null) ThrowHelper.ThrowJsonException_DuplicateMetadataProperty(state.Current.JsonPropertyName); goto IL_0257; case MetadataPropertyName.Values: state.Current.JsonPropertyName = s_valuesPropertyName; if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0) ThrowHelper.ThrowJsonException_DuplicateMetadataProperty(s_valuesPropertyName); if (flag) ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, jsonTypeInfo.Type, ref reader); goto IL_0257; default: { if (!allowOutOfOrderMetadataProperties) break; if (!flag) { flag = true; reader2 = reader; } if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0) ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, jsonTypeInfo.Type, ref reader); if (IsMetadataPropertyName(unescapedSpan, null)) ThrowHelper.ThrowUnexpectedMetadataException(unescapedSpan, ref reader, ref state); goto IL_0257; } IL_0257: state.Current.PropertyState = StackFramePropertyState.Name; goto IL_0263; } } break; } goto IL_0263; IL_0263: if ((int)state.Current.PropertyState < 3) { if (!reader.Read()) return false; state.Current.PropertyState = StackFramePropertyState.ReadValue; } switch (state.Current.LatestMetadataPropertyName) { case MetadataPropertyName.Id: if (reader.TokenType != JsonTokenType.String) ThrowHelper.ThrowJsonException_MetadataValueWasNotString(reader.TokenType); if (state.ReferenceId != null) ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_refPropertyName, ref reader, ref state); state.ReferenceId = reader.GetString(); goto IL_03c4; case MetadataPropertyName.Ref: if (reader.TokenType != JsonTokenType.String) ThrowHelper.ThrowJsonException_MetadataValueWasNotString(reader.TokenType); if (state.ReferenceId != null) ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_refPropertyName, ref reader, ref state); state.ReferenceId = reader.GetString(); goto IL_03c4; case MetadataPropertyName.Type: switch (reader.TokenType) { case JsonTokenType.String: state.PolymorphicTypeDiscriminator = reader.GetString(); break; case JsonTokenType.Number: state.PolymorphicTypeDiscriminator = reader.GetInt32(); break; default: ThrowHelper.ThrowJsonException_MetadataValueWasNotString(reader.TokenType); break; } goto IL_03c4; case MetadataPropertyName.Values: if (reader.TokenType != JsonTokenType.StartArray) ThrowHelper.ThrowJsonException_MetadataValuesInvalidToken(reader.TokenType); if (!allowOutOfOrderMetadataProperties) break; flag = true; reader2 = reader; ref reader.SkipWithVerify(); goto IL_03c4; default: { ref reader.SkipWithVerify(); goto IL_03c4; } IL_03c4: state.Current.MetadataPropertyNames |= state.Current.LatestMetadataPropertyName; state.Current.PropertyState = StackFramePropertyState.None; state.Current.JsonPropertyName = null; continue; } state.Current.MetadataPropertyNames |= MetadataPropertyName.Values; state.Current.PropertyState = StackFramePropertyState.None; break; } if (state.Current.MetadataPropertyNames == MetadataPropertyName.Values) ThrowHelper.ThrowJsonException_MetadataStandaloneValuesProperty(ref state, s_valuesPropertyName); if (flag) reader = reader2; return true; } internal static bool IsMetadataPropertyName(ReadOnlySpan<byte> propertyName, PolymorphicTypeResolver resolver) { if (propertyName.Length <= 0 || propertyName[0] != 36) { if (resolver == null) return false; return (resolver.CustomTypeDiscriminatorPropertyNameUtf8?.AsSpan().SequenceEqual(propertyName)).GetValueOrDefault(); } return true; } internal unsafe static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan<byte> propertyName, PolymorphicTypeResolver resolver) { if (propertyName.Length > 0 && propertyName[0] == 36) { switch (propertyName.Length) { case 3: if (propertyName.SequenceEqual(new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.F630228BD37E08E6923D5691940E6DFEEDB75FB8BC0780EE1ABA98C0C1A49778, 3))) return MetadataPropertyName.Id; break; case 4: if (propertyName.SequenceEqual(new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.84499CE395D80E3A7694D419919F5383E3C8B46E4B78F0FA291576406ECE3FA2, 4))) return MetadataPropertyName.Ref; break; case 5: if (resolver?.CustomTypeDiscriminatorPropertyNameUtf8 == null && propertyName.SequenceEqual(new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.AB50C59D4F64B05ABA854F60FD7DA6C6EB8F4715BCCE00520E5ACA886CB5B92C, 5))) return MetadataPropertyName.Type; break; case 7: if (propertyName.SequenceEqual(new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.F2CBFF984BEDEC6EBB2619964766BBE0A2EF28F9A3E5FF81ED9B35C90A9E1C70, 7))) return MetadataPropertyName.Values; break; } } byte[] array = resolver?.CustomTypeDiscriminatorPropertyNameUtf8; if (array != null && propertyName.SequenceEqual(array)) return MetadataPropertyName.Type; return MetadataPropertyName.None; } internal static bool TryHandleReferenceFromJsonElement(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state, JsonElement element, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out object referenceValue) { bool flag = false; referenceValue = null; if (element.ValueKind == JsonValueKind.Object) { int num = 0; { foreach (JsonProperty item in element.EnumerateObject()) { num++; if (flag) ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(); else { JsonElement value; if (item.EscapedNameEquals(s_idPropertyName)) { if (state.ReferenceId != null) ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_refPropertyName, ref reader, ref state); value = item.Value; if (value.ValueKind != JsonValueKind.String) { value = item.Value; ThrowHelper.ThrowJsonException_MetadataValueWasNotString(value.ValueKind); } object obj = element; ReferenceResolver referenceResolver = state.ReferenceResolver; value = item.Value; referenceResolver.AddReference(value.GetString(), obj); referenceValue = obj; return true; } if (item.EscapedNameEquals(s_refPropertyName)) { if (state.ReferenceId != null) ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_refPropertyName, ref reader, ref state); if (num > 1) ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(); value = item.Value; if (value.ValueKind != JsonValueKind.String) { value = item.Value; ThrowHelper.ThrowJsonException_MetadataValueWasNotString(value.ValueKind); } ReferenceResolver referenceResolver2 = state.ReferenceResolver; value = item.Value; referenceValue = referenceResolver2.ResolveReference(value.GetString()); flag = true; } } } return flag; } } return flag; } internal static bool TryHandleReferenceFromJsonNode(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state, JsonNode jsonNode, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out object referenceValue) { bool flag = false; referenceValue = null; JsonObject jsonObject = jsonNode as JsonObject; if (jsonObject != null) { int num = 0; { foreach (KeyValuePair<string, JsonNode> item in jsonObject) { num++; if (flag) ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(); else { if (item.Key == "$id") { if (state.ReferenceId != null) ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_refPropertyName, ref reader, ref state); string referenceId = <TryHandleReferenceFromJsonNode>g__ReadAsStringMetadataValue|64_0(item.Value); state.ReferenceResolver.AddReference(referenceId, jsonNode); referenceValue = jsonNode; return true; } if (item.Key == "$ref") { if (state.ReferenceId != null) ThrowHelper.ThrowNotSupportedException_ObjectWithParameterizedCtorRefMetadataNotSupported(s_refPropertyName, ref reader, ref state); if (num > 1) ThrowHelper.ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(); string referenceId2 = <TryHandleReferenceFromJsonNode>g__ReadAsStringMetadataValue|64_0(item.Value); referenceValue = state.ReferenceResolver.ResolveReference(referenceId2); flag = true; } } } return flag; } } return flag; } internal static void ValidateMetadataForObjectConverter(ref ReadStack state) { if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0) ThrowHelper.ThrowJsonException_MetadataUnexpectedProperty(s_valuesPropertyName, ref state); } internal static void ValidateMetadataForArrayConverter(JsonConverter converter, ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state) { switch (reader.TokenType) { case JsonTokenType.StartArray: break; case JsonTokenType.EndObject: if (state.Current.MetadataPropertyNames != MetadataPropertyName.Ref) ThrowHelper.ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref state, converter.Type); break; default: ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, converter.Type, ref reader); break; } } internal static T ResolveReferenceId<T>(ref ReadStack state) { string referenceId = state.ReferenceId; object obj = state.ReferenceResolver.ResolveReference(referenceId); state.ReferenceId = null; try { return (T)obj; } catch (InvalidCastException) { ThrowHelper.ThrowInvalidOperationException_MetadataReferenceOfTypeCannotBeAssignedToType(referenceId, obj.GetType(), typeof(T)); return default(T); } } internal static JsonPropertyInfo LookupProperty(object obj, ReadOnlySpan<byte> unescapedPropertyName, ref ReadStack state, JsonSerializerOptions options, out bool useExtensionProperty, bool createExtensionProperty = true) { JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; useExtensionProperty = false; byte[] utf8PropertyName; JsonPropertyInfo jsonPropertyInfo = jsonTypeInfo.GetProperty(unescapedPropertyName, ref state.Current, out utf8PropertyName); state.Current.PropertyIndex++; state.Current.JsonPropertyName = utf8PropertyName; if (jsonPropertyInfo == null) { if (jsonTypeInfo.EffectiveUnmappedMemberHandling == JsonUnmappedMemberHandling.Disallow) { string unmappedPropertyName = JsonHelpers.Utf8GetString(unescapedPropertyName); ThrowHelper.ThrowJsonException_UnmappedJsonProperty(jsonTypeInfo.Type, unmappedPropertyName); } JsonPropertyInfo extensionDataProperty = jsonTypeInfo.ExtensionDataProperty; if (extensionDataProperty != null && extensionDataProperty.HasGetter && extensionDataProperty.HasSetter) { state.Current.JsonPropertyNameAsString = JsonHelpers.Utf8GetString(unescapedPropertyName); if (createExtensionProperty) CreateExtensionDataProperty(obj, extensionDataProperty, options); jsonPropertyInfo = extensionDataProperty; useExtensionProperty = true; } else jsonPropertyInfo = JsonPropertyInfo.s_missingProperty; } state.Current.JsonPropertyInfo = jsonPropertyInfo; state.Current.NumberHandling = jsonPropertyInfo.EffectiveNumberHandling; return jsonPropertyInfo; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ReadOnlySpan<byte> GetPropertyName([System.Runtime.CompilerServices.ScopedRef] ref ReadStack state, ref Utf8JsonReader reader, JsonSerializerOptions options, out bool isAlreadyReadMetadataProperty) { ReadOnlySpan<byte> unescapedSpan = ref reader.GetUnescapedSpan(); isAlreadyReadMetadataProperty = false; if (state.Current.CanContainMetadata && IsMetadataPropertyName(unescapedSpan, state.Current.BaseJsonTypeInfo.PolymorphicTypeResolver)) { if (options.AllowOutOfOrderMetadataProperties) isAlreadyReadMetadataProperty = true; else ThrowHelper.ThrowUnexpectedMetadataException(unescapedSpan, ref reader, ref state); } return unescapedSpan; } internal static void CreateExtensionDataProperty(object obj, JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options) { object valueAsObject = jsonPropertyInfo.GetValueAsObject(obj); if (valueAsObject == null) { Func<object> obj2 = jsonPropertyInfo.JsonTypeInfo.CreateObject ?? jsonPropertyInfo.JsonTypeInfo.CreateObjectForExtensionDataProperty; if (obj2 == null) { if (jsonPropertyInfo.PropertyType.FullName == "System.Text.Json.Nodes.JsonObject") ThrowHelper.ThrowInvalidOperationException_NodeJsonObjectCustomConverterNotAllowedOnExtensionProperty(); else ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(jsonPropertyInfo.PropertyType); } valueAsObject = obj2(); jsonPropertyInfo.Set(obj, valueAsObject); } } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(0)] ReadOnlySpan<byte> utf8Json, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return ReadFromSpan(utf8Json, typeInfo, null); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static object Deserialize([System.Runtime.CompilerServices.Nullable(0)] ReadOnlySpan<byte> utf8Json, [System.Runtime.CompilerServices.Nullable(1)] Type returnType, JsonSerializerOptions options = null) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadFromSpanAsObject(utf8Json, typeInfo, null); } [System.Runtime.CompilerServices.NullableContext(2)] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(0)] ReadOnlySpan<byte> utf8Json, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpan(utf8Json, jsonTypeInfo, null); } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(ReadOnlySpan<byte> utf8Json, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpanAsObject(utf8Json, jsonTypeInfo, null); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Runtime.CompilerServices.Nullable(0)] ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerContext context) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); return ReadFromSpanAsObject(utf8Json, GetTypeInfo(context, returnType), null); } private static TValue ReadFromSpan<TValue>(ReadOnlySpan<byte> utf8Json, JsonTypeInfo<TValue> jsonTypeInfo, int? actualByteCount = default(int?)) { JsonReaderState state = new JsonReaderState(jsonTypeInfo.Options.GetReaderOptions()); Utf8JsonReader reader = new Utf8JsonReader(utf8Json, true, state); ReadStack state2 = default(ReadStack); state2.Initialize(jsonTypeInfo, false); return jsonTypeInfo.Deserialize(ref reader, ref state2); } private static object ReadFromSpanAsObject(ReadOnlySpan<byte> utf8Json, JsonTypeInfo jsonTypeInfo, int? actualByteCount = default(int?)) { JsonReaderState state = new JsonReaderState(jsonTypeInfo.Options.GetReaderOptions()); Utf8JsonReader reader = new Utf8JsonReader(utf8Json, true, state); ReadStack state2 = default(ReadStack); state2.Initialize(jsonTypeInfo, false); return jsonTypeInfo.DeserializeAsObject(ref reader, ref state2); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static ValueTask<TValue> DeserializeAsync<TValue>([System.Runtime.CompilerServices.Nullable(1)] Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); return GetTypeInfo<TValue>(options).DeserializeAsync(utf8Json, cancellationToken); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(1)] Stream utf8Json, JsonSerializerOptions options = null) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); return GetTypeInfo<TValue>(options).Deserialize(utf8Json); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static ValueTask<object> DeserializeAsync(Stream utf8Json, Type returnType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); return GetTypeInfo(options, returnType).DeserializeAsObjectAsync(utf8Json, cancellationToken); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(Stream utf8Json, Type returnType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); return GetTypeInfo(options, returnType).DeserializeAsObject(utf8Json); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static ValueTask<TValue> DeserializeAsync<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static ValueTask<object> DeserializeAsync(Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken); } [return: System.Runtime.CompilerServices.Nullable(2)] public static TValue Deserialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, JsonTypeInfo<TValue> jsonTypeInfo) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.Deserialize(utf8Json); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(Stream utf8Json, JsonTypeInfo jsonTypeInfo) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.DeserializeAsObject(utf8Json); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static ValueTask<object> DeserializeAsync(Stream utf8Json, Type returnType, JsonSerializerContext context, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); return GetTypeInfo(context, returnType).DeserializeAsObjectAsync(utf8Json, cancellationToken); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(Stream utf8Json, Type returnType, JsonSerializerContext context) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); return GetTypeInfo(context, returnType).DeserializeAsObject(utf8Json); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static IAsyncEnumerable<TValue> DeserializeAsyncEnumerable<TValue>([System.Runtime.CompilerServices.Nullable(1)] Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { return DeserializeAsyncEnumerable<TValue>(utf8Json, false, options, cancellationToken); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static IAsyncEnumerable<TValue> DeserializeAsyncEnumerable<TValue>([System.Runtime.CompilerServices.Nullable(1)] Stream utf8Json, bool topLevelValues, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return DeserializeAsyncEnumerableCore(utf8Json, typeInfo, topLevelValues, cancellationToken); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static IAsyncEnumerable<TValue> DeserializeAsyncEnumerable<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { return DeserializeAsyncEnumerable(utf8Json, jsonTypeInfo, false, cancellationToken); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static IAsyncEnumerable<TValue> DeserializeAsyncEnumerable<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, JsonTypeInfo<TValue> jsonTypeInfo, bool topLevelValues, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken); } private static IAsyncEnumerable<T> DeserializeAsyncEnumerableCore<T>(Stream utf8Json, JsonTypeInfo<T> jsonTypeInfo, bool topLevelValues, CancellationToken cancellationToken) { JsonReaderOptions readerOptions = jsonTypeInfo.Options.GetReaderOptions(); JsonTypeInfo<List<T>> listTypeInfo; if (topLevelValues) { listTypeInfo = <DeserializeAsyncEnumerableCore>g__GetOrAddListTypeInfoForRootLevelValueMode|92_2(jsonTypeInfo); readerOptions.AllowMultipleValues = true; } else listTypeInfo = <DeserializeAsyncEnumerableCore>g__GetOrAddListTypeInfoForArrayMode|92_1(jsonTypeInfo); return <DeserializeAsyncEnumerableCore>g__CreateAsyncEnumerableFromArray|92_0(utf8Json, listTypeInfo, readerOptions, cancellationToken); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(1)] [System.Diagnostics.CodeAnalysis.StringSyntax("Json")] string json, JsonSerializerOptions options = null) { if (json == null) ThrowHelper.ThrowArgumentNullException("json"); JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return ReadFromSpan(json.AsSpan(), typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(0)] [System.Diagnostics.CodeAnalysis.StringSyntax("Json")] ReadOnlySpan<char> json, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return ReadFromSpan(json, typeInfo); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] string json, Type returnType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (json == null) ThrowHelper.ThrowArgumentNullException("json"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadFromSpanAsObject(json.AsSpan(), typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static object Deserialize([System.Runtime.CompilerServices.Nullable(0)] [System.Diagnostics.CodeAnalysis.StringSyntax("Json")] ReadOnlySpan<char> json, [System.Runtime.CompilerServices.Nullable(1)] Type returnType, JsonSerializerOptions options = null) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadFromSpanAsObject(json, typeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static TValue Deserialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] string json, JsonTypeInfo<TValue> jsonTypeInfo) { if (json == null) ThrowHelper.ThrowArgumentNullException("json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpan(json.AsSpan(), jsonTypeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] public static TValue Deserialize<TValue>([System.Runtime.CompilerServices.Nullable(0)] [System.Diagnostics.CodeAnalysis.StringSyntax("Json")] ReadOnlySpan<char> json, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpan(json, jsonTypeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] string json, JsonTypeInfo jsonTypeInfo) { if (json == null) ThrowHelper.ThrowArgumentNullException("json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpanAsObject(json.AsSpan(), jsonTypeInfo); } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] ReadOnlySpan<char> json, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadFromSpanAsObject(json, jsonTypeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] string json, Type returnType, JsonSerializerContext context) { if (json == null) ThrowHelper.ThrowArgumentNullException("json"); if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); JsonTypeInfo typeInfo = GetTypeInfo(context, returnType); return ReadFromSpanAsObject(json.AsSpan(), typeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize([System.Runtime.CompilerServices.Nullable(0)] [System.Diagnostics.CodeAnalysis.StringSyntax("Json")] ReadOnlySpan<char> json, Type returnType, JsonSerializerContext context) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); JsonTypeInfo typeInfo = GetTypeInfo(context, returnType); return ReadFromSpanAsObject(json, typeInfo); } private unsafe static TValue ReadFromSpan<TValue>(ReadOnlySpan<char> json, JsonTypeInfo<TValue> jsonTypeInfo) { byte[] array = null; Span<byte> span = (json.Length > 85) ? ((Span<byte>)(((long)json.Length > 349525) ? new byte[JsonReaderHelper.GetUtf8ByteCount(json)] : (array = ArrayPool<byte>.Shared.Rent(json.Length * 3)))) : new Span<byte>((void*)stackalloc byte[256], 256); Span<byte> span2 = span; try { int utf8FromText = JsonReaderHelper.GetUtf8FromText(json, span2); span2 = span2.Slice(0, utf8FromText); return ReadFromSpan(span2, jsonTypeInfo, utf8FromText); } finally { if (array != null) { span2.Clear(); ArrayPool<byte>.Shared.Return(array, false); } } } private unsafe static object ReadFromSpanAsObject(ReadOnlySpan<char> json, JsonTypeInfo jsonTypeInfo) { byte[] array = null; Span<byte> span = (json.Length > 85) ? ((Span<byte>)(((long)json.Length > 349525) ? new byte[JsonReaderHelper.GetUtf8ByteCount(json)] : (array = ArrayPool<byte>.Shared.Rent(json.Length * 3)))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> span2 = span; try { int utf8FromText = JsonReaderHelper.GetUtf8FromText(json, span2); span2 = span2.Slice(0, utf8FromText); return ReadFromSpanAsObject(span2, jsonTypeInfo, utf8FromText); } finally { if (array != null) { span2.Clear(); ArrayPool<byte>.Shared.Return(array, false); } } } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return Read(ref reader, typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static object Deserialize(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.Nullable(1)] Type returnType, JsonSerializerOptions options = null) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); JsonTypeInfo typeInfo = GetTypeInfo(options, returnType); return ReadAsObject(ref reader, typeInfo); } [System.Runtime.CompilerServices.NullableContext(2)] public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return Read(ref reader, jsonTypeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(ref Utf8JsonReader reader, JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return ReadAsObject(ref reader, jsonTypeInfo); } [return: System.Runtime.CompilerServices.Nullable(2)] public static object Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerContext context) { if ((object)returnType == null) ThrowHelper.ThrowArgumentNullException("returnType"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); return ReadAsObject(ref reader, GetTypeInfo(context, returnType)); } private static TValue Read<TValue>(ref Utf8JsonReader reader, JsonTypeInfo<TValue> jsonTypeInfo) { if (reader.CurrentState.Options.CommentHandling == JsonCommentHandling.Allow) ThrowHelper.ThrowArgumentException_SerializerDoesNotSupportComments("reader"); ReadStack state = default(ReadStack); state.Initialize(jsonTypeInfo, false); Utf8JsonReader utf8JsonReader = reader; try { Utf8JsonReader reader2 = GetReaderScopedToNextValue(ref reader, ref state); return jsonTypeInfo.Deserialize(ref reader2, ref state); } catch (JsonException) { reader = utf8JsonReader; throw; } } private static object ReadAsObject(ref Utf8JsonReader reader, JsonTypeInfo jsonTypeInfo) { if (reader.CurrentState.Options.CommentHandling == JsonCommentHandling.Allow) ThrowHelper.ThrowArgumentException_SerializerDoesNotSupportComments("reader"); ReadStack state = default(ReadStack); state.Initialize(jsonTypeInfo, false); Utf8JsonReader utf8JsonReader = reader; try { Utf8JsonReader reader2 = GetReaderScopedToNextValue(ref reader, ref state); return jsonTypeInfo.DeserializeAsObject(ref reader2, ref state); } catch (JsonException) { reader = utf8JsonReader; throw; } } private static Utf8JsonReader GetReaderScopedToNextValue(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state) { ReadOnlySpan<byte> readOnlySpan = default(ReadOnlySpan<byte>); ReadOnlySequence<byte> readOnlySequence = default(ReadOnlySequence<byte>); try { JsonTokenType tokenType = reader.TokenType; ReadOnlySpan<byte> bytes; if ((tokenType == JsonTokenType.None || tokenType == JsonTokenType.PropertyName) && !reader.Read()) { bytes = default(ReadOnlySpan<byte>); ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.ExpectedOneCompleteToken, 0, bytes); } ReadOnlySequence<byte> valueSequence; switch (reader.TokenType) { case JsonTokenType.StartObject: case JsonTokenType.StartArray: { long tokenStartIndex = reader.TokenStartIndex; if (!reader.TrySkip()) { bytes = default(ReadOnlySpan<byte>); ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.NotEnoughData, 0, bytes); } long num2 = reader.BytesConsumed - tokenStartIndex; ReadOnlySequence<byte> originalSequence = reader.OriginalSequence; if (originalSequence.IsEmpty) { bytes = reader.OriginalSpan; readOnlySpan = checked(bytes.Slice((int)tokenStartIndex, (int)num2)); } else readOnlySequence = originalSequence.Slice(tokenStartIndex, num2); break; } case JsonTokenType.Number: case JsonTokenType.True: case JsonTokenType.False: case JsonTokenType.Null: if (reader.HasValueSequence) readOnlySequence = reader.ValueSequence; else readOnlySpan = reader.ValueSpan; break; case JsonTokenType.String: { ReadOnlySequence<byte> originalSequence2 = reader.OriginalSequence; if (originalSequence2.IsEmpty) { bytes = reader.ValueSpan; int length = bytes.Length + 2; readOnlySpan = reader.OriginalSpan.Slice((int)reader.TokenStartIndex, length); } else { long num3; if (!reader.HasValueSequence) { bytes = reader.ValueSpan; num3 = bytes.Length + 2; } else { valueSequence = reader.ValueSequence; num3 = valueSequence.Length + 2; } long length2 = num3; readOnlySequence = originalSequence2.Slice(reader.TokenStartIndex, length2); } break; } default: { int num; if (!reader.HasValueSequence) { bytes = reader.ValueSpan; num = bytes[0]; } else { valueSequence = reader.ValueSequence; bytes = valueSequence.First.Span; num = bytes[0]; } byte b = (byte)num; byte nextByte = b; bytes = default(ReadOnlySpan<byte>); ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.ExpectedStartOfValueNotFound, nextByte, bytes); break; } } } catch (JsonReaderException ex) { ThrowHelper.ReThrowWithPath(ref state, ex); } JsonReaderState currentState; if (!readOnlySpan.IsEmpty) { ReadOnlySpan<byte> jsonData = readOnlySpan; currentState = reader.CurrentState; return new Utf8JsonReader(jsonData, currentState.Options); } ReadOnlySequence<byte> jsonData2 = readOnlySequence; currentState = reader.CurrentState; return new Utf8JsonReader(jsonData2, currentState.Options); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static byte[] SerializeToUtf8Bytes<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return WriteBytes(ref value, typeInfo); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static byte[] SerializeToUtf8Bytes([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(options, inputType); return WriteBytesAsObject(value, typeInfo); } public static byte[] SerializeToUtf8Bytes<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteBytes(ref value, jsonTypeInfo); } public static byte[] SerializeToUtf8Bytes([System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteBytesAsObject(value, jsonTypeInfo); } public static byte[] SerializeToUtf8Bytes([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(context, inputType); return WriteBytesAsObject(value, typeInfo); } private static byte[] WriteBytes<TValue>([In] [System.Runtime.CompilerServices.IsReadOnly] ref TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.Serialize(writer, ref value, (object)null); return bufferWriter.WrittenMemory.ToArray(); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } private static byte[] WriteBytesAsObject(object value, JsonTypeInfo jsonTypeInfo) { PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.SerializeAsObject(writer, value); return bufferWriter.WrittenMemory.ToArray(); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } internal static MetadataPropertyName WriteMetadataForObject(JsonConverter jsonConverter, ref WriteStack state, Utf8JsonWriter writer) { MetadataPropertyName metadataPropertyName = MetadataPropertyName.None; if (state.NewReferenceId != null) { writer.WriteString(s_metadataId, state.NewReferenceId); metadataPropertyName |= MetadataPropertyName.Id; state.NewReferenceId = null; } object polymorphicTypeDiscriminator = state.PolymorphicTypeDiscriminator; if (polymorphicTypeDiscriminator != null) { JsonEncodedText? customTypeDiscriminatorPropertyNameJsonEncoded = state.PolymorphicTypeResolver.CustomTypeDiscriminatorPropertyNameJsonEncoded; JsonEncodedText jsonEncodedText; if (customTypeDiscriminatorPropertyNameJsonEncoded.HasValue) { JsonEncodedText valueOrDefault = customTypeDiscriminatorPropertyNameJsonEncoded.GetValueOrDefault(); jsonEncodedText = valueOrDefault; } else jsonEncodedText = s_metadataType; JsonEncodedText propertyName = jsonEncodedText; string text = polymorphicTypeDiscriminator as string; if (text != null) writer.WriteString(propertyName, text); else writer.WriteNumber(propertyName, (int)polymorphicTypeDiscriminator); metadataPropertyName |= MetadataPropertyName.Type; state.PolymorphicTypeDiscriminator = null; } return metadataPropertyName; } internal static MetadataPropertyName WriteMetadataForCollection(JsonConverter jsonConverter, ref WriteStack state, Utf8JsonWriter writer) { writer.WriteStartObject(); MetadataPropertyName result = WriteMetadataForObject(jsonConverter, ref state, writer); writer.WritePropertyName(s_metadataValues); return result; } internal static bool TryGetReferenceForValue(object currentValue, ref WriteStack state, Utf8JsonWriter writer) { bool alreadyExists; string reference = state.ReferenceResolver.GetReference(currentValue, out alreadyExists); if (alreadyExists) { writer.WriteStartObject(); writer.WriteString(s_metadataRef, reference); writer.WriteEndObject(); state.PolymorphicTypeDiscriminator = null; state.PolymorphicTypeResolver = null; } else state.NewReferenceId = reference; return alreadyExists; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static Task SerializeAsync<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); return GetTypeInfo<TValue>(options).SerializeAsync(utf8Json, value, cancellationToken, (object)null); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static void Serialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); GetTypeInfo<TValue>(options).Serialize(utf8Json, ref value, (object)null); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static Task SerializeAsync(Stream utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); ValidateInputType(value, inputType); return GetTypeInfo(options, inputType).SerializeAsObjectAsync(utf8Json, value, cancellationToken); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static void Serialize(Stream utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); ValidateInputType(value, inputType); GetTypeInfo(options, inputType).SerializeAsObject(utf8Json, value); } public static Task SerializeAsync<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken, (object)null); } public static void Serialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Stream utf8Json, TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); jsonTypeInfo.Serialize(utf8Json, ref value, (object)null); } public static Task SerializeAsync(Stream utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken); } public static void Serialize(Stream utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); jsonTypeInfo.SerializeAsObject(utf8Json, value); } public static Task SerializeAsync(Stream utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); return GetTypeInfo(context, inputType).SerializeAsObjectAsync(utf8Json, value, cancellationToken); } public static void Serialize(Stream utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); GetTypeInfo(context, inputType).SerializeAsObject(utf8Json, value); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static string Serialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { JsonTypeInfo<TValue> typeInfo = GetTypeInfo<TValue>(options); return WriteString(ref value, typeInfo); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static string Serialize([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(options, inputType); return WriteStringAsObject(value, typeInfo); } public static string Serialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteString(ref value, jsonTypeInfo); } public static string Serialize([System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo) { if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return WriteStringAsObject(value, jsonTypeInfo); } public static string Serialize([System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); JsonTypeInfo typeInfo = GetTypeInfo(context, inputType); return WriteStringAsObject(value, typeInfo); } private static string WriteString<TValue>([In] [System.Runtime.CompilerServices.IsReadOnly] ref TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.Serialize(writer, ref value, (object)null); return JsonReaderHelper.TranscodeHelper(bufferWriter.WrittenMemory.Span); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } private static string WriteStringAsObject(object value, JsonTypeInfo jsonTypeInfo) { PooledByteBufferWriter bufferWriter; Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(jsonTypeInfo.Options, out bufferWriter); try { jsonTypeInfo.SerializeAsObject(writer, value); return JsonReaderHelper.TranscodeHelper(bufferWriter.WrittenMemory.Span); } finally { Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, bufferWriter); } } public static Task SerializeAsync<[System.Runtime.CompilerServices.Nullable(2)] TValue>(PipeWriter utf8Json, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken, (object)null); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static Task SerializeAsync<[System.Runtime.CompilerServices.Nullable(2)] TValue>(PipeWriter utf8Json, TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); return GetTypeInfo<TValue>(options).SerializeAsync(utf8Json, value, cancellationToken, (object)null); } public static Task SerializeAsync(PipeWriter utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken); } public static Task SerializeAsync(PipeWriter utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); return GetTypeInfo(context, inputType).SerializeAsObjectAsync(utf8Json, value, cancellationToken); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static Task SerializeAsync(PipeWriter utf8Json, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); ValidateInputType(value, inputType); return GetTypeInfo(options, inputType).SerializeAsObjectAsync(utf8Json, value, cancellationToken); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static void Serialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Utf8JsonWriter writer, TValue value, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (writer == null) ThrowHelper.ThrowArgumentNullException("writer"); GetTypeInfo<TValue>(options).Serialize(writer, ref value, (object)null); } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public static void Serialize(Utf8JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (writer == null) ThrowHelper.ThrowArgumentNullException("writer"); ValidateInputType(value, inputType); GetTypeInfo(options, inputType).SerializeAsObject(writer, value); } public static void Serialize<[System.Runtime.CompilerServices.Nullable(2)] TValue>(Utf8JsonWriter writer, TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { if (writer == null) ThrowHelper.ThrowArgumentNullException("writer"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); jsonTypeInfo.Serialize(writer, ref value, (object)null); } public static void Serialize(Utf8JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonTypeInfo jsonTypeInfo) { if (writer == null) ThrowHelper.ThrowArgumentNullException("writer"); if (jsonTypeInfo == null) ThrowHelper.ThrowArgumentNullException("jsonTypeInfo"); jsonTypeInfo.EnsureConfigured(); jsonTypeInfo.SerializeAsObject(writer, value); } public static void Serialize(Utf8JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] object value, Type inputType, JsonSerializerContext context) { if (writer == null) ThrowHelper.ThrowArgumentNullException("writer"); if (context == null) ThrowHelper.ThrowArgumentNullException("context"); ValidateInputType(value, inputType); GetTypeInfo(context, inputType).SerializeAsObject(writer, value); } unsafe static JsonSerializer() { ReadOnlySpan<byte> readOnlySpan = new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.F630228BD37E08E6923D5691940E6DFEEDB75FB8BC0780EE1ABA98C0C1A49778, 3); s_idPropertyName = readOnlySpan.ToArray(); readOnlySpan = new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.84499CE395D80E3A7694D419919F5383E3C8B46E4B78F0FA291576406ECE3FA2, 4); s_refPropertyName = readOnlySpan.ToArray(); readOnlySpan = new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.AB50C59D4F64B05ABA854F60FD7DA6C6EB8F4715BCCE00520E5ACA886CB5B92C, 5); s_typePropertyName = readOnlySpan.ToArray(); readOnlySpan = new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.F2CBFF984BEDEC6EBB2619964766BBE0A2EF28F9A3E5FF81ED9B35C90A9E1C70, 7); s_valuesPropertyName = readOnlySpan.ToArray(); s_metadataId = JsonEncodedText.Encode("$id", null); s_metadataRef = JsonEncodedText.Encode("$ref", null); s_metadataType = JsonEncodedText.Encode("$type", null); s_metadataValues = JsonEncodedText.Encode("$values", null); } } }