openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Generated/Models/Batch/InternalBatchRequestInput.Serialization.cs

149lines · modecode

1// <auto-generated/>
2
3#nullable disable
4
5using System;
6using System.ClientModel.Primitives;
7using System.Collections.Generic;
8using System.Text.Json;
9using OpenAI;
10
11namespace OpenAI.Batch
12{
13 internal partial class InternalBatchRequestInput : IJsonModel<InternalBatchRequestInput>
14 {
15 void IJsonModel<InternalBatchRequestInput>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
16 {
17 writer.WriteStartObject();
18 JsonModelWriteCore(writer, options);
19 writer.WriteEndObject();
20 }
21
22 protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
23 {
24 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchRequestInput>)this).GetFormatFromOptions(options) : options.Format;
25 if (format != "J")
26 {
27 throw new FormatException($"The model {nameof(InternalBatchRequestInput)} does not support writing '{format}' format.");
28 }
29 if (Optional.IsDefined(CustomId) && _additionalBinaryDataProperties?.ContainsKey("custom_id") != true)
30 {
31 writer.WritePropertyName("custom_id"u8);
32 writer.WriteStringValue(CustomId);
33 }
34 if (Optional.IsDefined(Method) && _additionalBinaryDataProperties?.ContainsKey("method") != true)
35 {
36 writer.WritePropertyName("method"u8);
37 writer.WriteStringValue(Method);
38 }
39 if (Optional.IsDefined(Url) && _additionalBinaryDataProperties?.ContainsKey("url") != true)
40 {
41 writer.WritePropertyName("url"u8);
42 writer.WriteStringValue(Url.AbsoluteUri);
43 }
44 // Plugin customization: remove options.Format != "W" check
45 if (_additionalBinaryDataProperties != null)
46 {
47 foreach (var item in _additionalBinaryDataProperties)
48 {
49 if (ModelSerializationExtensions.IsSentinelValue(item.Value))
50 {
51 continue;
52 }
53 writer.WritePropertyName(item.Key);
54#if NET6_0_OR_GREATER
55 writer.WriteRawValue(item.Value);
56#else
57 using (JsonDocument document = JsonDocument.Parse(item.Value))
58 {
59 JsonSerializer.Serialize(writer, document.RootElement);
60 }
61#endif
62 }
63 }
64 }
65
66 InternalBatchRequestInput IJsonModel<InternalBatchRequestInput>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);
67
68 protected virtual InternalBatchRequestInput JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
69 {
70 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchRequestInput>)this).GetFormatFromOptions(options) : options.Format;
71 if (format != "J")
72 {
73 throw new FormatException($"The model {nameof(InternalBatchRequestInput)} does not support reading '{format}' format.");
74 }
75 using JsonDocument document = JsonDocument.ParseValue(ref reader);
76 return DeserializeInternalBatchRequestInput(document.RootElement, options);
77 }
78
79 internal static InternalBatchRequestInput DeserializeInternalBatchRequestInput(JsonElement element, ModelReaderWriterOptions options)
80 {
81 if (element.ValueKind == JsonValueKind.Null)
82 {
83 return null;
84 }
85 string customId = default;
86 string @method = default;
87 Uri url = default;
88 IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
89 foreach (var prop in element.EnumerateObject())
90 {
91 if (prop.NameEquals("custom_id"u8))
92 {
93 customId = prop.Value.GetString();
94 continue;
95 }
96 if (prop.NameEquals("method"u8))
97 {
98 @method = prop.Value.GetString();
99 continue;
100 }
101 if (prop.NameEquals("url"u8))
102 {
103 if (prop.Value.ValueKind == JsonValueKind.Null)
104 {
105 continue;
106 }
107 url = new Uri(prop.Value.GetString());
108 continue;
109 }
110 // Plugin customization: remove options.Format != "W" check
111 additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
112 }
113 return new InternalBatchRequestInput(customId, @method, url, additionalBinaryDataProperties);
114 }
115
116 BinaryData IPersistableModel<InternalBatchRequestInput>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
117
118 protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
119 {
120 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchRequestInput>)this).GetFormatFromOptions(options) : options.Format;
121 switch (format)
122 {
123 case "J":
124 return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
125 default:
126 throw new FormatException($"The model {nameof(InternalBatchRequestInput)} does not support writing '{options.Format}' format.");
127 }
128 }
129
130 InternalBatchRequestInput IPersistableModel<InternalBatchRequestInput>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);
131
132 protected virtual InternalBatchRequestInput PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
133 {
134 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchRequestInput>)this).GetFormatFromOptions(options) : options.Format;
135 switch (format)
136 {
137 case "J":
138 using (JsonDocument document = JsonDocument.Parse(data))
139 {
140 return DeserializeInternalBatchRequestInput(document.RootElement, options);
141 }
142 default:
143 throw new FormatException($"The model {nameof(InternalBatchRequestInput)} does not support reading '{options.Format}' format.");
144 }
145 }
146
147 string IPersistableModel<InternalBatchRequestInput>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
148 }
149}
150