aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrobibero <cody@robibe.ro>2020-12-12 10:36:17 -0700
committercrobibero <cody@robibe.ro>2020-12-12 10:36:17 -0700
commit8f4a4a3cc56e9a5c0582c2bda3c0608daa781f64 (patch)
tree44a23411dc5ed9c0dd5111caed0c2f7289aee3cf
parentf5cce9e630135eec076a6cb9e7ea08d81bcd45c5 (diff)
Convert values without throwing exception
-rw-r--r--Jellyfin.Data/Entities/User.cs30
1 files changed, 28 insertions, 2 deletions
diff --git a/Jellyfin.Data/Entities/User.cs b/Jellyfin.Data/Entities/User.cs
index 0733cc670..d19cbac7f 100644
--- a/Jellyfin.Data/Entities/User.cs
+++ b/Jellyfin.Data/Entities/User.cs
@@ -5,7 +5,6 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
-using System.Globalization;
using System.Linq;
using System.Text.Json.Serialization;
using Jellyfin.Data.Enums;
@@ -428,9 +427,36 @@ namespace Jellyfin.Data.Entities
return Array.Empty<T>();
}
+ // Convert array of {string} to array of {T}
var converter = TypeDescriptor.GetConverter(typeof(T));
var stringValues = val.Split(Delimiter);
- return Array.ConvertAll(stringValues, value => (T)converter.ConvertFromString(value));
+ var parsedValues = new object[stringValues.Length];
+ var convertedCount = 0;
+ for (var i = 0; i < stringValues.Length; i++)
+ {
+ try
+ {
+ parsedValues[i] = converter.ConvertFromString(stringValues[i].Trim());
+ convertedCount++;
+ }
+ catch (FormatException)
+ {
+ // Unable to convert value
+ }
+ }
+
+ var typedValues = new T[convertedCount];
+ var typedValueIndex = 0;
+ for (var i = 0; i < parsedValues.Length; i++)
+ {
+ if (parsedValues[i] != null)
+ {
+ typedValues.SetValue(parsedValues[i], typedValueIndex);
+ typedValueIndex++;
+ }
+ }
+
+ return typedValues;
}
/// <summary>