using System.Text.Json; using System.Text.Json.Serialization; namespace AIStudio.Provider.Google; public sealed record GoogleEmbeddingResponse { [JsonConverter(typeof(GoogleEmbeddingListConverter))] public List? Embedding { get; init; } private sealed class GoogleEmbeddingListConverter : JsonConverter> { public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.StartObject) { var single = JsonSerializer.Deserialize(ref reader, options); return single is null ? new() : new() { single }; } if (reader.TokenType == JsonTokenType.StartArray) return JsonSerializer.Deserialize>(ref reader, options) ?? new(); throw new JsonException("Expected object or array for embedding."); } public override void Write(Utf8JsonWriter writer, List value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, options); } }