openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Utility/CustomSerializationHelpers.cs
167lines · modecode
| 1 | #nullable enable |
| 2 | |
| 3 | using System; |
| 4 | using System.ClientModel.Primitives; |
| 5 | using System.Collections.Generic; |
| 6 | using System.Text.Json; |
| 7 | |
| 8 | namespace OpenAI; |
| 9 | |
| 10 | internal static partial class CustomSerializationHelpers |
| 11 | { |
| 12 | internal static TOutput DeserializeNewInstance<TOutput, UInstanceInput>( |
| 13 | UInstanceInput existingInstance, |
| 14 | Func<JsonElement, ModelReaderWriterOptions?, TOutput> deserializationFunc, |
| 15 | ref Utf8JsonReader reader, |
| 16 | ModelReaderWriterOptions options) |
| 17 | where UInstanceInput : IJsonModel<TOutput> |
| 18 | { |
| 19 | options ??= new("W"); |
| 20 | var format = options.Format == "W" ? ((IJsonModel<TOutput>)existingInstance).GetFormatFromOptions(options) : options.Format; |
| 21 | if (format != "J") |
| 22 | { |
| 23 | throw new FormatException($"The model {nameof(UInstanceInput)} does not support '{format}' format."); |
| 24 | } |
| 25 | |
| 26 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 27 | return deserializationFunc.Invoke(document.RootElement, options); |
| 28 | } |
| 29 | |
| 30 | internal static TOutput DeserializeNewInstance<TOutput, UInstanceInput>( |
| 31 | UInstanceInput existingInstance, |
| 32 | Func<JsonElement, ModelReaderWriterOptions, TOutput> deserializationFunc, |
| 33 | BinaryData data, |
| 34 | ModelReaderWriterOptions options) |
| 35 | where UInstanceInput : IPersistableModel<TOutput> |
| 36 | { |
| 37 | options ??= new("W"); |
| 38 | var format = options.Format == "W" ? ((IPersistableModel<TOutput>)existingInstance).GetFormatFromOptions(options) : options.Format; |
| 39 | |
| 40 | switch (format) |
| 41 | { |
| 42 | case "J": |
| 43 | { |
| 44 | using JsonDocument document = JsonDocument.Parse(data); |
| 45 | return deserializationFunc.Invoke(document.RootElement, options)!; |
| 46 | } |
| 47 | default: |
| 48 | throw new FormatException($"The model {nameof(UInstanceInput)} does not support '{format}' format."); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | internal static void SerializeInstance<TOutput, UInstanceInput>( |
| 53 | UInstanceInput instance, |
| 54 | Action<UInstanceInput, Utf8JsonWriter, ModelReaderWriterOptions> serializationFunc, |
| 55 | Utf8JsonWriter writer, |
| 56 | ModelReaderWriterOptions options) |
| 57 | where UInstanceInput : IJsonModel<TOutput> |
| 58 | { |
| 59 | options ??= new ModelReaderWriterOptions("W"); |
| 60 | AssertSupportedJsonWriteFormat<TOutput, UInstanceInput>(instance, options); |
| 61 | serializationFunc.Invoke(instance, writer, options); |
| 62 | } |
| 63 | |
| 64 | internal static void SerializeInstance<T>( |
| 65 | T instance, |
| 66 | Action<T, Utf8JsonWriter, ModelReaderWriterOptions> serializationFunc, |
| 67 | Utf8JsonWriter writer, |
| 68 | ModelReaderWriterOptions options) |
| 69 | where T : IJsonModel<T> |
| 70 | => SerializeInstance<T, T>(instance, serializationFunc, writer, options); |
| 71 | |
| 72 | internal static BinaryData SerializeInstance<TOutput, UInstanceInput>( |
| 73 | UInstanceInput instance, |
| 74 | ModelReaderWriterOptions options) |
| 75 | where UInstanceInput : IPersistableModel<TOutput> |
| 76 | { |
| 77 | options ??= new("W"); |
| 78 | AssertSupportedPersistableWriteFormat<TOutput, UInstanceInput>(instance, options); |
| 79 | return ModelReaderWriter.Write(instance, options, OpenAIContext.Default); |
| 80 | } |
| 81 | |
| 82 | internal static BinaryData SerializeInstance<T>(T instance, ModelReaderWriterOptions options) |
| 83 | where T : IPersistableModel<T> |
| 84 | => SerializeInstance<T, T>(instance, options); |
| 85 | |
| 86 | internal static void AssertSupportedJsonWriteFormat<T>(T instance, ModelReaderWriterOptions options) |
| 87 | where T : IJsonModel<T> |
| 88 | => AssertSupportedJsonWriteFormat<T, T>(instance, options); |
| 89 | |
| 90 | internal static void AssertSupportedJsonWriteFormat<TOutput, UInstanceInput>(UInstanceInput instance, ModelReaderWriterOptions options) |
| 91 | where UInstanceInput : IJsonModel<TOutput> |
| 92 | { |
| 93 | var format = options.Format == "W" ? ((IJsonModel<TOutput>)instance).GetFormatFromOptions(options) : options.Format; |
| 94 | if (format != "J") |
| 95 | { |
| 96 | throw new FormatException($"The model {nameof(UInstanceInput)} does not support '{format}' format."); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | internal static void AssertSupportedPersistableWriteFormat<T>(T instance, ModelReaderWriterOptions options) |
| 101 | where T : IPersistableModel<T> |
| 102 | => AssertSupportedPersistableWriteFormat<T, T>(instance, options); |
| 103 | |
| 104 | internal static void AssertSupportedPersistableWriteFormat<TOutput, UInstanceInput>(UInstanceInput instance, ModelReaderWriterOptions options) |
| 105 | where UInstanceInput : IPersistableModel<TOutput> |
| 106 | { |
| 107 | var format = options.Format == "W" ? ((IPersistableModel<TOutput>)instance).GetFormatFromOptions(options) : options.Format; |
| 108 | if (format != "J") |
| 109 | { |
| 110 | throw new FormatException($"The model {nameof(UInstanceInput)} does not support '{format}' format."); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | internal static void WriteSerializedAdditionalRawData(this Utf8JsonWriter writer, IDictionary<string, BinaryData> dictionary, ModelReaderWriterOptions options) |
| 115 | { |
| 116 | if (true && dictionary != null) |
| 117 | { |
| 118 | foreach (var item in dictionary) |
| 119 | { |
| 120 | writer.WritePropertyName(item.Key); |
| 121 | #if NET6_0_OR_GREATER |
| 122 | writer.WriteRawValue(item.Value); |
| 123 | #else |
| 124 | using JsonDocument document = JsonDocument.Parse(item.Value); |
| 125 | JsonSerializer.Serialize(writer, document.RootElement); |
| 126 | #endif |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | internal static void WriteOptionalProperty<T>(this Utf8JsonWriter writer, ReadOnlySpan<byte> name, T value, ModelReaderWriterOptions options) |
| 132 | { |
| 133 | if (Optional.IsDefined(value)) |
| 134 | { |
| 135 | writer.WritePropertyName(name); |
| 136 | writer.WriteObjectValue(value, options); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | internal static void WriteOptionalCollection<T>(this Utf8JsonWriter writer, ReadOnlySpan<byte> name, IEnumerable<T> values, ModelReaderWriterOptions options) |
| 141 | { |
| 142 | if (Optional.IsCollectionDefined(values)) |
| 143 | { |
| 144 | writer.WritePropertyName(name); |
| 145 | writer.WriteStartArray(); |
| 146 | foreach (T item in values) |
| 147 | { |
| 148 | writer.WriteObjectValue(item, options); |
| 149 | } |
| 150 | writer.WriteEndArray(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | internal static void WriteFirstObject<T>(this Utf8JsonWriter writer, ModelReaderWriterOptions options, params T[] values) |
| 155 | where T : class, IJsonModel<T> |
| 156 | { |
| 157 | foreach (T value in values) |
| 158 | { |
| 159 | if (value is not null) |
| 160 | { |
| 161 | writer.WriteObjectValue(value, options); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | } |
| 168 | |