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