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