openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Custom/Audio/AudioTranscriptionChunkingStrategy.Serialization.cs
59lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | namespace OpenAI.Audio; |
| 7 | |
| 8 | // CUSTOM: This type is not its own object. Instead, it represents a union, and as such, it must directly forward |
| 9 | // its serialization and deserialization logic to the components of said union. |
| 10 | public partial class AudioTranscriptionChunkingStrategy |
| 11 | { |
| 12 | // CUSTOM: Edited to remove calls to WriteStartObject() and WriteEndObject(). |
| 13 | void IJsonModel<AudioTranscriptionChunkingStrategy>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 14 | { |
| 15 | JsonModelWriteCore(writer, options); |
| 16 | } |
| 17 | |
| 18 | // CUSTOM: Edited to serialize the different components of the union. |
| 19 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 20 | { |
| 21 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranscriptionChunkingStrategy>)this).GetFormatFromOptions(options) : options.Format; |
| 22 | if (format != "J") |
| 23 | { |
| 24 | throw new FormatException($"The model {nameof(AudioTranscriptionChunkingStrategy)} does not support writing '{format}' format."); |
| 25 | } |
| 26 | if (Optional.IsDefined(DefaultChunkingStrategy)) |
| 27 | { |
| 28 | writer.WriteStringValue(DefaultChunkingStrategy.Value.ToString()); |
| 29 | } |
| 30 | if (Optional.IsDefined(CustomChunkingStrategy)) |
| 31 | { |
| 32 | writer.WriteObjectValue(CustomChunkingStrategy, options); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // CUSTOM: Edited to deserialize the different components of the union. |
| 37 | internal static AudioTranscriptionChunkingStrategy DeserializeAudioTranscriptionChunkingStrategy(JsonElement element, ModelReaderWriterOptions options) |
| 38 | { |
| 39 | if (element.ValueKind == JsonValueKind.Null) |
| 40 | { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | AudioTranscriptionDefaultChunkingStrategy? defaultChunkingStrategy = default; |
| 45 | AudioTranscriptionCustomChunkingStrategy customChunkingStrategy = default; |
| 46 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 47 | |
| 48 | if (element.ValueKind == JsonValueKind.String) |
| 49 | { |
| 50 | defaultChunkingStrategy = new AudioTranscriptionDefaultChunkingStrategy(element.GetString()); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | customChunkingStrategy = AudioTranscriptionCustomChunkingStrategy.DeserializeAudioTranscriptionCustomChunkingStrategy(element, options); |
| 55 | } |
| 56 | |
| 57 | return new AudioTranscriptionChunkingStrategy(defaultChunkingStrategy, customChunkingStrategy, additionalBinaryDataProperties); |
| 58 | } |
| 59 | } |
| 60 | |