openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Internal/BinaryContentHelper.cs
134lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Text.Json; |
| 9 | |
| 10 | namespace OpenAI |
| 11 | { |
| 12 | internal static partial class BinaryContentHelper |
| 13 | { |
| 14 | public static BinaryContent FromEnumerable<T>(IEnumerable<T> enumerable) |
| 15 | where T : notnull |
| 16 | { |
| 17 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 18 | content.JsonWriter.WriteStartArray(); |
| 19 | foreach (var item in enumerable) |
| 20 | { |
| 21 | content.JsonWriter.WriteObjectValue(item, ModelSerializationExtensions.WireOptions); |
| 22 | } |
| 23 | content.JsonWriter.WriteEndArray(); |
| 24 | |
| 25 | return content; |
| 26 | } |
| 27 | |
| 28 | public static BinaryContent FromEnumerable(IEnumerable<BinaryData> enumerable) |
| 29 | { |
| 30 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 31 | content.JsonWriter.WriteStartArray(); |
| 32 | foreach (var item in enumerable) |
| 33 | { |
| 34 | if (item == null) |
| 35 | { |
| 36 | content.JsonWriter.WriteNullValue(); |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | #if NET6_0_OR_GREATER |
| 41 | content.JsonWriter.WriteRawValue(item); |
| 42 | #else |
| 43 | using (JsonDocument document = JsonDocument.Parse(item)) |
| 44 | { |
| 45 | JsonSerializer.Serialize(content.JsonWriter, document.RootElement); |
| 46 | } |
| 47 | #endif |
| 48 | } |
| 49 | } |
| 50 | content.JsonWriter.WriteEndArray(); |
| 51 | |
| 52 | return content; |
| 53 | } |
| 54 | |
| 55 | public static BinaryContent FromEnumerable<T>(ReadOnlySpan<T> span) |
| 56 | where T : notnull |
| 57 | { |
| 58 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 59 | content.JsonWriter.WriteStartArray(); |
| 60 | int i = 0; |
| 61 | for (; i < span.Length; i++) |
| 62 | { |
| 63 | content.JsonWriter.WriteObjectValue(span[i], ModelSerializationExtensions.WireOptions); |
| 64 | } |
| 65 | content.JsonWriter.WriteEndArray(); |
| 66 | |
| 67 | return content; |
| 68 | } |
| 69 | |
| 70 | public static BinaryContent FromDictionary<TValue>(IDictionary<string, TValue> dictionary) |
| 71 | where TValue : notnull |
| 72 | { |
| 73 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 74 | content.JsonWriter.WriteStartObject(); |
| 75 | foreach (var item in dictionary) |
| 76 | { |
| 77 | content.JsonWriter.WritePropertyName(item.Key); |
| 78 | content.JsonWriter.WriteObjectValue(item.Value, ModelSerializationExtensions.WireOptions); |
| 79 | } |
| 80 | content.JsonWriter.WriteEndObject(); |
| 81 | |
| 82 | return content; |
| 83 | } |
| 84 | |
| 85 | public static BinaryContent FromDictionary(IDictionary<string, BinaryData> dictionary) |
| 86 | { |
| 87 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 88 | content.JsonWriter.WriteStartObject(); |
| 89 | foreach (var item in dictionary) |
| 90 | { |
| 91 | content.JsonWriter.WritePropertyName(item.Key); |
| 92 | if (item.Value == null) |
| 93 | { |
| 94 | content.JsonWriter.WriteNullValue(); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | #if NET6_0_OR_GREATER |
| 99 | content.JsonWriter.WriteRawValue(item.Value); |
| 100 | #else |
| 101 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 102 | { |
| 103 | JsonSerializer.Serialize(content.JsonWriter, document.RootElement); |
| 104 | } |
| 105 | #endif |
| 106 | } |
| 107 | } |
| 108 | content.JsonWriter.WriteEndObject(); |
| 109 | |
| 110 | return content; |
| 111 | } |
| 112 | |
| 113 | public static BinaryContent FromObject(object value) |
| 114 | { |
| 115 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 116 | content.JsonWriter.WriteObjectValue<object>(value, ModelSerializationExtensions.WireOptions); |
| 117 | return content; |
| 118 | } |
| 119 | |
| 120 | public static BinaryContent FromObject(BinaryData value) |
| 121 | { |
| 122 | Utf8JsonBinaryContent content = new Utf8JsonBinaryContent(); |
| 123 | #if NET6_0_OR_GREATER |
| 124 | content.JsonWriter.WriteRawValue(value); |
| 125 | #else |
| 126 | using (JsonDocument document = JsonDocument.Parse(value)) |
| 127 | { |
| 128 | JsonSerializer.Serialize(content.JsonWriter, document.RootElement); |
| 129 | } |
| 130 | #endif |
| 131 | return content; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |