|
| 1 | +// <auto-generated/> |
| 2 | + |
| 3 | +#nullable disable |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.ClientModel.Primitives; |
| 7 | +using System.Text; |
| 8 | +using System.Text.Json; |
| 9 | +using OpenAI; |
| 10 | + |
| 11 | +namespace OpenAI.Responses |
| 12 | +{ |
| 13 | + internal partial class InternalConversation : IJsonModel<InternalConversation> |
| 14 | + { |
| 15 | + internal InternalConversation() |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + void IJsonModel<InternalConversation>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 20 | + { |
| 21 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 22 | + if (Patch.Contains("$"u8)) |
| 23 | + { |
| 24 | + writer.WriteRawValue(Patch.GetJson("$"u8)); |
| 25 | + return; |
| 26 | + } |
| 27 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 28 | + |
| 29 | + writer.WriteStartObject(); |
| 30 | + JsonModelWriteCore(writer, options); |
| 31 | + writer.WriteEndObject(); |
| 32 | + } |
| 33 | + |
| 34 | + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 35 | + { |
| 36 | + string format = options.Format == "W" ? ((IPersistableModel<InternalConversation>)this).GetFormatFromOptions(options) : options.Format; |
| 37 | + if (format != "J") |
| 38 | + { |
| 39 | + throw new FormatException($"The model {nameof(InternalConversation)} does not support writing '{format}' format."); |
| 40 | + } |
| 41 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 42 | + if (!Patch.Contains("$.id"u8)) |
| 43 | + { |
| 44 | + writer.WritePropertyName("id"u8); |
| 45 | + writer.WriteStringValue(Id); |
| 46 | + } |
| 47 | + |
| 48 | + Patch.WriteTo(writer); |
| 49 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 50 | + } |
| 51 | + |
| 52 | + InternalConversation IJsonModel<InternalConversation>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 53 | + |
| 54 | + protected virtual InternalConversation JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 55 | + { |
| 56 | + string format = options.Format == "W" ? ((IPersistableModel<InternalConversation>)this).GetFormatFromOptions(options) : options.Format; |
| 57 | + if (format != "J") |
| 58 | + { |
| 59 | + throw new FormatException($"The model {nameof(InternalConversation)} does not support reading '{format}' format."); |
| 60 | + } |
| 61 | + using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 62 | + return DeserializeInternalConversation(document.RootElement, null, options); |
| 63 | + } |
| 64 | + |
| 65 | + internal static InternalConversation DeserializeInternalConversation(JsonElement element, BinaryData data, ModelReaderWriterOptions options) |
| 66 | + { |
| 67 | + if (element.ValueKind == JsonValueKind.Null) |
| 68 | + { |
| 69 | + return null; |
| 70 | + } |
| 71 | + string id = default; |
| 72 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 73 | + JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 74 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 75 | + foreach (var prop in element.EnumerateObject()) |
| 76 | + { |
| 77 | + if (prop.NameEquals("id"u8)) |
| 78 | + { |
| 79 | + id = prop.Value.GetString(); |
| 80 | + continue; |
| 81 | + } |
| 82 | + patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); |
| 83 | + } |
| 84 | + return new InternalConversation(id, patch); |
| 85 | + } |
| 86 | + |
| 87 | + BinaryData IPersistableModel<InternalConversation>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 88 | + |
| 89 | + protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 90 | + { |
| 91 | + string format = options.Format == "W" ? ((IPersistableModel<InternalConversation>)this).GetFormatFromOptions(options) : options.Format; |
| 92 | + switch (format) |
| 93 | + { |
| 94 | + case "J": |
| 95 | + return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 96 | + default: |
| 97 | + throw new FormatException($"The model {nameof(InternalConversation)} does not support writing '{options.Format}' format."); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + InternalConversation IPersistableModel<InternalConversation>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 102 | + |
| 103 | + protected virtual InternalConversation PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 104 | + { |
| 105 | + string format = options.Format == "W" ? ((IPersistableModel<InternalConversation>)this).GetFormatFromOptions(options) : options.Format; |
| 106 | + switch (format) |
| 107 | + { |
| 108 | + case "J": |
| 109 | + using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 110 | + { |
| 111 | + return DeserializeInternalConversation(document.RootElement, data, options); |
| 112 | + } |
| 113 | + default: |
| 114 | + throw new FormatException($"The model {nameof(InternalConversation)} does not support reading '{options.Format}' format."); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + string IPersistableModel<InternalConversation>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 119 | + } |
| 120 | +} |
0 commit comments