openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Evals/InternalEval.Serialization.cs
266lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.ClientModel.Primitives; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | |
| 12 | namespace OpenAI.Evals |
| 13 | { |
| 14 | internal partial class InternalEval : IJsonModel<InternalEval> |
| 15 | { |
| 16 | internal InternalEval() : this(null, null, null, null, null, default, null, null) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | void IJsonModel<InternalEval>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 21 | { |
| 22 | writer.WriteStartObject(); |
| 23 | JsonModelWriteCore(writer, options); |
| 24 | writer.WriteEndObject(); |
| 25 | } |
| 26 | |
| 27 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 28 | { |
| 29 | string format = options.Format == "W" ? ((IPersistableModel<InternalEval>)this).GetFormatFromOptions(options) : options.Format; |
| 30 | if (format != "J") |
| 31 | { |
| 32 | throw new FormatException($"The model {nameof(InternalEval)} does not support writing '{format}' format."); |
| 33 | } |
| 34 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 35 | { |
| 36 | writer.WritePropertyName("object"u8); |
| 37 | writer.WriteStringValue(Object); |
| 38 | } |
| 39 | if (_additionalBinaryDataProperties?.ContainsKey("id") != true) |
| 40 | { |
| 41 | writer.WritePropertyName("id"u8); |
| 42 | writer.WriteStringValue(Id); |
| 43 | } |
| 44 | if (_additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 45 | { |
| 46 | writer.WritePropertyName("name"u8); |
| 47 | writer.WriteStringValue(Name); |
| 48 | } |
| 49 | if (_additionalBinaryDataProperties?.ContainsKey("data_source_config") != true) |
| 50 | { |
| 51 | writer.WritePropertyName("data_source_config"u8); |
| 52 | writer.WriteObjectValue(DataSourceConfig, options); |
| 53 | } |
| 54 | if (_additionalBinaryDataProperties?.ContainsKey("testing_criteria") != true) |
| 55 | { |
| 56 | if (Optional.IsCollectionDefined(TestingCriteria)) |
| 57 | { |
| 58 | writer.WritePropertyName("testing_criteria"u8); |
| 59 | writer.WriteStartArray(); |
| 60 | foreach (InternalEvalGraderResource item in TestingCriteria) |
| 61 | { |
| 62 | writer.WriteObjectValue(item, options); |
| 63 | } |
| 64 | writer.WriteEndArray(); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | writer.WriteNull("testing_criteria"u8); |
| 69 | } |
| 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("metadata") != true) |
| 77 | { |
| 78 | if (Optional.IsCollectionDefined(Metadata)) |
| 79 | { |
| 80 | writer.WritePropertyName("metadata"u8); |
| 81 | writer.WriteStartObject(); |
| 82 | foreach (var item in Metadata) |
| 83 | { |
| 84 | writer.WritePropertyName(item.Key); |
| 85 | if (item.Value == null) |
| 86 | { |
| 87 | writer.WriteNullValue(); |
| 88 | continue; |
| 89 | } |
| 90 | writer.WriteStringValue(item.Value); |
| 91 | } |
| 92 | writer.WriteEndObject(); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | writer.WriteNull("metadata"u8); |
| 97 | } |
| 98 | } |
| 99 | // Plugin customization: remove options.Format != "W" check |
| 100 | if (_additionalBinaryDataProperties != null) |
| 101 | { |
| 102 | foreach (var item in _additionalBinaryDataProperties) |
| 103 | { |
| 104 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 105 | { |
| 106 | continue; |
| 107 | } |
| 108 | writer.WritePropertyName(item.Key); |
| 109 | #if NET6_0_OR_GREATER |
| 110 | writer.WriteRawValue(item.Value); |
| 111 | #else |
| 112 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 113 | { |
| 114 | JsonSerializer.Serialize(writer, document.RootElement); |
| 115 | } |
| 116 | #endif |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | InternalEval IJsonModel<InternalEval>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 122 | |
| 123 | protected virtual InternalEval JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 124 | { |
| 125 | string format = options.Format == "W" ? ((IPersistableModel<InternalEval>)this).GetFormatFromOptions(options) : options.Format; |
| 126 | if (format != "J") |
| 127 | { |
| 128 | throw new FormatException($"The model {nameof(InternalEval)} does not support reading '{format}' format."); |
| 129 | } |
| 130 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 131 | return DeserializeInternalEval(document.RootElement, options); |
| 132 | } |
| 133 | |
| 134 | internal static InternalEval DeserializeInternalEval(JsonElement element, ModelReaderWriterOptions options) |
| 135 | { |
| 136 | if (element.ValueKind == JsonValueKind.Null) |
| 137 | { |
| 138 | return null; |
| 139 | } |
| 140 | string @object = default; |
| 141 | string id = default; |
| 142 | string name = default; |
| 143 | InternalEvalDataSourceConfigResource dataSourceConfig = default; |
| 144 | IList<InternalEvalGraderResource> testingCriteria = default; |
| 145 | DateTimeOffset createdAt = default; |
| 146 | IDictionary<string, string> metadata = default; |
| 147 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 148 | foreach (var prop in element.EnumerateObject()) |
| 149 | { |
| 150 | if (prop.NameEquals("object"u8)) |
| 151 | { |
| 152 | @object = prop.Value.GetString(); |
| 153 | continue; |
| 154 | } |
| 155 | if (prop.NameEquals("id"u8)) |
| 156 | { |
| 157 | id = prop.Value.GetString(); |
| 158 | continue; |
| 159 | } |
| 160 | if (prop.NameEquals("name"u8)) |
| 161 | { |
| 162 | name = prop.Value.GetString(); |
| 163 | continue; |
| 164 | } |
| 165 | if (prop.NameEquals("data_source_config"u8)) |
| 166 | { |
| 167 | dataSourceConfig = InternalEvalDataSourceConfigResource.DeserializeInternalEvalDataSourceConfigResource(prop.Value, options); |
| 168 | continue; |
| 169 | } |
| 170 | if (prop.NameEquals("testing_criteria"u8)) |
| 171 | { |
| 172 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 173 | { |
| 174 | testingCriteria = new ChangeTrackingList<InternalEvalGraderResource>(); |
| 175 | continue; |
| 176 | } |
| 177 | List<InternalEvalGraderResource> array = new List<InternalEvalGraderResource>(); |
| 178 | foreach (var item in prop.Value.EnumerateArray()) |
| 179 | { |
| 180 | array.Add(InternalEvalGraderResource.DeserializeInternalEvalGraderResource(item, options)); |
| 181 | } |
| 182 | testingCriteria = array; |
| 183 | continue; |
| 184 | } |
| 185 | if (prop.NameEquals("created_at"u8)) |
| 186 | { |
| 187 | createdAt = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 188 | continue; |
| 189 | } |
| 190 | if (prop.NameEquals("metadata"u8)) |
| 191 | { |
| 192 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 193 | { |
| 194 | metadata = new ChangeTrackingDictionary<string, string>(); |
| 195 | continue; |
| 196 | } |
| 197 | Dictionary<string, string> dictionary = new Dictionary<string, string>(); |
| 198 | foreach (var prop0 in prop.Value.EnumerateObject()) |
| 199 | { |
| 200 | if (prop0.Value.ValueKind == JsonValueKind.Null) |
| 201 | { |
| 202 | dictionary.Add(prop0.Name, null); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | dictionary.Add(prop0.Name, prop0.Value.GetString()); |
| 207 | } |
| 208 | } |
| 209 | metadata = dictionary; |
| 210 | continue; |
| 211 | } |
| 212 | // Plugin customization: remove options.Format != "W" check |
| 213 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 214 | } |
| 215 | return new InternalEval( |
| 216 | @object, |
| 217 | id, |
| 218 | name, |
| 219 | dataSourceConfig, |
| 220 | testingCriteria, |
| 221 | createdAt, |
| 222 | metadata, |
| 223 | additionalBinaryDataProperties); |
| 224 | } |
| 225 | |
| 226 | BinaryData IPersistableModel<InternalEval>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 227 | |
| 228 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 229 | { |
| 230 | string format = options.Format == "W" ? ((IPersistableModel<InternalEval>)this).GetFormatFromOptions(options) : options.Format; |
| 231 | switch (format) |
| 232 | { |
| 233 | case "J": |
| 234 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 235 | default: |
| 236 | throw new FormatException($"The model {nameof(InternalEval)} does not support writing '{options.Format}' format."); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | InternalEval IPersistableModel<InternalEval>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 241 | |
| 242 | protected virtual InternalEval PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 243 | { |
| 244 | string format = options.Format == "W" ? ((IPersistableModel<InternalEval>)this).GetFormatFromOptions(options) : options.Format; |
| 245 | switch (format) |
| 246 | { |
| 247 | case "J": |
| 248 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 249 | { |
| 250 | return DeserializeInternalEval(document.RootElement, options); |
| 251 | } |
| 252 | default: |
| 253 | throw new FormatException($"The model {nameof(InternalEval)} does not support reading '{options.Format}' format."); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | string IPersistableModel<InternalEval>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 258 | |
| 259 | public static explicit operator InternalEval(ClientResult result) |
| 260 | { |
| 261 | using PipelineResponse response = result.GetRawResponse(); |
| 262 | using JsonDocument document = JsonDocument.Parse(response.Content); |
| 263 | return DeserializeInternalEval(document.RootElement, ModelSerializationExtensions.WireOptions); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |