openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6b3c8b0a3d3208218dc3cbcd295502024988a956

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Generated/Models/Files/InternalAddUploadPartRequest.Serialization.cs

127lines · 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.Files
12{
13 internal partial class InternalAddUploadPartRequest : IJsonModel<InternalAddUploadPartRequest>
14 {
15 internal InternalAddUploadPartRequest()
16 {
17 }
18
19 void IJsonModel<InternalAddUploadPartRequest>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
20 {
21 writer.WriteStartObject();
22 JsonModelWriteCore(writer, options);
23 writer.WriteEndObject();
24 }
25
26 protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
27 {
28 string format = options.Format == "W" ? ((IPersistableModel<InternalAddUploadPartRequest>)this).GetFormatFromOptions(options) : options.Format;
29 if (format != "J")
30 {
31 throw new FormatException($"The model {nameof(InternalAddUploadPartRequest)} does not support writing '{format}' format.");
32 }
33 if (_additionalBinaryDataProperties?.ContainsKey("data") != true)
34 {
35 writer.WritePropertyName("data"u8);
36 writer.WriteBase64StringValue(Data.ToArray(), "D");
37 }
38 // Plugin customization: remove options.Format != "W" check
39 if (_additionalBinaryDataProperties != null)
40 {
41 foreach (var item in _additionalBinaryDataProperties)
42 {
43 if (ModelSerializationExtensions.IsSentinelValue(item.Value))
44 {
45 continue;
46 }
47 writer.WritePropertyName(item.Key);
48#if NET6_0_OR_GREATER
49 writer.WriteRawValue(item.Value);
50#else
51 using (JsonDocument document = JsonDocument.Parse(item.Value))
52 {
53 JsonSerializer.Serialize(writer, document.RootElement);
54 }
55#endif
56 }
57 }
58 }
59
60 InternalAddUploadPartRequest IJsonModel<InternalAddUploadPartRequest>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);
61
62 protected virtual InternalAddUploadPartRequest JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
63 {
64 string format = options.Format == "W" ? ((IPersistableModel<InternalAddUploadPartRequest>)this).GetFormatFromOptions(options) : options.Format;
65 if (format != "J")
66 {
67 throw new FormatException($"The model {nameof(InternalAddUploadPartRequest)} does not support reading '{format}' format.");
68 }
69 using JsonDocument document = JsonDocument.ParseValue(ref reader);
70 return DeserializeInternalAddUploadPartRequest(document.RootElement, options);
71 }
72
73 internal static InternalAddUploadPartRequest DeserializeInternalAddUploadPartRequest(JsonElement element, ModelReaderWriterOptions options)
74 {
75 if (element.ValueKind == JsonValueKind.Null)
76 {
77 return null;
78 }
79 BinaryData data = default;
80 IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
81 foreach (var prop in element.EnumerateObject())
82 {
83 if (prop.NameEquals("data"u8))
84 {
85 data = BinaryData.FromBytes(prop.Value.GetBytesFromBase64("D"));
86 continue;
87 }
88 // Plugin customization: remove options.Format != "W" check
89 additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
90 }
91 return new InternalAddUploadPartRequest(data, additionalBinaryDataProperties);
92 }
93
94 BinaryData IPersistableModel<InternalAddUploadPartRequest>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
95
96 protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
97 {
98 string format = options.Format == "W" ? ((IPersistableModel<InternalAddUploadPartRequest>)this).GetFormatFromOptions(options) : options.Format;
99 switch (format)
100 {
101 case "J":
102 return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
103 default:
104 throw new FormatException($"The model {nameof(InternalAddUploadPartRequest)} does not support writing '{options.Format}' format.");
105 }
106 }
107
108 InternalAddUploadPartRequest IPersistableModel<InternalAddUploadPartRequest>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);
109
110 protected virtual InternalAddUploadPartRequest PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
111 {
112 string format = options.Format == "W" ? ((IPersistableModel<InternalAddUploadPartRequest>)this).GetFormatFromOptions(options) : options.Format;
113 switch (format)
114 {
115 case "J":
116 using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
117 {
118 return DeserializeInternalAddUploadPartRequest(document.RootElement, options);
119 }
120 default:
121 throw new FormatException($"The model {nameof(InternalAddUploadPartRequest)} does not support reading '{options.Format}' format.");
122 }
123 }
124
125 string IPersistableModel<InternalAddUploadPartRequest>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
126 }
127}