| File: System\Text\Json\Serialization\JsonSerializer.Read.Node.cs | Web Access |
| Project: src\runtime\src\libraries\System.Text.Json\src\System.Text.Json.csproj (System.Text.Json) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Nodes; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; namespace System.Text.Json { public static partial class JsonSerializer { /// <summary> /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <typeparamref name="TValue"/>. /// </summary> /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam> /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns> /// <param name="node">The <see cref="JsonNode"/> to convert.</param> /// <param name="options">Options to control the behavior during parsing.</param> /// <exception cref="JsonException"> /// <typeparamref name="TValue" /> is not compatible with the JSON. /// </exception> /// <exception cref="NotSupportedException"> /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> /// for <typeparamref name="TValue"/> or its serializable members. /// </exception> [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] public static TValue? Deserialize<TValue>(this JsonNode? node, JsonSerializerOptions? options = null) { JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); return ReadFromNode(node, jsonTypeInfo); } /// <summary> /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <paramref name="returnType"/>. /// </summary> /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns> /// <param name="node">The <see cref="JsonNode"/> to convert.</param> /// <param name="returnType">The type of the object to convert to and return.</param> /// <param name="options">Options to control the behavior during parsing.</param> /// <exception cref="JsonException"> /// <paramref name="returnType"/> is not compatible with the JSON. /// </exception> /// <exception cref="NotSupportedException"> /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> /// for <paramref name="returnType"/> or its serializable members. /// </exception> [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] public static object? Deserialize(this JsonNode? node, Type returnType, JsonSerializerOptions? options = null) { ArgumentNullException.ThrowIfNull(returnType); JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType); return ReadFromNodeAsObject(node, jsonTypeInfo); } /// <summary> /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <typeparamref name="TValue"/>. /// </summary> /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam> /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns> /// <param name="node">The <see cref="JsonNode"/> to convert.</param> /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>. /// </exception> /// <exception cref="JsonException"> /// <typeparamref name="TValue" /> is not compatible with the JSON. /// </exception> public static TValue? Deserialize<TValue>(this JsonNode? node, JsonTypeInfo<TValue> jsonTypeInfo) { ArgumentNullException.ThrowIfNull(jsonTypeInfo); jsonTypeInfo.EnsureConfigured(); return ReadFromNode(node, jsonTypeInfo); } /// <summary> /// Converts the <see cref="JsonNode"/> representing a single JSON value into an instance specified by the <paramref name="jsonTypeInfo"/>. /// </summary> /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns> /// <param name="node">The <see cref="JsonNode"/> to convert.</param> /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="jsonTypeInfo"/> is <see langword="null"/>. /// </exception> public static object? Deserialize(this JsonNode? node, JsonTypeInfo jsonTypeInfo) { ArgumentNullException.ThrowIfNull(jsonTypeInfo); jsonTypeInfo.EnsureConfigured(); return ReadFromNodeAsObject(node, jsonTypeInfo); } /// <summary> /// Converts the <see cref="JsonNode"/> representing a single JSON value into a <paramref name="returnType"/>. /// </summary> /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns> /// <param name="node">The <see cref="JsonNode"/> to convert.</param> /// <param name="returnType">The type of the object to convert to and return.</param> /// <param name="context">A metadata provider for serializable types.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="returnType"/> is <see langword="null"/>. /// /// -or- /// /// <paramref name="context"/> is <see langword="null"/>. /// </exception> /// <exception cref="JsonException"> /// The JSON is invalid. /// /// -or- /// /// <paramref name="returnType" /> is not compatible with the JSON. /// /// -or- /// /// There is remaining data in the string beyond a single JSON value.</exception> /// <exception cref="NotSupportedException"> /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> /// for <paramref name="returnType"/> or its serializable members. /// </exception> /// <exception cref="InvalidOperationException"> /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method of the provided /// <paramref name="context"/> returns <see langword="null"/> for the type to convert. /// </exception> public static object? Deserialize(this JsonNode? node, Type returnType, JsonSerializerContext context) { ArgumentNullException.ThrowIfNull(returnType); ArgumentNullException.ThrowIfNull(context); JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType); return ReadFromNodeAsObject(node, jsonTypeInfo); } private static TValue? ReadFromNode<TValue>(JsonNode? node, JsonTypeInfo<TValue> jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; // For performance, share the same buffer across serialization and deserialization. using var output = new PooledByteBufferWriter(options.DefaultBufferSize); using (var writer = new Utf8JsonWriter(output, options.GetWriterOptions())) { if (node is null) { writer.WriteNullValue(); } else { node.WriteTo(writer, options); } } return ReadFromSpan(output.WrittenSpan, jsonTypeInfo); } private static object? ReadFromNodeAsObject(JsonNode? node, JsonTypeInfo jsonTypeInfo) { JsonSerializerOptions options = jsonTypeInfo.Options; // For performance, share the same buffer across serialization and deserialization. using var output = new PooledByteBufferWriter(options.DefaultBufferSize); using (var writer = new Utf8JsonWriter(output, options.GetWriterOptions())) { if (node is null) { writer.WriteNullValue(); } else { node.WriteTo(writer, options); } } return ReadFromSpanAsObject(output.WrittenSpan, jsonTypeInfo); } } }