Enable nullable for ItemDataProvider

This commit is contained in:
Bond_009 2021-10-01 11:21:22 +02:00
parent 531efc345a
commit 1ee58bf020
1 changed files with 10 additions and 4 deletions

View File

@ -1,9 +1,8 @@
#nullable disable
#pragma warning disable CS1591 #pragma warning disable CS1591
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
@ -18,7 +17,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
private readonly string _dataPath; private readonly string _dataPath;
private readonly object _fileDataLock = new object(); private readonly object _fileDataLock = new object();
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
private T[] _items; private T[]? _items;
public ItemDataProvider( public ItemDataProvider(
ILogger logger, ILogger logger,
@ -34,6 +33,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
protected Func<T, T, bool> EqualityComparer { get; } protected Func<T, T, bool> EqualityComparer { get; }
[MemberNotNull(nameof(_items))]
private void EnsureLoaded() private void EnsureLoaded()
{ {
if (_items != null) if (_items != null)
@ -49,6 +49,12 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
{ {
var bytes = File.ReadAllBytes(_dataPath); var bytes = File.ReadAllBytes(_dataPath);
_items = JsonSerializer.Deserialize<T[]>(bytes, _jsonOptions); _items = JsonSerializer.Deserialize<T[]>(bytes, _jsonOptions);
if (_items == null)
{
Logger.LogError("Error deserializing {Path}, data was null", _dataPath);
_items = Array.Empty<T>();
}
return; return;
} }
catch (JsonException ex) catch (JsonException ex)
@ -62,7 +68,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
private void SaveList() private void SaveList()
{ {
Directory.CreateDirectory(Path.GetDirectoryName(_dataPath)); Directory.CreateDirectory(Path.GetDirectoryName(_dataPath) ?? throw new ArgumentException("Path can't be a root directory.", nameof(_dataPath)));
var jsonString = JsonSerializer.Serialize(_items, _jsonOptions); var jsonString = JsonSerializer.Serialize(_items, _jsonOptions);
File.WriteAllText(_dataPath, jsonString); File.WriteAllText(_dataPath, jsonString);
} }