openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1011-another-one

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

166lines · 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 InternalBatchError : IJsonModel<InternalBatchError>
14 {
15 protected virtual InternalBatchError PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
16 {
17 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchError>)this).GetFormatFromOptions(options) : options.Format;
18 switch (format)
19 {
20 case "J":
21 using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
22 {
23 return DeserializeInternalBatchError(document.RootElement, options);
24 }
25 default:
26 throw new FormatException($"The model {nameof(InternalBatchError)} does not support reading '{options.Format}' format.");
27 }
28 }
29
30 protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
31 {
32 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchError>)this).GetFormatFromOptions(options) : options.Format;
33 switch (format)
34 {
35 case "J":
36 return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
37 default:
38 throw new FormatException($"The model {nameof(InternalBatchError)} does not support writing '{options.Format}' format.");
39 }
40 }
41
42 BinaryData IPersistableModel<InternalBatchError>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
43
44 InternalBatchError IPersistableModel<InternalBatchError>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);
45
46 string IPersistableModel<InternalBatchError>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
47
48 void IJsonModel<InternalBatchError>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
49 {
50 writer.WriteStartObject();
51 JsonModelWriteCore(writer, options);
52 writer.WriteEndObject();
53 }
54
55 protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
56 {
57 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchError>)this).GetFormatFromOptions(options) : options.Format;
58 if (format != "J")
59 {
60 throw new FormatException($"The model {nameof(InternalBatchError)} does not support writing '{format}' format.");
61 }
62 if (Optional.IsDefined(Code) && _additionalBinaryDataProperties?.ContainsKey("code") != true)
63 {
64 writer.WritePropertyName("code"u8);
65 writer.WriteStringValue(Code);
66 }
67 if (Optional.IsDefined(Message) && _additionalBinaryDataProperties?.ContainsKey("message") != true)
68 {
69 writer.WritePropertyName("message"u8);
70 writer.WriteStringValue(Message);
71 }
72 if (Optional.IsDefined(Param) && _additionalBinaryDataProperties?.ContainsKey("param") != true)
73 {
74 writer.WritePropertyName("param"u8);
75 writer.WriteStringValue(Param);
76 }
77 if (Optional.IsDefined(Line) && _additionalBinaryDataProperties?.ContainsKey("line") != true)
78 {
79 writer.WritePropertyName("line"u8);
80 writer.WriteNumberValue(Line.Value);
81 }
82 // Plugin customization: remove options.Format != "W" check
83 if (_additionalBinaryDataProperties != null)
84 {
85 foreach (var item in _additionalBinaryDataProperties)
86 {
87 if (ModelSerializationExtensions.IsSentinelValue(item.Value))
88 {
89 continue;
90 }
91 writer.WritePropertyName(item.Key);
92#if NET6_0_OR_GREATER
93 writer.WriteRawValue(item.Value);
94#else
95 using (JsonDocument document = JsonDocument.Parse(item.Value))
96 {
97 JsonSerializer.Serialize(writer, document.RootElement);
98 }
99#endif
100 }
101 }
102 }
103
104 InternalBatchError IJsonModel<InternalBatchError>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);
105
106 protected virtual InternalBatchError JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
107 {
108 string format = options.Format == "W" ? ((IPersistableModel<InternalBatchError>)this).GetFormatFromOptions(options) : options.Format;
109 if (format != "J")
110 {
111 throw new FormatException($"The model {nameof(InternalBatchError)} does not support reading '{format}' format.");
112 }
113 using JsonDocument document = JsonDocument.ParseValue(ref reader);
114 return DeserializeInternalBatchError(document.RootElement, options);
115 }
116
117 internal static InternalBatchError DeserializeInternalBatchError(JsonElement element, ModelReaderWriterOptions options)
118 {
119 if (element.ValueKind == JsonValueKind.Null)
120 {
121 return null;
122 }
123 string code = default;
124 string message = default;
125 string @param = default;
126 int? line = default;
127 IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
128 foreach (var prop in element.EnumerateObject())
129 {
130 if (prop.NameEquals("code"u8))
131 {
132 code = prop.Value.GetString();
133 continue;
134 }
135 if (prop.NameEquals("message"u8))
136 {
137 message = prop.Value.GetString();
138 continue;
139 }
140 if (prop.NameEquals("param"u8))
141 {
142 if (prop.Value.ValueKind == JsonValueKind.Null)
143 {
144 @param = null;
145 continue;
146 }
147 @param = prop.Value.GetString();
148 continue;
149 }
150 if (prop.NameEquals("line"u8))
151 {
152 if (prop.Value.ValueKind == JsonValueKind.Null)
153 {
154 line = null;
155 continue;
156 }
157 line = prop.Value.GetInt32();
158 continue;
159 }
160 // Plugin customization: remove options.Format != "W" check
161 additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
162 }
163 return new InternalBatchError(code, message, @param, line, additionalBinaryDataProperties);
164 }
165 }
166}
167