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