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