|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace TypeAgent.Common; |
| 5 | + |
| 6 | +public abstract class OneOrManyItem |
| 7 | +{ |
| 8 | + [JsonIgnore] |
| 9 | + public abstract bool IsSingle { get; } |
| 10 | + |
| 11 | + public static OneOrManyItem<T>? Create<T>(T? value) |
| 12 | + { |
| 13 | + return value is not null ? new SingleItem<T>(value) : null; |
| 14 | + } |
| 15 | + |
| 16 | + public static OneOrManyItem<T>? Create<T>(IList<T>? value) |
| 17 | + { |
| 18 | + return value.IsNullOrEmpty() |
| 19 | + ? null |
| 20 | + : value.Count == 1 |
| 21 | + ? new SingleItem<T>(value[0]) |
| 22 | + : new ListItem<T>(value); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +[JsonConverter(typeof(OneOrManyJsonConverterFactory))] |
| 27 | +public abstract class OneOrManyItem<T> : OneOrManyItem |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | +public class SingleItem<T> : OneOrManyItem<T> |
| 32 | +{ |
| 33 | + public SingleItem() { } |
| 34 | + |
| 35 | + public SingleItem(T value) |
| 36 | + { |
| 37 | + Value = value; |
| 38 | + } |
| 39 | + |
| 40 | + [JsonIgnore] |
| 41 | + public override bool IsSingle => true; |
| 42 | + |
| 43 | + public T Value { get; set; } |
| 44 | + |
| 45 | + public static implicit operator T(SingleItem<T> item) |
| 46 | + { |
| 47 | + return item.Value; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +public class ListItem<T> : OneOrManyItem<T> |
| 52 | +{ |
| 53 | + public ListItem() |
| 54 | + { |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public ListItem(IList<T> value) |
| 59 | + { |
| 60 | + Value = value; |
| 61 | + } |
| 62 | + |
| 63 | + [JsonIgnore] |
| 64 | + public override bool IsSingle => false; |
| 65 | + |
| 66 | + public IList<T> Value { get; set; } |
| 67 | +} |
| 68 | + |
| 69 | +public class OneOrManyJsonConverterFactory : JsonConverterFactory |
| 70 | +{ |
| 71 | + public override bool CanConvert(Type typeToConvert) => true; |
| 72 | + |
| 73 | + public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| 74 | + { |
| 75 | + var elementType = typeToConvert.GetGenericArguments()[0]; |
| 76 | + var converterType = typeof(OneOrManyJsonConverter<>).MakeGenericType(elementType); |
| 77 | + return (JsonConverter)Activator.CreateInstance(converterType)!; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +public class OneOrManyJsonConverter<T> : JsonConverter<OneOrManyItem<T>> |
| 82 | +{ |
| 83 | + public override OneOrManyItem<T>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 84 | + { |
| 85 | + if (reader.TokenType == JsonTokenType.Null) |
| 86 | + { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + if (reader.TokenType == JsonTokenType.StartArray) |
| 91 | + { |
| 92 | + var list = JsonSerializer.Deserialize<List<T>>(ref reader, options); |
| 93 | + return new ListItem<T>() |
| 94 | + { |
| 95 | + Value = list |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + T value = JsonSerializer.Deserialize<T>(ref reader, options); |
| 100 | + return new SingleItem<T> |
| 101 | + { |
| 102 | + Value = value |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + public override void Write(Utf8JsonWriter writer, OneOrManyItem<T> value, JsonSerializerOptions options) |
| 107 | + { |
| 108 | + if (value is ListItem<T> list) |
| 109 | + { |
| 110 | + JsonSerializer.Serialize(writer, list.Value, options); |
| 111 | + } |
| 112 | + else if (value is SingleItem<T> item) |
| 113 | + { |
| 114 | + JsonSerializer.Serialize(writer, item.Value, options); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments