openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Files/InternalCompleteUploadRequest.Serialization.cs
160lines · 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.Files |
| 12 | { |
| 13 | internal partial class InternalCompleteUploadRequest : IJsonModel<InternalCompleteUploadRequest> |
| 14 | { |
| 15 | internal InternalCompleteUploadRequest() : this(null, null, null) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void IJsonModel<InternalCompleteUploadRequest>.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<InternalCompleteUploadRequest>)this).GetFormatFromOptions(options) : options.Format; |
| 29 | if (format != "J") |
| 30 | { |
| 31 | throw new FormatException($"The model {nameof(InternalCompleteUploadRequest)} does not support writing '{format}' format."); |
| 32 | } |
| 33 | if (_additionalBinaryDataProperties?.ContainsKey("part_ids") != true) |
| 34 | { |
| 35 | writer.WritePropertyName("part_ids"u8); |
| 36 | writer.WriteStartArray(); |
| 37 | foreach (string item in PartIds) |
| 38 | { |
| 39 | if (item == null) |
| 40 | { |
| 41 | writer.WriteNullValue(); |
| 42 | continue; |
| 43 | } |
| 44 | writer.WriteStringValue(item); |
| 45 | } |
| 46 | writer.WriteEndArray(); |
| 47 | } |
| 48 | if (Optional.IsDefined(Md5) && _additionalBinaryDataProperties?.ContainsKey("md5") != true) |
| 49 | { |
| 50 | writer.WritePropertyName("md5"u8); |
| 51 | writer.WriteStringValue(Md5); |
| 52 | } |
| 53 | // Plugin customization: remove options.Format != "W" check |
| 54 | if (_additionalBinaryDataProperties != null) |
| 55 | { |
| 56 | foreach (var item in _additionalBinaryDataProperties) |
| 57 | { |
| 58 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 59 | { |
| 60 | continue; |
| 61 | } |
| 62 | writer.WritePropertyName(item.Key); |
| 63 | #if NET6_0_OR_GREATER |
| 64 | writer.WriteRawValue(item.Value); |
| 65 | #else |
| 66 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 67 | { |
| 68 | JsonSerializer.Serialize(writer, document.RootElement); |
| 69 | } |
| 70 | #endif |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | InternalCompleteUploadRequest IJsonModel<InternalCompleteUploadRequest>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 76 | |
| 77 | protected virtual InternalCompleteUploadRequest JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 78 | { |
| 79 | string format = options.Format == "W" ? ((IPersistableModel<InternalCompleteUploadRequest>)this).GetFormatFromOptions(options) : options.Format; |
| 80 | if (format != "J") |
| 81 | { |
| 82 | throw new FormatException($"The model {nameof(InternalCompleteUploadRequest)} does not support reading '{format}' format."); |
| 83 | } |
| 84 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 85 | return DeserializeInternalCompleteUploadRequest(document.RootElement, options); |
| 86 | } |
| 87 | |
| 88 | internal static InternalCompleteUploadRequest DeserializeInternalCompleteUploadRequest(JsonElement element, ModelReaderWriterOptions options) |
| 89 | { |
| 90 | if (element.ValueKind == JsonValueKind.Null) |
| 91 | { |
| 92 | return null; |
| 93 | } |
| 94 | IList<string> partIds = default; |
| 95 | string md5 = default; |
| 96 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 97 | foreach (var prop in element.EnumerateObject()) |
| 98 | { |
| 99 | if (prop.NameEquals("part_ids"u8)) |
| 100 | { |
| 101 | List<string> array = new List<string>(); |
| 102 | foreach (var item in prop.Value.EnumerateArray()) |
| 103 | { |
| 104 | if (item.ValueKind == JsonValueKind.Null) |
| 105 | { |
| 106 | array.Add(null); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | array.Add(item.GetString()); |
| 111 | } |
| 112 | } |
| 113 | partIds = array; |
| 114 | continue; |
| 115 | } |
| 116 | if (prop.NameEquals("md5"u8)) |
| 117 | { |
| 118 | md5 = prop.Value.GetString(); |
| 119 | continue; |
| 120 | } |
| 121 | // Plugin customization: remove options.Format != "W" check |
| 122 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 123 | } |
| 124 | return new InternalCompleteUploadRequest(partIds, md5, additionalBinaryDataProperties); |
| 125 | } |
| 126 | |
| 127 | BinaryData IPersistableModel<InternalCompleteUploadRequest>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 128 | |
| 129 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 130 | { |
| 131 | string format = options.Format == "W" ? ((IPersistableModel<InternalCompleteUploadRequest>)this).GetFormatFromOptions(options) : options.Format; |
| 132 | switch (format) |
| 133 | { |
| 134 | case "J": |
| 135 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 136 | default: |
| 137 | throw new FormatException($"The model {nameof(InternalCompleteUploadRequest)} does not support writing '{options.Format}' format."); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | InternalCompleteUploadRequest IPersistableModel<InternalCompleteUploadRequest>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 142 | |
| 143 | protected virtual InternalCompleteUploadRequest PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 144 | { |
| 145 | string format = options.Format == "W" ? ((IPersistableModel<InternalCompleteUploadRequest>)this).GetFormatFromOptions(options) : options.Format; |
| 146 | switch (format) |
| 147 | { |
| 148 | case "J": |
| 149 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 150 | { |
| 151 | return DeserializeInternalCompleteUploadRequest(document.RootElement, options); |
| 152 | } |
| 153 | default: |
| 154 | throw new FormatException($"The model {nameof(InternalCompleteUploadRequest)} does not support reading '{options.Format}' format."); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | string IPersistableModel<InternalCompleteUploadRequest>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 159 | } |
| 160 | } |