openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Graders/GraderMulti.Serialization.cs
143lines · 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.Graders |
| 12 | { |
| 13 | public partial class GraderMulti : IJsonModel<GraderMulti> |
| 14 | { |
| 15 | internal GraderMulti() : this(GraderType.Multi, null, null, null, null) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void IJsonModel<GraderMulti>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 20 | { |
| 21 | writer.WriteStartObject(); |
| 22 | JsonModelWriteCore(writer, options); |
| 23 | writer.WriteEndObject(); |
| 24 | } |
| 25 | |
| 26 | protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 27 | { |
| 28 | string format = options.Format == "W" ? ((IPersistableModel<GraderMulti>)this).GetFormatFromOptions(options) : options.Format; |
| 29 | if (format != "J") |
| 30 | { |
| 31 | throw new FormatException($"The model {nameof(GraderMulti)} does not support writing '{format}' format."); |
| 32 | } |
| 33 | base.JsonModelWriteCore(writer, options); |
| 34 | if (_additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 35 | { |
| 36 | writer.WritePropertyName("name"u8); |
| 37 | writer.WriteStringValue(Name); |
| 38 | } |
| 39 | if (_additionalBinaryDataProperties?.ContainsKey("graders") != true) |
| 40 | { |
| 41 | writer.WritePropertyName("graders"u8); |
| 42 | #if NET6_0_OR_GREATER |
| 43 | writer.WriteRawValue(Graders); |
| 44 | #else |
| 45 | using (JsonDocument document = JsonDocument.Parse(Graders)) |
| 46 | { |
| 47 | JsonSerializer.Serialize(writer, document.RootElement); |
| 48 | } |
| 49 | #endif |
| 50 | } |
| 51 | if (_additionalBinaryDataProperties?.ContainsKey("calculate_output") != true) |
| 52 | { |
| 53 | writer.WritePropertyName("calculate_output"u8); |
| 54 | writer.WriteStringValue(CalculateOutput); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | GraderMulti IJsonModel<GraderMulti>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (GraderMulti)JsonModelCreateCore(ref reader, options); |
| 59 | |
| 60 | protected override Grader JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 61 | { |
| 62 | string format = options.Format == "W" ? ((IPersistableModel<GraderMulti>)this).GetFormatFromOptions(options) : options.Format; |
| 63 | if (format != "J") |
| 64 | { |
| 65 | throw new FormatException($"The model {nameof(GraderMulti)} does not support reading '{format}' format."); |
| 66 | } |
| 67 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 68 | return DeserializeGraderMulti(document.RootElement, options); |
| 69 | } |
| 70 | |
| 71 | internal static GraderMulti DeserializeGraderMulti(JsonElement element, ModelReaderWriterOptions options) |
| 72 | { |
| 73 | if (element.ValueKind == JsonValueKind.Null) |
| 74 | { |
| 75 | return null; |
| 76 | } |
| 77 | GraderType kind = default; |
| 78 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 79 | string name = default; |
| 80 | BinaryData graders = default; |
| 81 | string calculateOutput = default; |
| 82 | foreach (var prop in element.EnumerateObject()) |
| 83 | { |
| 84 | if (prop.NameEquals("type"u8)) |
| 85 | { |
| 86 | kind = new GraderType(prop.Value.GetString()); |
| 87 | continue; |
| 88 | } |
| 89 | if (prop.NameEquals("name"u8)) |
| 90 | { |
| 91 | name = prop.Value.GetString(); |
| 92 | continue; |
| 93 | } |
| 94 | if (prop.NameEquals("graders"u8)) |
| 95 | { |
| 96 | graders = BinaryData.FromString(prop.Value.GetRawText()); |
| 97 | continue; |
| 98 | } |
| 99 | if (prop.NameEquals("calculate_output"u8)) |
| 100 | { |
| 101 | calculateOutput = prop.Value.GetString(); |
| 102 | continue; |
| 103 | } |
| 104 | // Plugin customization: remove options.Format != "W" check |
| 105 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 106 | } |
| 107 | return new GraderMulti(kind, additionalBinaryDataProperties, name, graders, calculateOutput); |
| 108 | } |
| 109 | |
| 110 | BinaryData IPersistableModel<GraderMulti>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 111 | |
| 112 | protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 113 | { |
| 114 | string format = options.Format == "W" ? ((IPersistableModel<GraderMulti>)this).GetFormatFromOptions(options) : options.Format; |
| 115 | switch (format) |
| 116 | { |
| 117 | case "J": |
| 118 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 119 | default: |
| 120 | throw new FormatException($"The model {nameof(GraderMulti)} does not support writing '{options.Format}' format."); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | GraderMulti IPersistableModel<GraderMulti>.Create(BinaryData data, ModelReaderWriterOptions options) => (GraderMulti)PersistableModelCreateCore(data, options); |
| 125 | |
| 126 | protected override Grader PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 127 | { |
| 128 | string format = options.Format == "W" ? ((IPersistableModel<GraderMulti>)this).GetFormatFromOptions(options) : options.Format; |
| 129 | switch (format) |
| 130 | { |
| 131 | case "J": |
| 132 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 133 | { |
| 134 | return DeserializeGraderMulti(document.RootElement, options); |
| 135 | } |
| 136 | default: |
| 137 | throw new FormatException($"The model {nameof(GraderMulti)} does not support reading '{options.Format}' format."); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | string IPersistableModel<GraderMulti>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 142 | } |
| 143 | } |
| 144 | |