| File: System\Text\Json\Serialization\JsonSerializer.Write.Utf8JsonWriter.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.CodeAnalysis; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; namespace System.Text.Json { public static partial class JsonSerializer { /// <summary> /// Writes one JSON value (including objects or arrays) to the provided writer. /// </summary> /// <typeparam name="TValue">The type of the value to serialize.</typeparam> /// <param name="writer">The writer to write.</param> /// <param name="value">The value to convert and write.</param> /// <param name="options">Options to control the behavior.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="writer"/> is <see langword="null"/>. /// </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 void Serialize<TValue>( Utf8JsonWriter writer, TValue value, JsonSerializerOptions? options = null) { ArgumentNullException.ThrowIfNull(writer); JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); jsonTypeInfo.Serialize(writer, value); } /// <summary> /// Writes one JSON value (including objects or arrays) to the provided writer. /// </summary> /// <param name="writer"></param> /// <param name="value">The value to convert and write.</param> /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param> /// <param name="options">Options to control the behavior.</param> /// <exception cref="ArgumentException"> /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="writer"/> or <paramref name="inputType"/> is <see langword="null"/>. /// </exception> /// <exception cref="NotSupportedException"> /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> /// for <paramref name="inputType"/> or its serializable members. /// </exception> [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] public static void Serialize( Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerOptions? options = null) { ArgumentNullException.ThrowIfNull(writer); ValidateInputType(value, inputType); JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType); jsonTypeInfo.SerializeAsObject(writer, value); } /// <summary> /// Writes one JSON value (including objects or arrays) to the provided writer. /// </summary> /// <typeparam name="TValue">The type of the value to serialize.</typeparam> /// <param name="writer">The writer to write.</param> /// <param name="value">The value to convert and write.</param> /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="writer"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. /// </exception> public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonTypeInfo<TValue> jsonTypeInfo) { ArgumentNullException.ThrowIfNull(writer); ArgumentNullException.ThrowIfNull(jsonTypeInfo); jsonTypeInfo.EnsureConfigured(); jsonTypeInfo.Serialize(writer, value); } /// <summary> /// Writes one JSON value (including objects or arrays) to the provided writer. /// </summary> /// <param name="writer">The writer to write.</param> /// <param name="value">The value to convert and write.</param> /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="writer"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. /// </exception> /// <exception cref="InvalidCastException"> /// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>. /// </exception> public static void Serialize(Utf8JsonWriter writer, object? value, JsonTypeInfo jsonTypeInfo) { ArgumentNullException.ThrowIfNull(writer); ArgumentNullException.ThrowIfNull(jsonTypeInfo); jsonTypeInfo.EnsureConfigured(); jsonTypeInfo.SerializeAsObject(writer, value); } /// <summary> /// Writes one JSON value (including objects or arrays) to the provided writer. /// </summary> /// <param name="writer"></param> /// <param name="value">The value to convert and write.</param> /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param> /// <param name="context">A metadata provider for serializable types.</param> /// <exception cref="ArgumentException"> /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="writer"/> or <paramref name="inputType"/> is <see langword="null"/>. /// </exception> /// <exception cref="NotSupportedException"> /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> /// for <paramref name="inputType"/> 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 void Serialize(Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerContext context) { ArgumentNullException.ThrowIfNull(writer); ArgumentNullException.ThrowIfNull(context); ValidateInputType(value, inputType); JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType); jsonTypeInfo.SerializeAsObject(writer, value); } } }