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