Files
temporal-ai-agent/enterprise/SingleOrArrayConverter.cs
2025-02-14 11:10:16 -08:00

30 lines
974 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace TrainSearchWorker.Converters
{
public class SingleOrArrayConverter<T> : JsonConverter<List<T>>
{
public override List<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartArray)
{
return JsonSerializer.Deserialize<List<T>>(ref reader, options) ?? new List<T>();
}
else
{
// Single element wrap it in a list.
T element = JsonSerializer.Deserialize<T>(ref reader, options);
return new List<T> { element };
}
}
public override void Write(Utf8JsonWriter writer, List<T> value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, options);
}
}
}