mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 14:08:08 +01:00
30 lines
974 B
C#
30 lines
974 B
C#
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);
|
||
}
|
||
}
|
||
}
|