/// <summary> /// Write 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> public static void Serialize(Utf8JsonWriter writer, object?value, Type inputType, JsonSerializerOptions?options = null) { if (inputType == null) { throw new ArgumentNullException(nameof(inputType)); } if (value != null && !inputType.IsAssignableFrom(value.GetType())) { ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value); } Serialize <object?>(writer, value, inputType, options); }
private static void VerifyValueAndType(object?value, Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } else if (value != null) { if (!type.IsAssignableFrom(value.GetType())) { ThrowHelper.ThrowArgumentException_DeserializeWrongType(type, value); } } }
/// <summary> /// Convert the provided value into a <see cref="byte"/> array. /// </summary> /// <returns>A UTF-8 representation of the value.</returns> /// <param name="value">The value to convert.</param> /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param> /// <param name="options">Options to control the conversion behavior.</param> /// <exception cref="ArgumentException"> /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>. /// </exception> /// <exception cref="System.ArgumentNullException"> /// <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> public static byte[] SerializeToUtf8Bytes( object?value, [DynamicallyAccessedMembers(MembersAccessedOnWrite)] Type inputType, JsonSerializerOptions?options = null) { if (inputType == null) { throw new ArgumentNullException(nameof(inputType)); } if (value != null && !inputType.IsAssignableFrom(value.GetType())) { ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value); } return(WriteCoreBytes <object>(value !, inputType, options)); }
/// <summary> /// Convert the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Stream"/>. /// </summary> /// <returns>A task that represents the asynchronous write operation.</returns> /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param> /// <param name="value">The value to convert.</param> /// <param name="inputType">The type of the <paramref name="value"/> to convert.</param> /// <param name="options">Options to control the conversion behavior.</param> /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> which may be used to cancel the write operation.</param> /// <exception cref="ArgumentException"> /// <paramref name="inputType"/> is not compatible with <paramref name="value"/>. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="utf8Json"/> 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> public static Task SerializeAsync(Stream utf8Json, object?value, Type inputType, JsonSerializerOptions?options = null, CancellationToken cancellationToken = default) { if (utf8Json == null) { throw new ArgumentNullException(nameof(utf8Json)); } if (inputType == null) { throw new ArgumentNullException(nameof(inputType)); } if (value != null && !inputType.IsAssignableFrom(value.GetType())) { ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value); } return(WriteAsyncCore <object>(utf8Json, value !, inputType, options, cancellationToken)); }