openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Images/GeneratedImageCollection.Serialization.cs
71lines · modeblame
58f93c8dShivangiReja2 years ago | 1 | using System; |
9f9f2936Jose Arriaga Maldonado2 years ago | 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; | |
| 4 | using System.Text.Json; | |
| 5 | | |
| 6 | namespace OpenAI.Images; | |
| 7 | | |
| 8 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Images.GeneratedImageCollection>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] | |
| 9 | public partial class GeneratedImageCollection : IJsonModel<GeneratedImageCollection> | |
| 10 | { | |
| 11 | // CUSTOM: | |
| 12 | // - Serialized the Items property. | |
19fc7d54Jose Arriaga Maldonado2 years ago | 13 | // - Recovered the serialization of SerializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
9f9f2936Jose Arriaga Maldonado2 years ago | 14 | void IJsonModel<GeneratedImageCollection>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => CustomSerializationHelpers.SerializeInstance(this, SerializeGeneratedImageCollection, writer, options); | |
| 16 | | |
| 17 | internal static void SerializeGeneratedImageCollection(GeneratedImageCollection instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) | |
| 18 | { | |
| 19 | writer.WriteStartObject(); | |
| 20 | writer.WritePropertyName("created"u8); | |
| 21 | writer.WriteNumberValue(instance.CreatedAt, "U"); | |
| 22 | writer.WritePropertyName("data"u8); | |
| 23 | writer.WriteStartArray(); | |
| 24 | foreach (var item in instance.Items) | |
| 25 | { | |
| 26 | writer.WriteObjectValue<GeneratedImage>(item, options); | |
| 27 | } | |
| 28 | writer.WriteEndArray(); | |
19fc7d54Jose Arriaga Maldonado2 years ago | 29 | writer.WriteSerializedAdditionalRawData(instance.SerializedAdditionalRawData, options); |
9f9f2936Jose Arriaga Maldonado2 years ago | 30 | writer.WriteEndObject(); |
| 31 | } | |
| 32 | | |
19fc7d54Jose Arriaga Maldonado2 years ago | 33 | // CUSTOM: Recovered the deserialization of SerializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
9f9f2936Jose Arriaga Maldonado2 years ago | 34 | internal static GeneratedImageCollection DeserializeGeneratedImageCollection(JsonElement element, ModelReaderWriterOptions options = null) |
| 35 | { | |
| 36 | options ??= ModelSerializationExtensions.WireOptions; | |
| 37 | | |
| 38 | if (element.ValueKind == JsonValueKind.Null) | |
| 39 | { | |
| 40 | return null; | |
| 41 | } | |
| 42 | DateTimeOffset created = default; | |
| 43 | IReadOnlyList<GeneratedImage> data = 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("created"u8)) | |
| 49 | { | |
| 50 | created = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64()); | |
| 51 | continue; | |
| 52 | } | |
| 53 | if (property.NameEquals("data"u8)) | |
| 54 | { | |
| 55 | List<GeneratedImage> array = new List<GeneratedImage>(); | |
| 56 | foreach (var item in property.Value.EnumerateArray()) | |
| 57 | { | |
| 58 | array.Add(GeneratedImage.DeserializeGeneratedImage(item, options)); | |
| 59 | } | |
| 60 | data = array; | |
| 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 GeneratedImageCollection(created, data, serializedAdditionalRawData); | |
| 70 | } | |
| 71 | } |