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