openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Generated/Models/Conversations/InternalConversationItemList.Serialization.cs
195lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.ClientModel.Primitives; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | |
| 12 | namespace OpenAI.Conversations |
| 13 | { |
| 14 | internal partial class InternalConversationItemList : IJsonModel<InternalConversationItemList> |
| 15 | { |
| 16 | internal InternalConversationItemList() : this(null, null, default, null, null, null) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | protected virtual InternalConversationItemList PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 21 | { |
| 22 | string format = options.Format == "W" ? ((IPersistableModel<InternalConversationItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 23 | switch (format) |
| 24 | { |
| 25 | case "J": |
| 26 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 27 | { |
| 28 | return DeserializeInternalConversationItemList(document.RootElement, options); |
| 29 | } |
| 30 | default: |
| 31 | throw new FormatException($"The model {nameof(InternalConversationItemList)} does not support reading '{options.Format}' format."); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 36 | { |
| 37 | string format = options.Format == "W" ? ((IPersistableModel<InternalConversationItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 38 | switch (format) |
| 39 | { |
| 40 | case "J": |
| 41 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 42 | default: |
| 43 | throw new FormatException($"The model {nameof(InternalConversationItemList)} does not support writing '{options.Format}' format."); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | BinaryData IPersistableModel<InternalConversationItemList>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 48 | |
| 49 | InternalConversationItemList IPersistableModel<InternalConversationItemList>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 50 | |
| 51 | string IPersistableModel<InternalConversationItemList>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 52 | |
| 53 | public static explicit operator InternalConversationItemList(ClientResult result) |
| 54 | { |
| 55 | PipelineResponse response = result.GetRawResponse(); |
| 56 | using JsonDocument document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); |
| 57 | return DeserializeInternalConversationItemList(document.RootElement, ModelSerializationExtensions.WireOptions); |
| 58 | } |
| 59 | |
| 60 | void IJsonModel<InternalConversationItemList>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 61 | { |
| 62 | writer.WriteStartObject(); |
| 63 | JsonModelWriteCore(writer, options); |
| 64 | writer.WriteEndObject(); |
| 65 | } |
| 66 | |
| 67 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 68 | { |
| 69 | string format = options.Format == "W" ? ((IPersistableModel<InternalConversationItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 70 | if (format != "J") |
| 71 | { |
| 72 | throw new FormatException($"The model {nameof(InternalConversationItemList)} does not support writing '{format}' format."); |
| 73 | } |
| 74 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 75 | { |
| 76 | writer.WritePropertyName("object"u8); |
| 77 | writer.WriteStringValue(Object); |
| 78 | } |
| 79 | if (_additionalBinaryDataProperties?.ContainsKey("data") != true) |
| 80 | { |
| 81 | writer.WritePropertyName("data"u8); |
| 82 | writer.WriteStartArray(); |
| 83 | foreach (InternalConversationItemResource item in Data) |
| 84 | { |
| 85 | writer.WriteObjectValue(item, options); |
| 86 | } |
| 87 | writer.WriteEndArray(); |
| 88 | } |
| 89 | if (_additionalBinaryDataProperties?.ContainsKey("has_more") != true) |
| 90 | { |
| 91 | writer.WritePropertyName("has_more"u8); |
| 92 | writer.WriteBooleanValue(HasMore); |
| 93 | } |
| 94 | if (_additionalBinaryDataProperties?.ContainsKey("first_id") != true) |
| 95 | { |
| 96 | writer.WritePropertyName("first_id"u8); |
| 97 | writer.WriteStringValue(FirstId); |
| 98 | } |
| 99 | if (_additionalBinaryDataProperties?.ContainsKey("last_id") != true) |
| 100 | { |
| 101 | writer.WritePropertyName("last_id"u8); |
| 102 | writer.WriteStringValue(LastId); |
| 103 | } |
| 104 | // Plugin customization: remove options.Format != "W" check |
| 105 | if (_additionalBinaryDataProperties != null) |
| 106 | { |
| 107 | foreach (var item in _additionalBinaryDataProperties) |
| 108 | { |
| 109 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 110 | { |
| 111 | continue; |
| 112 | } |
| 113 | writer.WritePropertyName(item.Key); |
| 114 | #if NET6_0_OR_GREATER |
| 115 | writer.WriteRawValue(item.Value); |
| 116 | #else |
| 117 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 118 | { |
| 119 | JsonSerializer.Serialize(writer, document.RootElement); |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | InternalConversationItemList IJsonModel<InternalConversationItemList>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 127 | |
| 128 | protected virtual InternalConversationItemList JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 129 | { |
| 130 | string format = options.Format == "W" ? ((IPersistableModel<InternalConversationItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 131 | if (format != "J") |
| 132 | { |
| 133 | throw new FormatException($"The model {nameof(InternalConversationItemList)} does not support reading '{format}' format."); |
| 134 | } |
| 135 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 136 | return DeserializeInternalConversationItemList(document.RootElement, options); |
| 137 | } |
| 138 | |
| 139 | internal static InternalConversationItemList DeserializeInternalConversationItemList(JsonElement element, ModelReaderWriterOptions options) |
| 140 | { |
| 141 | if (element.ValueKind == JsonValueKind.Null) |
| 142 | { |
| 143 | return null; |
| 144 | } |
| 145 | string @object = default; |
| 146 | IList<InternalConversationItemResource> data = default; |
| 147 | bool hasMore = default; |
| 148 | string firstId = default; |
| 149 | string lastId = default; |
| 150 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 151 | foreach (var prop in element.EnumerateObject()) |
| 152 | { |
| 153 | if (prop.NameEquals("object"u8)) |
| 154 | { |
| 155 | @object = prop.Value.GetString(); |
| 156 | continue; |
| 157 | } |
| 158 | if (prop.NameEquals("data"u8)) |
| 159 | { |
| 160 | List<InternalConversationItemResource> array = new List<InternalConversationItemResource>(); |
| 161 | foreach (var item in prop.Value.EnumerateArray()) |
| 162 | { |
| 163 | array.Add(InternalConversationItemResource.DeserializeInternalConversationItemResource(item, options)); |
| 164 | } |
| 165 | data = array; |
| 166 | continue; |
| 167 | } |
| 168 | if (prop.NameEquals("has_more"u8)) |
| 169 | { |
| 170 | hasMore = prop.Value.GetBoolean(); |
| 171 | continue; |
| 172 | } |
| 173 | if (prop.NameEquals("first_id"u8)) |
| 174 | { |
| 175 | firstId = prop.Value.GetString(); |
| 176 | continue; |
| 177 | } |
| 178 | if (prop.NameEquals("last_id"u8)) |
| 179 | { |
| 180 | lastId = prop.Value.GetString(); |
| 181 | continue; |
| 182 | } |
| 183 | // Plugin customization: remove options.Format != "W" check |
| 184 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 185 | } |
| 186 | return new InternalConversationItemList( |
| 187 | @object, |
| 188 | data, |
| 189 | hasMore, |
| 190 | firstId, |
| 191 | lastId, |
| 192 | additionalBinaryDataProperties); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |