openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
83db8ef208eccfa6eeff4aaa710be7330936f083

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

OpenAI.Responses/src/Generated/Models/ApplyPatchDeleteFileOperation.Serialization.cs

127lines · modepreview

// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Text;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Responses
{
    public partial class ApplyPatchDeleteFileOperation : ApplyPatchOperation, IJsonModel<ApplyPatchDeleteFileOperation>
    {
        internal ApplyPatchDeleteFileOperation() : this(InternalApplyPatchOperationType.DeleteFile, default, null)
        {
        }

        protected override ApplyPatchOperation PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchDeleteFileOperation>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
                    {
                        return DeserializeApplyPatchDeleteFileOperation(document.RootElement, data, options);
                    }
                default:
                    throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support reading '{options.Format}' format.");
            }
        }

        protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchDeleteFileOperation>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
                default:
                    throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support writing '{options.Format}' format.");
            }
        }

        BinaryData IPersistableModel<ApplyPatchDeleteFileOperation>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

        ApplyPatchDeleteFileOperation IPersistableModel<ApplyPatchDeleteFileOperation>.Create(BinaryData data, ModelReaderWriterOptions options) => (ApplyPatchDeleteFileOperation)PersistableModelCreateCore(data, options);

        string IPersistableModel<ApplyPatchDeleteFileOperation>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";

        void IJsonModel<ApplyPatchDeleteFileOperation>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
            if (Patch.Contains("$"u8))
            {
                writer.WriteRawValue(Patch.GetJson("$"u8));
                return;
            }
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.

            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchDeleteFileOperation>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
            if (!Patch.Contains("$.path"u8))
            {
                writer.WritePropertyName("path"u8);
                writer.WriteStringValue(FilePath);
            }

            Patch.WriteTo(writer);
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
        }

        ApplyPatchDeleteFileOperation IJsonModel<ApplyPatchDeleteFileOperation>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (ApplyPatchDeleteFileOperation)JsonModelCreateCore(ref reader, options);

        protected override ApplyPatchOperation JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchDeleteFileOperation>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support reading '{format}' format.");
            }
            using JsonDocument document = JsonDocument.ParseValue(ref reader);
            return DeserializeApplyPatchDeleteFileOperation(document.RootElement, null, options);
        }

        internal static ApplyPatchDeleteFileOperation DeserializeApplyPatchDeleteFileOperation(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalApplyPatchOperationType kind = default;
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
            JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory());
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
            string filePath = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalApplyPatchOperationType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("path"u8))
                {
                    filePath = prop.Value.GetString();
                    continue;
                }
                patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
            }
            return new ApplyPatchDeleteFileOperation(kind, patch, filePath);
        }
    }
}