openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Files/OpenAIFileCollection.Serialization.cs
90lines · 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.OpenAIFileCollection>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 9 | public partial class OpenAIFileCollection : IJsonModel<OpenAIFileCollection> |
| 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<OpenAIFileCollection>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => CustomSerializationHelpers.SerializeInstance(this, SerializeOpenAIFileCollection, writer, options); |
| 16 | |
| 17 | internal static void SerializeOpenAIFileCollection(OpenAIFileCollection 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<OpenAIFile>(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 OpenAIFileCollection DeserializeOpenAIFileCollection(JsonElement element, ModelReaderWriterOptions options = null) |
| 35 | { |
| 36 | options ??= ModelSerializationExtensions.WireOptions; |
| 37 | |
| 38 | if (element.ValueKind == JsonValueKind.Null) |
| 39 | { |
| 40 | return null; |
| 41 | } |
| 42 | IReadOnlyList<OpenAIFile> data = default; |
| 43 | string @object = default; |
| 44 | string firstId = default; |
| 45 | string lastId = default; |
| 46 | bool hasMore = false; |
| 47 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 48 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 49 | foreach (var property in element.EnumerateObject()) |
| 50 | { |
| 51 | if (property.NameEquals("data"u8)) |
| 52 | { |
| 53 | List<OpenAIFile> array = new List<OpenAIFile>(); |
| 54 | foreach (var item in property.Value.EnumerateArray()) |
| 55 | { |
| 56 | array.Add(OpenAIFile.DeserializeOpenAIFile(item, options)); |
| 57 | } |
| 58 | data = array; |
| 59 | continue; |
| 60 | } |
| 61 | if (property.NameEquals("object"u8)) |
| 62 | { |
| 63 | @object = property.Value.GetString(); |
| 64 | continue; |
| 65 | } |
| 66 | if (property.NameEquals("first_id"u8)) |
| 67 | { |
| 68 | firstId = property.Value.GetString(); |
| 69 | continue; |
| 70 | } |
| 71 | if (property.NameEquals("last_id"u8)) |
| 72 | { |
| 73 | lastId = property.Value.GetString(); |
| 74 | continue; |
| 75 | } |
| 76 | if (property.NameEquals("has_more"u8)) |
| 77 | { |
| 78 | hasMore = property.Value.GetBoolean(); |
| 79 | continue; |
| 80 | } |
| 81 | if (true) |
| 82 | { |
| 83 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 84 | } |
| 85 | } |
| 86 | serializedAdditionalRawData = rawDataDictionary; |
| 87 | return new OpenAIFileCollection(data, @object, firstId, lastId, hasMore, serializedAdditionalRawData); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |