1
0
forked from liza/Questionable
Questionable/Questionable.Model/V1/Converter/StringListOrValueConverter.cs

44 lines
1.2 KiB
C#
Raw Normal View History

2024-07-19 14:31:38 +00:00
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Questionable.Model.V1.Converter;
public sealed class StringListOrValueConverter : JsonConverter<List<string>>
{
public override List<string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2024-07-19 14:31:38 +00:00
{
if (reader.TokenType == JsonTokenType.String)
return [reader.GetString()!];
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
reader.Read();
List<string> value = [];
while (reader.TokenType != JsonTokenType.EndArray)
{
value.Add(reader.GetString()!);
reader.Read();
}
return value;
}
public override void Write(Utf8JsonWriter writer, List<string>? value, JsonSerializerOptions options)
{
if (value == null)
writer.WriteNullValue();
else if (value.Count == 1)
writer.WriteStringValue(value[0]);
else
{
writer.WriteStartArray();
foreach (var v in value)
writer.WriteStringValue(v);
writer.WriteEndArray();
}
}
}