//
#nullable disable
using System;
using System.ClientModel.Primitives;
using System.Text;
using System.Text.Json;
using OpenAI;
namespace OpenAI.Responses
{
public partial class ApplyPatchCallOutputItem : ResponseItem, IJsonModel
{
internal ApplyPatchCallOutputItem() : this(ResponseItemKind.ApplyPatchCallOutput, null, default, null, default, null, null)
{
}
protected override ResponseItem PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
{
return DeserializeApplyPatchCallOutputItem(document.RootElement, data, options);
}
default:
throw new FormatException($"The model {nameof(ApplyPatchCallOutputItem)} does not support reading '{options.Format}' format.");
}
}
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
default:
throw new FormatException($"The model {nameof(ApplyPatchCallOutputItem)} does not support writing '{options.Format}' format.");
}
}
BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
ApplyPatchCallOutputItem IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) => (ApplyPatchCallOutputItem)PersistableModelCreateCore(data, options);
string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
void IJsonModel.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)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ApplyPatchCallOutputItem)} 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("$.call_id"u8))
{
writer.WritePropertyName("call_id"u8);
writer.WriteStringValue(CallId);
}
if (!Patch.Contains("$.status"u8))
{
writer.WritePropertyName("status"u8);
writer.WriteStringValue(Status.ToString());
}
if (Optional.IsDefined(Output) && !Patch.Contains("$.output"u8))
{
writer.WritePropertyName("output"u8);
writer.WriteStringValue(Output);
}
if (Optional.IsDefined(CreatedBy) && !Patch.Contains("$.created_by"u8))
{
writer.WritePropertyName("created_by"u8);
writer.WriteStringValue(CreatedBy);
}
Patch.WriteTo(writer);
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
}
ApplyPatchCallOutputItem IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (ApplyPatchCallOutputItem)JsonModelCreateCore(ref reader, options);
protected override ResponseItem JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ApplyPatchCallOutputItem)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeApplyPatchCallOutputItem(document.RootElement, null, options);
}
internal static ApplyPatchCallOutputItem DeserializeApplyPatchCallOutputItem(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
ResponseItemKind kind = default;
string id = 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.Empty : data.ToMemory());
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
string callId = default;
ApplyPatchCallOutputStatus status = default;
string output = default;
string createdBy = default;
foreach (var prop in element.EnumerateObject())
{
if (prop.NameEquals("type"u8))
{
kind = new ResponseItemKind(prop.Value.GetString());
continue;
}
if (prop.NameEquals("id"u8))
{
id = prop.Value.GetString();
continue;
}
if (prop.NameEquals("call_id"u8))
{
callId = prop.Value.GetString();
continue;
}
if (prop.NameEquals("status"u8))
{
status = new ApplyPatchCallOutputStatus(prop.Value.GetString());
continue;
}
if (prop.NameEquals("output"u8))
{
if (prop.Value.ValueKind == JsonValueKind.Null)
{
output = null;
continue;
}
output = prop.Value.GetString();
continue;
}
if (prop.NameEquals("created_by"u8))
{
createdBy = prop.Value.GetString();
continue;
}
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
}
return new ApplyPatchCallOutputItem(
kind,
id,
patch,
callId,
status,
output,
createdBy);
}
}
}