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