|
| 1 | +using System; |
| 2 | +using System.ClientModel.Primitives; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +namespace OpenAI.Responses; |
| 7 | + |
| 8 | +public partial class ResponsePrompt : IJsonModel<ResponsePrompt> |
| 9 | +{ |
| 10 | + void IJsonModel<ResponsePrompt>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 11 | + => CustomSerializationHelpers.SerializeInstance(this, SerializeResponsePrompt, writer, options); |
| 12 | + |
| 13 | + ResponsePrompt IJsonModel<ResponsePrompt>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 14 | + => CustomSerializationHelpers.DeserializeNewInstance(this, DeserializeResponsePrompt, ref reader, options); |
| 15 | + |
| 16 | + BinaryData IPersistableModel<ResponsePrompt>.Write(ModelReaderWriterOptions options) |
| 17 | + => CustomSerializationHelpers.SerializeInstance(this, options); |
| 18 | + |
| 19 | + ResponsePrompt IPersistableModel<ResponsePrompt>.Create(BinaryData data, ModelReaderWriterOptions options) |
| 20 | + => CustomSerializationHelpers.DeserializeNewInstance(this, DeserializeResponsePrompt, data, options); |
| 21 | + |
| 22 | + string IPersistableModel<ResponsePrompt>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 23 | + |
| 24 | + internal static void SerializeResponsePrompt(ResponsePrompt instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 25 | + { |
| 26 | + writer.WriteStartObject(); |
| 27 | + |
| 28 | + if (instance.Id != null) |
| 29 | + { |
| 30 | + writer.WritePropertyName("id"); |
| 31 | + writer.WriteStringValue(instance.Id); |
| 32 | + } |
| 33 | + |
| 34 | + if (instance.Version != null) |
| 35 | + { |
| 36 | + writer.WritePropertyName("version"); |
| 37 | + writer.WriteStringValue(instance.Version); |
| 38 | + } |
| 39 | + |
| 40 | + if (instance.Variables != null && instance.Variables.Count > 0) |
| 41 | + { |
| 42 | + writer.WritePropertyName("variables"); |
| 43 | + writer.WriteStartObject(); |
| 44 | + foreach (var variable in instance.Variables) |
| 45 | + { |
| 46 | + writer.WritePropertyName(variable.Key); |
| 47 | + JsonSerializer.Serialize(writer, variable.Value, options.Format == "W" ? null : (JsonSerializerOptions)null); |
| 48 | + } |
| 49 | + writer.WriteEndObject(); |
| 50 | + } |
| 51 | + |
| 52 | + writer.WriteEndObject(); |
| 53 | + } |
| 54 | + |
| 55 | + internal static ResponsePrompt DeserializeResponsePrompt(JsonElement element, ModelReaderWriterOptions options = null) |
| 56 | + { |
| 57 | + if (element.ValueKind == JsonValueKind.Null) |
| 58 | + { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + string id = null; |
| 63 | + string version = null; |
| 64 | + Dictionary<string, object> variables = new Dictionary<string, object>(); |
| 65 | + |
| 66 | + foreach (var property in element.EnumerateObject()) |
| 67 | + { |
| 68 | + if (property.NameEquals("id")) |
| 69 | + { |
| 70 | + id = property.Value.GetString(); |
| 71 | + } |
| 72 | + else if (property.NameEquals("version")) |
| 73 | + { |
| 74 | + version = property.Value.GetString(); |
| 75 | + } |
| 76 | + else if (property.NameEquals("variables")) |
| 77 | + { |
| 78 | + foreach (var variable in property.Value.EnumerateObject()) |
| 79 | + { |
| 80 | + variables[variable.Name] = JsonSerializer.Deserialize<object>(variable.Value.GetRawText()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + var result = new ResponsePrompt(); |
| 86 | + result.Id = id; |
| 87 | + result.Version = version; |
| 88 | + |
| 89 | + if (variables.Count > 0) |
| 90 | + { |
| 91 | + foreach (var variable in variables) |
| 92 | + { |
| 93 | + result.Variables[variable.Key] = variable.Value; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return result; |
| 98 | + } |
| 99 | +} |
0 commit comments