openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Containers/ContainerResource.Serialization.cs
201lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.ClientModel.Primitives; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | |
| 12 | namespace OpenAI.Containers |
| 13 | { |
| 14 | public partial class ContainerResource : IJsonModel<ContainerResource> |
| 15 | { |
| 16 | internal ContainerResource() |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | protected virtual ContainerResource PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 21 | { |
| 22 | string format = options.Format == "W" ? ((IPersistableModel<ContainerResource>)this).GetFormatFromOptions(options) : options.Format; |
| 23 | switch (format) |
| 24 | { |
| 25 | case "J": |
| 26 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 27 | { |
| 28 | return DeserializeContainerResource(document.RootElement, options); |
| 29 | } |
| 30 | default: |
| 31 | throw new FormatException($"The model {nameof(ContainerResource)} does not support reading '{options.Format}' format."); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 36 | { |
| 37 | string format = options.Format == "W" ? ((IPersistableModel<ContainerResource>)this).GetFormatFromOptions(options) : options.Format; |
| 38 | switch (format) |
| 39 | { |
| 40 | case "J": |
| 41 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 42 | default: |
| 43 | throw new FormatException($"The model {nameof(ContainerResource)} does not support writing '{options.Format}' format."); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | BinaryData IPersistableModel<ContainerResource>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 48 | |
| 49 | ContainerResource IPersistableModel<ContainerResource>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 50 | |
| 51 | string IPersistableModel<ContainerResource>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 52 | |
| 53 | public static explicit operator ContainerResource(ClientResult result) |
| 54 | { |
| 55 | PipelineResponse response = result.GetRawResponse(); |
| 56 | using JsonDocument document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); |
| 57 | return DeserializeContainerResource(document.RootElement, ModelSerializationExtensions.WireOptions); |
| 58 | } |
| 59 | |
| 60 | void IJsonModel<ContainerResource>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 61 | { |
| 62 | writer.WriteStartObject(); |
| 63 | JsonModelWriteCore(writer, options); |
| 64 | writer.WriteEndObject(); |
| 65 | } |
| 66 | |
| 67 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 68 | { |
| 69 | string format = options.Format == "W" ? ((IPersistableModel<ContainerResource>)this).GetFormatFromOptions(options) : options.Format; |
| 70 | if (format != "J") |
| 71 | { |
| 72 | throw new FormatException($"The model {nameof(ContainerResource)} does not support writing '{format}' format."); |
| 73 | } |
| 74 | if (_additionalBinaryDataProperties?.ContainsKey("id") != true) |
| 75 | { |
| 76 | writer.WritePropertyName("id"u8); |
| 77 | writer.WriteStringValue(Id); |
| 78 | } |
| 79 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 80 | { |
| 81 | writer.WritePropertyName("object"u8); |
| 82 | writer.WriteStringValue(Object); |
| 83 | } |
| 84 | if (_additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 85 | { |
| 86 | writer.WritePropertyName("name"u8); |
| 87 | writer.WriteStringValue(Name); |
| 88 | } |
| 89 | if (_additionalBinaryDataProperties?.ContainsKey("created_at") != true) |
| 90 | { |
| 91 | writer.WritePropertyName("created_at"u8); |
| 92 | writer.WriteNumberValue(CreatedAt, "U"); |
| 93 | } |
| 94 | if (_additionalBinaryDataProperties?.ContainsKey("status") != true) |
| 95 | { |
| 96 | writer.WritePropertyName("status"u8); |
| 97 | writer.WriteStringValue(Status); |
| 98 | } |
| 99 | if (Optional.IsDefined(ExpiresAfter) && _additionalBinaryDataProperties?.ContainsKey("expires_after") != true) |
| 100 | { |
| 101 | writer.WritePropertyName("expires_after"u8); |
| 102 | writer.WriteObjectValue(ExpiresAfter, options); |
| 103 | } |
| 104 | // Plugin customization: remove options.Format != "W" check |
| 105 | if (_additionalBinaryDataProperties != null) |
| 106 | { |
| 107 | foreach (var item in _additionalBinaryDataProperties) |
| 108 | { |
| 109 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 110 | { |
| 111 | continue; |
| 112 | } |
| 113 | writer.WritePropertyName(item.Key); |
| 114 | #if NET6_0_OR_GREATER |
| 115 | writer.WriteRawValue(item.Value); |
| 116 | #else |
| 117 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 118 | { |
| 119 | JsonSerializer.Serialize(writer, document.RootElement); |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | ContainerResource IJsonModel<ContainerResource>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 127 | |
| 128 | protected virtual ContainerResource JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 129 | { |
| 130 | string format = options.Format == "W" ? ((IPersistableModel<ContainerResource>)this).GetFormatFromOptions(options) : options.Format; |
| 131 | if (format != "J") |
| 132 | { |
| 133 | throw new FormatException($"The model {nameof(ContainerResource)} does not support reading '{format}' format."); |
| 134 | } |
| 135 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 136 | return DeserializeContainerResource(document.RootElement, options); |
| 137 | } |
| 138 | |
| 139 | internal static ContainerResource DeserializeContainerResource(JsonElement element, ModelReaderWriterOptions options) |
| 140 | { |
| 141 | if (element.ValueKind == JsonValueKind.Null) |
| 142 | { |
| 143 | return null; |
| 144 | } |
| 145 | string id = default; |
| 146 | string @object = default; |
| 147 | string name = default; |
| 148 | DateTimeOffset createdAt = default; |
| 149 | string status = default; |
| 150 | ContainerResourceExpiresAfter expiresAfter = default; |
| 151 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 152 | foreach (var prop in element.EnumerateObject()) |
| 153 | { |
| 154 | if (prop.NameEquals("id"u8)) |
| 155 | { |
| 156 | id = prop.Value.GetString(); |
| 157 | continue; |
| 158 | } |
| 159 | if (prop.NameEquals("object"u8)) |
| 160 | { |
| 161 | @object = prop.Value.GetString(); |
| 162 | continue; |
| 163 | } |
| 164 | if (prop.NameEquals("name"u8)) |
| 165 | { |
| 166 | name = prop.Value.GetString(); |
| 167 | continue; |
| 168 | } |
| 169 | if (prop.NameEquals("created_at"u8)) |
| 170 | { |
| 171 | createdAt = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 172 | continue; |
| 173 | } |
| 174 | if (prop.NameEquals("status"u8)) |
| 175 | { |
| 176 | status = prop.Value.GetString(); |
| 177 | continue; |
| 178 | } |
| 179 | if (prop.NameEquals("expires_after"u8)) |
| 180 | { |
| 181 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 182 | { |
| 183 | continue; |
| 184 | } |
| 185 | expiresAfter = ContainerResourceExpiresAfter.DeserializeContainerResourceExpiresAfter(prop.Value, options); |
| 186 | continue; |
| 187 | } |
| 188 | // Plugin customization: remove options.Format != "W" check |
| 189 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 190 | } |
| 191 | return new ContainerResource( |
| 192 | id, |
| 193 | @object, |
| 194 | name, |
| 195 | createdAt, |
| 196 | status, |
| 197 | expiresAfter, |
| 198 | additionalBinaryDataProperties); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |