openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI.Responses/src/Generated/Models/ApplyPatchCreateFileOperation.Serialization.cs
138lines · modecode
| 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 | public partial class ApplyPatchCreateFileOperation : ApplyPatchOperation, IJsonModel<ApplyPatchCreateFileOperation> |
| 14 | { |
| 15 | internal ApplyPatchCreateFileOperation() : this(InternalApplyPatchOperationType.CreateFile, default, null, null) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | protected override ApplyPatchOperation PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 20 | { |
| 21 | string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCreateFileOperation>)this).GetFormatFromOptions(options) : options.Format; |
| 22 | switch (format) |
| 23 | { |
| 24 | case "J": |
| 25 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 26 | { |
| 27 | return DeserializeApplyPatchCreateFileOperation(document.RootElement, data, options); |
| 28 | } |
| 29 | default: |
| 30 | throw new FormatException($"The model {nameof(ApplyPatchCreateFileOperation)} does not support reading '{options.Format}' format."); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 35 | { |
| 36 | string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCreateFileOperation>)this).GetFormatFromOptions(options) : options.Format; |
| 37 | switch (format) |
| 38 | { |
| 39 | case "J": |
| 40 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 41 | default: |
| 42 | throw new FormatException($"The model {nameof(ApplyPatchCreateFileOperation)} does not support writing '{options.Format}' format."); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | BinaryData IPersistableModel<ApplyPatchCreateFileOperation>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 47 | |
| 48 | ApplyPatchCreateFileOperation IPersistableModel<ApplyPatchCreateFileOperation>.Create(BinaryData data, ModelReaderWriterOptions options) => (ApplyPatchCreateFileOperation)PersistableModelCreateCore(data, options); |
| 49 | |
| 50 | string IPersistableModel<ApplyPatchCreateFileOperation>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 51 | |
| 52 | void IJsonModel<ApplyPatchCreateFileOperation>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 53 | { |
| 54 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 55 | if (Patch.Contains("$"u8)) |
| 56 | { |
| 57 | writer.WriteRawValue(Patch.GetJson("$"u8)); |
| 58 | return; |
| 59 | } |
| 60 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 61 | |
| 62 | writer.WriteStartObject(); |
| 63 | JsonModelWriteCore(writer, options); |
| 64 | writer.WriteEndObject(); |
| 65 | } |
| 66 | |
| 67 | protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 68 | { |
| 69 | string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCreateFileOperation>)this).GetFormatFromOptions(options) : options.Format; |
| 70 | if (format != "J") |
| 71 | { |
| 72 | throw new FormatException($"The model {nameof(ApplyPatchCreateFileOperation)} does not support writing '{format}' format."); |
| 73 | } |
| 74 | base.JsonModelWriteCore(writer, options); |
| 75 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 76 | if (!Patch.Contains("$.path"u8)) |
| 77 | { |
| 78 | writer.WritePropertyName("path"u8); |
| 79 | writer.WriteStringValue(FilePath); |
| 80 | } |
| 81 | if (!Patch.Contains("$.diff"u8)) |
| 82 | { |
| 83 | writer.WritePropertyName("diff"u8); |
| 84 | writer.WriteStringValue(Diff); |
| 85 | } |
| 86 | |
| 87 | Patch.WriteTo(writer); |
| 88 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 89 | } |
| 90 | |
| 91 | ApplyPatchCreateFileOperation IJsonModel<ApplyPatchCreateFileOperation>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (ApplyPatchCreateFileOperation)JsonModelCreateCore(ref reader, options); |
| 92 | |
| 93 | protected override ApplyPatchOperation JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 94 | { |
| 95 | string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCreateFileOperation>)this).GetFormatFromOptions(options) : options.Format; |
| 96 | if (format != "J") |
| 97 | { |
| 98 | throw new FormatException($"The model {nameof(ApplyPatchCreateFileOperation)} does not support reading '{format}' format."); |
| 99 | } |
| 100 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 101 | return DeserializeApplyPatchCreateFileOperation(document.RootElement, null, options); |
| 102 | } |
| 103 | |
| 104 | internal static ApplyPatchCreateFileOperation DeserializeApplyPatchCreateFileOperation(JsonElement element, BinaryData data, ModelReaderWriterOptions options) |
| 105 | { |
| 106 | if (element.ValueKind == JsonValueKind.Null) |
| 107 | { |
| 108 | return null; |
| 109 | } |
| 110 | InternalApplyPatchOperationType kind = default; |
| 111 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 112 | JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 113 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 114 | string filePath = default; |
| 115 | string diff = default; |
| 116 | foreach (var prop in element.EnumerateObject()) |
| 117 | { |
| 118 | if (prop.NameEquals("type"u8)) |
| 119 | { |
| 120 | kind = new InternalApplyPatchOperationType(prop.Value.GetString()); |
| 121 | continue; |
| 122 | } |
| 123 | if (prop.NameEquals("path"u8)) |
| 124 | { |
| 125 | filePath = prop.Value.GetString(); |
| 126 | continue; |
| 127 | } |
| 128 | if (prop.NameEquals("diff"u8)) |
| 129 | { |
| 130 | diff = prop.Value.GetString(); |
| 131 | continue; |
| 132 | } |
| 133 | patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); |
| 134 | } |
| 135 | return new ApplyPatchCreateFileOperation(kind, patch, filePath, diff); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |