openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Graders/Grader.Serialization.cs
133lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel.Primitives; |
| 7 | using System.Text.Json; |
| 8 | using OpenAI; |
| 9 | |
| 10 | namespace OpenAI.Graders |
| 11 | { |
| 12 | [PersistableModelProxy(typeof(UnknownGrader))] |
| 13 | public partial class Grader : IJsonModel<Grader> |
| 14 | { |
| 15 | internal Grader() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void IJsonModel<Grader>.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<Grader>)this).GetFormatFromOptions(options) : options.Format; |
| 29 | if (format != "J") |
| 30 | { |
| 31 | throw new FormatException($"The model {nameof(Grader)} does not support writing '{format}' format."); |
| 32 | } |
| 33 | if (_additionalBinaryDataProperties?.ContainsKey("type") != true) |
| 34 | { |
| 35 | writer.WritePropertyName("type"u8); |
| 36 | writer.WriteStringValue(Kind.ToString()); |
| 37 | } |
| 38 | // Plugin customization: remove options.Format != "W" check |
| 39 | if (_additionalBinaryDataProperties != null) |
| 40 | { |
| 41 | foreach (var item in _additionalBinaryDataProperties) |
| 42 | { |
| 43 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 44 | { |
| 45 | continue; |
| 46 | } |
| 47 | writer.WritePropertyName(item.Key); |
| 48 | #if NET6_0_OR_GREATER |
| 49 | writer.WriteRawValue(item.Value); |
| 50 | #else |
| 51 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 52 | { |
| 53 | JsonSerializer.Serialize(writer, document.RootElement); |
| 54 | } |
| 55 | #endif |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | Grader IJsonModel<Grader>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 61 | |
| 62 | protected virtual Grader JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 63 | { |
| 64 | string format = options.Format == "W" ? ((IPersistableModel<Grader>)this).GetFormatFromOptions(options) : options.Format; |
| 65 | if (format != "J") |
| 66 | { |
| 67 | throw new FormatException($"The model {nameof(Grader)} does not support reading '{format}' format."); |
| 68 | } |
| 69 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 70 | return DeserializeGrader(document.RootElement, options); |
| 71 | } |
| 72 | |
| 73 | internal static Grader DeserializeGrader(JsonElement element, ModelReaderWriterOptions options) |
| 74 | { |
| 75 | if (element.ValueKind == JsonValueKind.Null) |
| 76 | { |
| 77 | return null; |
| 78 | } |
| 79 | if (element.TryGetProperty("type"u8, out JsonElement discriminator)) |
| 80 | { |
| 81 | switch (discriminator.GetString()) |
| 82 | { |
| 83 | case "string_check": |
| 84 | return GraderStringCheck.DeserializeGraderStringCheck(element, options); |
| 85 | case "text_similarity": |
| 86 | return GraderTextSimilarity.DeserializeGraderTextSimilarity(element, options); |
| 87 | case "python": |
| 88 | return GraderPython.DeserializeGraderPython(element, options); |
| 89 | case "score_model": |
| 90 | return GraderScoreModel.DeserializeGraderScoreModel(element, options); |
| 91 | case "multi": |
| 92 | return GraderMulti.DeserializeGraderMulti(element, options); |
| 93 | case "label_model": |
| 94 | return GraderLabelModel.DeserializeGraderLabelModel(element, options); |
| 95 | } |
| 96 | } |
| 97 | return UnknownGrader.DeserializeUnknownGrader(element, options); |
| 98 | } |
| 99 | |
| 100 | BinaryData IPersistableModel<Grader>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 101 | |
| 102 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 103 | { |
| 104 | string format = options.Format == "W" ? ((IPersistableModel<Grader>)this).GetFormatFromOptions(options) : options.Format; |
| 105 | switch (format) |
| 106 | { |
| 107 | case "J": |
| 108 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 109 | default: |
| 110 | throw new FormatException($"The model {nameof(Grader)} does not support writing '{options.Format}' format."); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | Grader IPersistableModel<Grader>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 115 | |
| 116 | protected virtual Grader PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 117 | { |
| 118 | string format = options.Format == "W" ? ((IPersistableModel<Grader>)this).GetFormatFromOptions(options) : options.Format; |
| 119 | switch (format) |
| 120 | { |
| 121 | case "J": |
| 122 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 123 | { |
| 124 | return DeserializeGrader(document.RootElement, options); |
| 125 | } |
| 126 | default: |
| 127 | throw new FormatException($"The model {nameof(Grader)} does not support reading '{options.Format}' format."); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | string IPersistableModel<Grader>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 132 | } |
| 133 | } |
| 134 | |