openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/FineTuning/FineTuningEvent.Serialization.cs
216lines · 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 FineTuningEvent : IJsonModel<FineTuningEvent> |
| 14 | { |
| 15 | internal FineTuningEvent() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | protected virtual FineTuningEvent PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 20 | { |
| 21 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningEvent>)this).GetFormatFromOptions(options) : options.Format; |
| 22 | switch (format) |
| 23 | { |
| 24 | case "J": |
| 25 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 26 | { |
| 27 | return DeserializeFineTuningEvent(document.RootElement, options); |
| 28 | } |
| 29 | default: |
| 30 | throw new FormatException($"The model {nameof(FineTuningEvent)} does not support reading '{options.Format}' format."); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 35 | { |
| 36 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningEvent>)this).GetFormatFromOptions(options) : options.Format; |
| 37 | switch (format) |
| 38 | { |
| 39 | case "J": |
| 40 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 41 | default: |
| 42 | throw new FormatException($"The model {nameof(FineTuningEvent)} does not support writing '{options.Format}' format."); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | BinaryData IPersistableModel<FineTuningEvent>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 47 | |
| 48 | FineTuningEvent IPersistableModel<FineTuningEvent>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 49 | |
| 50 | string IPersistableModel<FineTuningEvent>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 51 | |
| 52 | void IJsonModel<FineTuningEvent>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 53 | { |
| 54 | writer.WriteStartObject(); |
| 55 | JsonModelWriteCore(writer, options); |
| 56 | writer.WriteEndObject(); |
| 57 | } |
| 58 | |
| 59 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 60 | { |
| 61 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningEvent>)this).GetFormatFromOptions(options) : options.Format; |
| 62 | if (format != "J") |
| 63 | { |
| 64 | throw new FormatException($"The model {nameof(FineTuningEvent)} does not support writing '{format}' format."); |
| 65 | } |
| 66 | if (_additionalBinaryDataProperties?.ContainsKey("id") != true) |
| 67 | { |
| 68 | writer.WritePropertyName("id"u8); |
| 69 | writer.WriteStringValue(Id); |
| 70 | } |
| 71 | if (_additionalBinaryDataProperties?.ContainsKey("created_at") != true) |
| 72 | { |
| 73 | writer.WritePropertyName("created_at"u8); |
| 74 | writer.WriteNumberValue(CreatedAt, "U"); |
| 75 | } |
| 76 | if (_additionalBinaryDataProperties?.ContainsKey("message") != true) |
| 77 | { |
| 78 | writer.WritePropertyName("message"u8); |
| 79 | writer.WriteStringValue(Message); |
| 80 | } |
| 81 | if (Optional.IsDefined(Kind) && _additionalBinaryDataProperties?.ContainsKey("type") != true) |
| 82 | { |
| 83 | writer.WritePropertyName("type"u8); |
| 84 | writer.WriteStringValue(Kind.Value.ToString()); |
| 85 | } |
| 86 | if (Optional.IsDefined(Data) && _additionalBinaryDataProperties?.ContainsKey("data") != true) |
| 87 | { |
| 88 | writer.WritePropertyName("data"u8); |
| 89 | #if NET6_0_OR_GREATER |
| 90 | writer.WriteRawValue(Data); |
| 91 | #else |
| 92 | using (JsonDocument document = JsonDocument.Parse(Data)) |
| 93 | { |
| 94 | JsonSerializer.Serialize(writer, document.RootElement); |
| 95 | } |
| 96 | #endif |
| 97 | } |
| 98 | if (_additionalBinaryDataProperties?.ContainsKey("level") != true) |
| 99 | { |
| 100 | writer.WritePropertyName("level"u8); |
| 101 | writer.WriteStringValue(Level); |
| 102 | } |
| 103 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 104 | { |
| 105 | writer.WritePropertyName("object"u8); |
| 106 | writer.WriteStringValue(_object); |
| 107 | } |
| 108 | // Plugin customization: remove options.Format != "W" check |
| 109 | if (_additionalBinaryDataProperties != null) |
| 110 | { |
| 111 | foreach (var item in _additionalBinaryDataProperties) |
| 112 | { |
| 113 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | writer.WritePropertyName(item.Key); |
| 118 | #if NET6_0_OR_GREATER |
| 119 | writer.WriteRawValue(item.Value); |
| 120 | #else |
| 121 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 122 | { |
| 123 | JsonSerializer.Serialize(writer, document.RootElement); |
| 124 | } |
| 125 | #endif |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | FineTuningEvent IJsonModel<FineTuningEvent>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 131 | |
| 132 | protected virtual FineTuningEvent JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 133 | { |
| 134 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningEvent>)this).GetFormatFromOptions(options) : options.Format; |
| 135 | if (format != "J") |
| 136 | { |
| 137 | throw new FormatException($"The model {nameof(FineTuningEvent)} does not support reading '{format}' format."); |
| 138 | } |
| 139 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 140 | return DeserializeFineTuningEvent(document.RootElement, options); |
| 141 | } |
| 142 | |
| 143 | internal static FineTuningEvent DeserializeFineTuningEvent(JsonElement element, ModelReaderWriterOptions options) |
| 144 | { |
| 145 | if (element.ValueKind == JsonValueKind.Null) |
| 146 | { |
| 147 | return null; |
| 148 | } |
| 149 | string id = default; |
| 150 | DateTimeOffset createdAt = default; |
| 151 | string message = default; |
| 152 | FineTuningJobEventKind? kind = default; |
| 153 | BinaryData data = default; |
| 154 | string level = default; |
| 155 | string @object = default; |
| 156 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 157 | foreach (var prop in element.EnumerateObject()) |
| 158 | { |
| 159 | if (prop.NameEquals("id"u8)) |
| 160 | { |
| 161 | id = prop.Value.GetString(); |
| 162 | continue; |
| 163 | } |
| 164 | if (prop.NameEquals("created_at"u8)) |
| 165 | { |
| 166 | createdAt = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 167 | continue; |
| 168 | } |
| 169 | if (prop.NameEquals("message"u8)) |
| 170 | { |
| 171 | message = prop.Value.GetString(); |
| 172 | continue; |
| 173 | } |
| 174 | if (prop.NameEquals("type"u8)) |
| 175 | { |
| 176 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 177 | { |
| 178 | continue; |
| 179 | } |
| 180 | kind = new FineTuningJobEventKind(prop.Value.GetString()); |
| 181 | continue; |
| 182 | } |
| 183 | if (prop.NameEquals("data"u8)) |
| 184 | { |
| 185 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 186 | { |
| 187 | continue; |
| 188 | } |
| 189 | data = BinaryData.FromString(prop.Value.GetRawText()); |
| 190 | continue; |
| 191 | } |
| 192 | if (prop.NameEquals("level"u8)) |
| 193 | { |
| 194 | level = prop.Value.GetString(); |
| 195 | continue; |
| 196 | } |
| 197 | if (prop.NameEquals("object"u8)) |
| 198 | { |
| 199 | @object = prop.Value.GetString(); |
| 200 | continue; |
| 201 | } |
| 202 | // Plugin customization: remove options.Format != "W" check |
| 203 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 204 | } |
| 205 | return new FineTuningEvent( |
| 206 | id, |
| 207 | createdAt, |
| 208 | message, |
| 209 | kind, |
| 210 | data, |
| 211 | level, |
| 212 | @object, |
| 213 | additionalBinaryDataProperties); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |