Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Source/Schema.NET/OneOrMany{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ public OneOrMany(List<T> list)
}
}

/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="list">The list of values.</param>
public OneOrMany(IEnumerable<object> list)
{
if (list is null)
{
throw new ArgumentNullException(nameof(list));
}

var items = new List<T>();
foreach (var item in list)
{
if (item is T itemT)
{
items.Add(itemT);
}
}

if (items.Count == 1)
{
this.collection = null;
this.item = items[0];
}
else
{
this.collection = items;
this.item = default;
}
}

/// <summary>
/// Gets the number of elements contained in the <see cref="OneOrMany{T}"/>.
/// </summary>
Expand Down
40 changes: 0 additions & 40 deletions Source/Schema.NET/TimeSpanToISO8601DurationValuesJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,6 @@ namespace Schema.NET
/// <seealso cref="ValuesJsonConverter" />
public class TimeSpanToISO8601DurationValuesJsonConverter : ValuesJsonConverter
{
/// <inheritdoc />
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader is null)
{
throw new ArgumentNullException(nameof(reader));
}

if (objectType is null)
{
throw new ArgumentNullException(nameof(objectType));
}

if (serializer is null)
{
throw new ArgumentNullException(nameof(serializer));
}

var valuesType = objectType.GetUnderlyingTypeFromNullable();
if (valuesType != null && valuesType.GenericTypeArguments.Length == 1)
{
var mainType = valuesType.GenericTypeArguments[0];
var genericType = typeof(OneOrMany<TimeSpan>);
if (mainType.IsNullable())
{
mainType = Nullable.GetUnderlyingType(mainType);
genericType = typeof(OneOrMany<TimeSpan?>);
}

if (mainType == typeof(TimeSpan))
{
var timeSpan = XmlConvert.ToTimeSpan(reader.Value.ToString());
var instance = Activator.CreateInstance(genericType, timeSpan);
return instance;
}
}

return base.ReadJson(reader, objectType, existingValue, serializer);
}

/// <summary>
/// Writes the object retrieved from <see cref="IValues"/> when one is found.
/// </summary>
Expand Down
Loading