// #nullable disable using System; using System.ClientModel.Primitives; using System.Text; using System.Text.Json; using OpenAI; namespace OpenAI.Chat { public partial class ChatAudioOptions : IJsonModel { internal ChatAudioOptions() { } protected virtual ChatAudioOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) { string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) { return DeserializeChatAudioOptions(document.RootElement, data, options); } default: throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support reading '{options.Format}' format."); } } protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) { string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": return ModelReaderWriter.Write(this, options, OpenAIContext.Default); default: throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support writing '{options.Format}' format."); } } BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); ChatAudioOptions IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) { #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. if (Patch.Contains("$"u8)) { writer.WriteRawValue(Patch.GetJson("$"u8)); return; } #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. writer.WriteStartObject(); JsonModelWriteCore(writer, options); writer.WriteEndObject(); } protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support writing '{format}' format."); } #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. if (!Patch.Contains("$.voice"u8)) { writer.WritePropertyName("voice"u8); writer.WriteStringValue(OutputAudioVoice.ToString()); } if (!Patch.Contains("$.format"u8)) { writer.WritePropertyName("format"u8); writer.WriteStringValue(OutputAudioFormat.ToString()); } Patch.WriteTo(writer); #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. } ChatAudioOptions IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); protected virtual ChatAudioOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) { string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support reading '{format}' format."); } using JsonDocument document = JsonDocument.ParseValue(ref reader); return DeserializeChatAudioOptions(document.RootElement, null, options); } internal static ChatAudioOptions DeserializeChatAudioOptions(JsonElement element, BinaryData data, ModelReaderWriterOptions options) { if (element.ValueKind == JsonValueKind.Null) { return null; } ChatOutputAudioVoice outputAudioVoice = default; ChatOutputAudioFormat outputAudioFormat = default; #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory.Empty : data.ToMemory()); #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. foreach (var prop in element.EnumerateObject()) { if (prop.NameEquals("voice"u8)) { outputAudioVoice = new ChatOutputAudioVoice(prop.Value.GetString()); continue; } if (prop.NameEquals("format"u8)) { outputAudioFormat = new ChatOutputAudioFormat(prop.Value.GetString()); continue; } patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); } return new ChatAudioOptions(outputAudioVoice, outputAudioFormat, patch); } } }