openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Files/OpenAIFileInfoCollection.Serialization.cs
72lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | namespace OpenAI.Files; |
| 7 | |
| 8 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Files.OpenAIFileInfoCollection>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 9 | public partial class OpenAIFileInfoCollection : IJsonModel<OpenAIFileInfoCollection> |
| 10 | { |
| 11 | // CUSTOM: |
| 12 | // - Serialized the Items property. |
| 13 | // - Recovered the serialization of _serializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
| 14 | void IJsonModel<OpenAIFileInfoCollection>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => CustomSerializationHelpers.SerializeInstance(this, SerializeOpenAIFileInfoCollection, writer, options); |
| 16 | |
| 17 | internal static void SerializeOpenAIFileInfoCollection(OpenAIFileInfoCollection instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | writer.WriteStartObject(); |
| 20 | writer.WritePropertyName("data"u8); |
| 21 | writer.WriteStartArray(); |
| 22 | foreach (var item in instance.Items) |
| 23 | { |
| 24 | writer.WriteObjectValue<OpenAIFileInfo>(item, options); |
| 25 | } |
| 26 | writer.WriteEndArray(); |
| 27 | writer.WritePropertyName("object"u8); |
| 28 | writer.WriteStringValue(instance.Object.ToString()); |
| 29 | writer.WriteSerializedAdditionalRawData(instance._serializedAdditionalRawData, options); |
| 30 | writer.WriteEndObject(); |
| 31 | } |
| 32 | |
| 33 | // CUSTOM: Recovered the deserialization of _serializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
| 34 | internal static OpenAIFileInfoCollection DeserializeOpenAIFileInfoCollection(JsonElement element, ModelReaderWriterOptions options = null) |
| 35 | { |
| 36 | options ??= ModelSerializationExtensions.WireOptions; |
| 37 | |
| 38 | if (element.ValueKind == JsonValueKind.Null) |
| 39 | { |
| 40 | return null; |
| 41 | } |
| 42 | IReadOnlyList<OpenAIFileInfo> data = default; |
| 43 | InternalListFilesResponseObject @object = default; |
| 44 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 45 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 46 | foreach (var property in element.EnumerateObject()) |
| 47 | { |
| 48 | if (property.NameEquals("data"u8)) |
| 49 | { |
| 50 | List<OpenAIFileInfo> array = new List<OpenAIFileInfo>(); |
| 51 | foreach (var item in property.Value.EnumerateArray()) |
| 52 | { |
| 53 | array.Add(OpenAIFileInfo.DeserializeOpenAIFileInfo(item, options)); |
| 54 | } |
| 55 | data = array; |
| 56 | continue; |
| 57 | } |
| 58 | if (property.NameEquals("object"u8)) |
| 59 | { |
| 60 | @object = new InternalListFilesResponseObject(property.Value.GetString()); |
| 61 | continue; |
| 62 | } |
| 63 | if (true) |
| 64 | { |
| 65 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 66 | } |
| 67 | } |
| 68 | serializedAdditionalRawData = rawDataDictionary; |
| 69 | return new OpenAIFileInfoCollection(data, @object, serializedAdditionalRawData); |
| 70 | } |
| 71 | |
| 72 | } |