|
| 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 | + if (variable.Value is JsonElement element) |
| 48 | + { |
| 49 | +#if NET6_0_OR_GREATER |
| 50 | + writer.WriteRawValue(element.GetRawText()); |
| 51 | +#else |
| 52 | + using JsonDocument document = JsonDocument.Parse(element.GetRawText()); |
| 53 | + JsonSerializer.Serialize(writer, document.RootElement); |
| 54 | +#endif |
| 55 | + } |
| 56 | + else if (variable.Value != null) |
| 57 | + { |
| 58 | + // Handle primitive types directly |
| 59 | + switch (variable.Value) |
| 60 | + { |
| 61 | + case string str: |
| 62 | + writer.WriteStringValue(str); |
| 63 | + break; |
| 64 | + case int intVal: |
| 65 | + writer.WriteNumberValue(intVal); |
| 66 | + break; |
| 67 | + case long longVal: |
| 68 | + writer.WriteNumberValue(longVal); |
| 69 | + break; |
| 70 | + case float floatVal: |
| 71 | + writer.WriteNumberValue(floatVal); |
| 72 | + break; |
| 73 | + case double doubleVal: |
| 74 | + writer.WriteNumberValue(doubleVal); |
| 75 | + break; |
| 76 | + case decimal decimalVal: |
| 77 | + writer.WriteNumberValue(decimalVal); |
| 78 | + break; |
| 79 | + case bool boolVal: |
| 80 | + writer.WriteBooleanValue(boolVal); |
| 81 | + break; |
| 82 | + default: |
| 83 | + // For other types, write as string value |
| 84 | + writer.WriteStringValue(variable.Value.ToString()); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + writer.WriteNullValue(); |
| 91 | + } |
| 92 | + } |
| 93 | + writer.WriteEndObject(); |
| 94 | + } |
| 95 | + |
| 96 | + writer.WriteEndObject(); |
| 97 | + } |
| 98 | + |
| 99 | + internal static ResponsePrompt DeserializeResponsePrompt(JsonElement element, ModelReaderWriterOptions options = null) |
| 100 | + { |
| 101 | + if (element.ValueKind == JsonValueKind.Null) |
| 102 | + { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + string id = null; |
| 107 | + string version = null; |
| 108 | + Dictionary<string, object> variables = new Dictionary<string, object>(); |
| 109 | + |
| 110 | + foreach (var property in element.EnumerateObject()) |
| 111 | + { |
| 112 | + if (property.NameEquals("id")) |
| 113 | + { |
| 114 | + id = property.Value.GetString(); |
| 115 | + } |
| 116 | + else if (property.NameEquals("version")) |
| 117 | + { |
| 118 | + version = property.Value.GetString(); |
| 119 | + } |
| 120 | + else if (property.NameEquals("variables")) |
| 121 | + { |
| 122 | + foreach (var variable in property.Value.EnumerateObject()) |
| 123 | + { |
| 124 | + // Handle different JSON value types appropriately |
| 125 | + object value = variable.Value.ValueKind switch |
| 126 | + { |
| 127 | + JsonValueKind.String => variable.Value.GetString(), |
| 128 | + JsonValueKind.Number => variable.Value.TryGetInt32(out int intVal) ? intVal : variable.Value.GetDouble(), |
| 129 | + JsonValueKind.True => true, |
| 130 | + JsonValueKind.False => false, |
| 131 | + JsonValueKind.Null => null, |
| 132 | + _ => variable.Value.GetRawText() // For objects/arrays, store as raw JSON string |
| 133 | + }; |
| 134 | + variables[variable.Name] = value; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + var result = new ResponsePrompt(); |
| 140 | + result.Id = id; |
| 141 | + result.Version = version; |
| 142 | + |
| 143 | + // Add variables to the existing dictionary (Variables property is get-only) |
| 144 | + if (variables.Count > 0) |
| 145 | + { |
| 146 | + foreach (var variable in variables) |
| 147 | + { |
| 148 | + result.Variables[variable.Key] = variable.Value; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return result; |
| 153 | + } |
| 154 | +} |
0 commit comments