openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Generated/Models/Chat/ChatAudioOptions.Serialization.cs
131lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel.Primitives; |
| 7 | using System.Text; |
| 8 | using System.Text.Json; |
| 9 | using OpenAI; |
| 10 | |
| 11 | namespace OpenAI.Chat |
| 12 | { |
| 13 | public partial class ChatAudioOptions : IJsonModel<ChatAudioOptions> |
| 14 | { |
| 15 | internal ChatAudioOptions() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | protected virtual ChatAudioOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 20 | { |
| 21 | string format = options.Format == "W" ? ((IPersistableModel<ChatAudioOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 22 | switch (format) |
| 23 | { |
| 24 | case "J": |
| 25 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 26 | { |
| 27 | return DeserializeChatAudioOptions(document.RootElement, data, options); |
| 28 | } |
| 29 | default: |
| 30 | throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support reading '{options.Format}' format."); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 35 | { |
| 36 | string format = options.Format == "W" ? ((IPersistableModel<ChatAudioOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 37 | switch (format) |
| 38 | { |
| 39 | case "J": |
| 40 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 41 | default: |
| 42 | throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support writing '{options.Format}' format."); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | BinaryData IPersistableModel<ChatAudioOptions>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 47 | |
| 48 | ChatAudioOptions IPersistableModel<ChatAudioOptions>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 49 | |
| 50 | string IPersistableModel<ChatAudioOptions>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 51 | |
| 52 | void IJsonModel<ChatAudioOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 53 | { |
| 54 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 55 | if (Patch.Contains("$"u8)) |
| 56 | { |
| 57 | writer.WriteRawValue(Patch.GetJson("$"u8)); |
| 58 | return; |
| 59 | } |
| 60 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 61 | |
| 62 | writer.WriteStartObject(); |
| 63 | JsonModelWriteCore(writer, options); |
| 64 | writer.WriteEndObject(); |
| 65 | } |
| 66 | |
| 67 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 68 | { |
| 69 | string format = options.Format == "W" ? ((IPersistableModel<ChatAudioOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 70 | if (format != "J") |
| 71 | { |
| 72 | throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support writing '{format}' format."); |
| 73 | } |
| 74 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 75 | if (!Patch.Contains("$.voice"u8)) |
| 76 | { |
| 77 | writer.WritePropertyName("voice"u8); |
| 78 | writer.WriteStringValue(OutputAudioVoice.ToString()); |
| 79 | } |
| 80 | if (!Patch.Contains("$.format"u8)) |
| 81 | { |
| 82 | writer.WritePropertyName("format"u8); |
| 83 | writer.WriteStringValue(OutputAudioFormat.ToString()); |
| 84 | } |
| 85 | |
| 86 | Patch.WriteTo(writer); |
| 87 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 88 | } |
| 89 | |
| 90 | ChatAudioOptions IJsonModel<ChatAudioOptions>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 91 | |
| 92 | protected virtual ChatAudioOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 93 | { |
| 94 | string format = options.Format == "W" ? ((IPersistableModel<ChatAudioOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 95 | if (format != "J") |
| 96 | { |
| 97 | throw new FormatException($"The model {nameof(ChatAudioOptions)} does not support reading '{format}' format."); |
| 98 | } |
| 99 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 100 | return DeserializeChatAudioOptions(document.RootElement, null, options); |
| 101 | } |
| 102 | |
| 103 | internal static ChatAudioOptions DeserializeChatAudioOptions(JsonElement element, BinaryData data, ModelReaderWriterOptions options) |
| 104 | { |
| 105 | if (element.ValueKind == JsonValueKind.Null) |
| 106 | { |
| 107 | return null; |
| 108 | } |
| 109 | ChatOutputAudioVoice outputAudioVoice = default; |
| 110 | ChatOutputAudioFormat outputAudioFormat = default; |
| 111 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 112 | JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 113 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 114 | foreach (var prop in element.EnumerateObject()) |
| 115 | { |
| 116 | if (prop.NameEquals("voice"u8)) |
| 117 | { |
| 118 | outputAudioVoice = new ChatOutputAudioVoice(prop.Value.GetString()); |
| 119 | continue; |
| 120 | } |
| 121 | if (prop.NameEquals("format"u8)) |
| 122 | { |
| 123 | outputAudioFormat = new ChatOutputAudioFormat(prop.Value.GetString()); |
| 124 | continue; |
| 125 | } |
| 126 | patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); |
| 127 | } |
| 128 | return new ChatAudioOptions(outputAudioVoice, outputAudioFormat, patch); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |