openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Assistants/AssistantResponseFormat.Serialization.cs
75lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | namespace OpenAI.Assistants; |
| 7 | |
| 8 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Assistants.AssistantResponseFormat>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 9 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Assistants.AssistantResponseFormat>.Create", typeof(Utf8JsonReader), typeof(ModelReaderWriterOptions))] |
| 10 | [CodeGenSuppress("global::System.ClientModel.Primitives.IPersistableModel<OpenAI.Assistants.AssistantResponseFormat>.Write", typeof(ModelReaderWriterOptions))] |
| 11 | [CodeGenSuppress("global::System.ClientModel.Primitives.IPersistableModel<OpenAI.Assistants.AssistantResponseFormat>.Create", typeof(BinaryData), typeof(ModelReaderWriterOptions))] |
| 12 | public partial class AssistantResponseFormat : IJsonModel<AssistantResponseFormat> |
| 13 | { |
| 14 | void IJsonModel<AssistantResponseFormat>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => CustomSerializationHelpers.SerializeInstance(this, SerializeAssistantResponseFormat, writer, options); |
| 16 | |
| 17 | AssistantResponseFormat IJsonModel<AssistantResponseFormat>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 18 | => CustomSerializationHelpers.DeserializeNewInstance(this, DeserializeAssistantResponseFormat, ref reader, options); |
| 19 | |
| 20 | BinaryData IPersistableModel<AssistantResponseFormat>.Write(ModelReaderWriterOptions options) |
| 21 | => CustomSerializationHelpers.SerializeInstance(this, options); |
| 22 | |
| 23 | AssistantResponseFormat IPersistableModel<AssistantResponseFormat>.Create(BinaryData data, ModelReaderWriterOptions options) |
| 24 | => CustomSerializationHelpers.DeserializeNewInstance(this, DeserializeAssistantResponseFormat, data, options); |
| 25 | |
| 26 | internal static void SerializeAssistantResponseFormat(AssistantResponseFormat formatInstance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 27 | { |
| 28 | if (formatInstance._plainTextValue is not null) |
| 29 | { |
| 30 | writer.WriteStringValue(formatInstance._plainTextValue); |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | writer.WriteStartObject(); |
| 35 | writer.WritePropertyName("type"u8); |
| 36 | writer.WriteStringValue(formatInstance._objectType); |
| 37 | writer.WriteSerializedAdditionalRawData(formatInstance._serializedAdditionalRawData, options); |
| 38 | writer.WriteEndObject(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | internal static AssistantResponseFormat DeserializeAssistantResponseFormat(JsonElement element, ModelReaderWriterOptions options = null) |
| 43 | { |
| 44 | options ??= ModelSerializationExtensions.WireOptions; |
| 45 | |
| 46 | string plainTextValue = null; |
| 47 | string objectType = null; |
| 48 | IDictionary<string, BinaryData> rawDataDictionary = new ChangeTrackingDictionary<string, BinaryData>(); |
| 49 | |
| 50 | if (element.ValueKind == JsonValueKind.Null) |
| 51 | { |
| 52 | return null; |
| 53 | } |
| 54 | else if (element.ValueKind == JsonValueKind.String) |
| 55 | { |
| 56 | plainTextValue = element.GetString(); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | foreach (var property in element.EnumerateObject()) |
| 61 | { |
| 62 | if (property.NameEquals("type"u8)) |
| 63 | { |
| 64 | objectType = property.Value.GetString(); |
| 65 | continue; |
| 66 | } |
| 67 | if (true) |
| 68 | { |
| 69 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return new AssistantResponseFormat(plainTextValue, objectType, rawDataDictionary); |
| 74 | } |
| 75 | } |