openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/AssistantChatMessage.Serialization.cs
64lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | namespace OpenAI.Chat; |
| 7 | |
| 8 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Chat.AssistantChatMessage>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 9 | public partial class AssistantChatMessage : IJsonModel<AssistantChatMessage> |
| 10 | { |
| 11 | void IJsonModel<AssistantChatMessage>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 12 | => CustomSerializationHelpers.SerializeInstance(this, SerializeAssistantChatMessage, writer, options); |
| 13 | |
| 14 | internal static void SerializeAssistantChatMessage(AssistantChatMessage instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => instance.WriteCore(writer, options); |
| 16 | |
| 17 | protected override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | writer.WriteStartObject(); |
| 20 | if (Optional.IsDefined(ParticipantName)) |
| 21 | { |
| 22 | writer.WritePropertyName("name"u8); |
| 23 | writer.WriteStringValue(ParticipantName); |
| 24 | } |
| 25 | if (Optional.IsCollectionDefined(ToolCalls)) |
| 26 | { |
| 27 | writer.WritePropertyName("tool_calls"u8); |
| 28 | writer.WriteStartArray(); |
| 29 | foreach (var item in ToolCalls) |
| 30 | { |
| 31 | writer.WriteObjectValue(item, options); |
| 32 | } |
| 33 | writer.WriteEndArray(); |
| 34 | } |
| 35 | if (Optional.IsDefined(FunctionCall)) |
| 36 | { |
| 37 | if (FunctionCall != null) |
| 38 | { |
| 39 | writer.WritePropertyName("function_call"u8); |
| 40 | writer.WriteObjectValue(FunctionCall, options); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | writer.WriteNull("function_call"); |
| 45 | } |
| 46 | } |
| 47 | writer.WritePropertyName("role"u8); |
| 48 | writer.WriteStringValue(Role); |
| 49 | if (Optional.IsCollectionDefined(Content)) |
| 50 | { |
| 51 | if (Content[0] != null) |
| 52 | { |
| 53 | writer.WritePropertyName("content"u8); |
| 54 | writer.WriteStringValue(Content[0].Text); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | writer.WriteNull("content"); |
| 59 | } |
| 60 | } |
| 61 | writer.WriteSerializedAdditionalRawData(_serializedAdditionalRawData, options); |
| 62 | writer.WriteEndObject(); |
| 63 | } |
| 64 | } |