openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Audio/AudioTranslation.Serialization.cs
197lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel.Primitives; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Diagnostics.CodeAnalysis; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | |
| 12 | namespace OpenAI.Audio |
| 13 | { |
| 14 | public partial class AudioTranslation : IJsonModel<AudioTranslation> |
| 15 | { |
| 16 | internal AudioTranslation() : this(null, null, null, null, default, null) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | void IJsonModel<AudioTranslation>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 21 | { |
| 22 | writer.WriteStartObject(); |
| 23 | JsonModelWriteCore(writer, options); |
| 24 | writer.WriteEndObject(); |
| 25 | } |
| 26 | |
| 27 | [Experimental("OPENAI001")] |
| 28 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 29 | { |
| 30 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranslation>)this).GetFormatFromOptions(options) : options.Format; |
| 31 | if (format != "J") |
| 32 | { |
| 33 | throw new FormatException($"The model {nameof(AudioTranslation)} does not support writing '{format}' format."); |
| 34 | } |
| 35 | if (_additionalBinaryDataProperties?.ContainsKey("language") != true) |
| 36 | { |
| 37 | writer.WritePropertyName("language"u8); |
| 38 | writer.WriteStringValue(Language); |
| 39 | } |
| 40 | if (_additionalBinaryDataProperties?.ContainsKey("text") != true) |
| 41 | { |
| 42 | writer.WritePropertyName("text"u8); |
| 43 | writer.WriteStringValue(Text); |
| 44 | } |
| 45 | // Plugin customization: remove options.Format != "W" check |
| 46 | if (Optional.IsCollectionDefined(Segments) && _additionalBinaryDataProperties?.ContainsKey("segments") != true) |
| 47 | { |
| 48 | writer.WritePropertyName("segments"u8); |
| 49 | writer.WriteStartArray(); |
| 50 | foreach (TranscribedSegment item in Segments) |
| 51 | { |
| 52 | writer.WriteObjectValue(item, options); |
| 53 | } |
| 54 | writer.WriteEndArray(); |
| 55 | } |
| 56 | if (_additionalBinaryDataProperties?.ContainsKey("task") != true) |
| 57 | { |
| 58 | writer.WritePropertyName("task"u8); |
| 59 | writer.WriteStringValue(Task); |
| 60 | } |
| 61 | if (_additionalBinaryDataProperties?.ContainsKey("duration") != true) |
| 62 | { |
| 63 | writer.WritePropertyName("duration"u8); |
| 64 | writer.WriteNumberValue(Convert.ToDouble(Duration.Value.ToString("s\\.FFF"))); |
| 65 | } |
| 66 | // Plugin customization: remove options.Format != "W" check |
| 67 | if (_additionalBinaryDataProperties != null) |
| 68 | { |
| 69 | foreach (var item in _additionalBinaryDataProperties) |
| 70 | { |
| 71 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 72 | { |
| 73 | continue; |
| 74 | } |
| 75 | writer.WritePropertyName(item.Key); |
| 76 | #if NET6_0_OR_GREATER |
| 77 | writer.WriteRawValue(item.Value); |
| 78 | #else |
| 79 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 80 | { |
| 81 | JsonSerializer.Serialize(writer, document.RootElement); |
| 82 | } |
| 83 | #endif |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | AudioTranslation IJsonModel<AudioTranslation>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 89 | |
| 90 | [Experimental("OPENAI001")] |
| 91 | protected virtual AudioTranslation JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 92 | { |
| 93 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranslation>)this).GetFormatFromOptions(options) : options.Format; |
| 94 | if (format != "J") |
| 95 | { |
| 96 | throw new FormatException($"The model {nameof(AudioTranslation)} does not support reading '{format}' format."); |
| 97 | } |
| 98 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 99 | return DeserializeAudioTranslation(document.RootElement, options); |
| 100 | } |
| 101 | |
| 102 | internal static AudioTranslation DeserializeAudioTranslation(JsonElement element, ModelReaderWriterOptions options) |
| 103 | { |
| 104 | if (element.ValueKind == JsonValueKind.Null) |
| 105 | { |
| 106 | return null; |
| 107 | } |
| 108 | string language = default; |
| 109 | string text = default; |
| 110 | IReadOnlyList<TranscribedSegment> segments = default; |
| 111 | string task = default; |
| 112 | TimeSpan? duration = default; |
| 113 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 114 | foreach (var prop in element.EnumerateObject()) |
| 115 | { |
| 116 | if (prop.NameEquals("language"u8)) |
| 117 | { |
| 118 | language = prop.Value.GetString(); |
| 119 | continue; |
| 120 | } |
| 121 | if (prop.NameEquals("text"u8)) |
| 122 | { |
| 123 | text = prop.Value.GetString(); |
| 124 | continue; |
| 125 | } |
| 126 | if (prop.NameEquals("segments"u8)) |
| 127 | { |
| 128 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 129 | { |
| 130 | continue; |
| 131 | } |
| 132 | List<TranscribedSegment> array = new List<TranscribedSegment>(); |
| 133 | foreach (var item in prop.Value.EnumerateArray()) |
| 134 | { |
| 135 | array.Add(TranscribedSegment.DeserializeTranscribedSegment(item, options)); |
| 136 | } |
| 137 | segments = array; |
| 138 | continue; |
| 139 | } |
| 140 | if (prop.NameEquals("task"u8)) |
| 141 | { |
| 142 | task = prop.Value.GetString(); |
| 143 | continue; |
| 144 | } |
| 145 | if (prop.NameEquals("duration"u8)) |
| 146 | { |
| 147 | duration = TimeSpan.FromSeconds(prop.Value.GetDouble()); |
| 148 | continue; |
| 149 | } |
| 150 | // Plugin customization: remove options.Format != "W" check |
| 151 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 152 | } |
| 153 | return new AudioTranslation( |
| 154 | language, |
| 155 | text, |
| 156 | segments ?? new ChangeTrackingList<TranscribedSegment>(), |
| 157 | task, |
| 158 | duration, |
| 159 | additionalBinaryDataProperties); |
| 160 | } |
| 161 | |
| 162 | BinaryData IPersistableModel<AudioTranslation>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 163 | |
| 164 | [Experimental("OPENAI001")] |
| 165 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 166 | { |
| 167 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranslation>)this).GetFormatFromOptions(options) : options.Format; |
| 168 | switch (format) |
| 169 | { |
| 170 | case "J": |
| 171 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 172 | default: |
| 173 | throw new FormatException($"The model {nameof(AudioTranslation)} does not support writing '{options.Format}' format."); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | AudioTranslation IPersistableModel<AudioTranslation>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 178 | |
| 179 | [Experimental("OPENAI001")] |
| 180 | protected virtual AudioTranslation PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 181 | { |
| 182 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranslation>)this).GetFormatFromOptions(options) : options.Format; |
| 183 | switch (format) |
| 184 | { |
| 185 | case "J": |
| 186 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 187 | { |
| 188 | return DeserializeAudioTranslation(document.RootElement, options); |
| 189 | } |
| 190 | default: |
| 191 | throw new FormatException($"The model {nameof(AudioTranslation)} does not support reading '{options.Format}' format."); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | string IPersistableModel<AudioTranslation>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 196 | } |
| 197 | } |
| 198 | |