openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Messages/ChatMessage.Serialization.cs
50lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Runtime.CompilerServices; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | namespace OpenAI.Chat; |
| 8 | |
| 9 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Chat.ChatMessage>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 10 | [CodeGenSerialization(nameof(Content), SerializationValueHook = nameof(SerializeContentValue), DeserializationValueHook = nameof(DeserializeContentValue))] |
| 11 | public partial class ChatMessage |
| 12 | { |
| 13 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 14 | internal void SerializeContentValue(Utf8JsonWriter writer, ModelReaderWriterOptions options = null) |
| 15 | { |
| 16 | throw new NotImplementedException(); |
| 17 | } |
| 18 | |
| 19 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 20 | internal static void DeserializeContentValue(JsonProperty property, ref ChatMessageContent content, ModelReaderWriterOptions options = null) |
| 21 | { |
| 22 | content = ChatMessageContent.DeserializeChatMessageContent(property.Value, options); |
| 23 | } |
| 24 | |
| 25 | void IJsonModel<ChatMessage>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 26 | => CustomSerializationHelpers.SerializeInstance(this, WriteCore, writer, options); |
| 27 | |
| 28 | internal static void WriteCore(ChatMessage instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 29 | => instance.WriteCore(writer, options); |
| 30 | |
| 31 | internal virtual void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 32 | => throw new InvalidOperationException($"The {nameof(WriteCore)} method should be invoked on an overriding type derived from {nameof(ChatMessage)}."); |
| 33 | |
| 34 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 35 | internal void WriteRoleProperty(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 36 | { |
| 37 | writer.WritePropertyName("role"u8); |
| 38 | writer.WriteStringValue(Role.ToSerialString()); |
| 39 | } |
| 40 | |
| 41 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 42 | internal void WriteContentProperty(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 43 | { |
| 44 | if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined()) |
| 45 | { |
| 46 | writer.WritePropertyName("content"u8); |
| 47 | Content.WriteTo(writer, options); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |