From e553eba31e9f0e05effc30417ee53c02d63304bd Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 25 Sep 2019 17:43:20 +0200 Subject: Use System.Text.Json api --- .../Data/SqliteDisplayPreferencesRepository.cs | 36 ++++++++++------------ 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs') diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1a67ab912..2cd4d65b3 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -2,44 +2,44 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Text.Json; using System.Threading; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Json; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Entities; using MediaBrowser.Model.IO; -using MediaBrowser.Model.Serialization; using Microsoft.Extensions.Logging; using SQLitePCL.pretty; namespace Emby.Server.Implementations.Data { /// - /// Class SQLiteDisplayPreferencesRepository + /// Class SQLiteDisplayPreferencesRepository. /// public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository { private readonly IFileSystem _fileSystem; - public SqliteDisplayPreferencesRepository(ILogger logger, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IFileSystem fileSystem) + private readonly JsonSerializerOptions _jsonOptions; + + public SqliteDisplayPreferencesRepository(ILogger logger, IApplicationPaths appPaths, IFileSystem fileSystem) : base(logger) { - _jsonSerializer = jsonSerializer; _fileSystem = fileSystem; + + _jsonOptions = JsonDefaults.GetOptions(); + DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db"); } /// - /// Gets the name of the repository + /// Gets the name of the repository. /// /// The name. public string Name => "SQLite"; - /// - /// The _json serializer - /// - private readonly IJsonSerializer _jsonSerializer; - public void Initialize() { try @@ -106,7 +106,7 @@ namespace Emby.Server.Implementations.Data private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection) { - var serialized = _jsonSerializer.SerializeToBytes(displayPreferences); + var serialized = JsonSerializer.SerializeToUtf8Bytes(displayPreferences, _jsonOptions); using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)")) { @@ -198,15 +198,13 @@ namespace Emby.Server.Implementations.Data var list = new List(); using (var connection = GetConnection(true)) + using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { - using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) - { - statement.TryBind("@userId", userId.ToGuidBlob()); + statement.TryBind("@userId", userId.ToGuidBlob()); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(Get(row)); - } + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); } } @@ -214,7 +212,7 @@ namespace Emby.Server.Implementations.Data } private DisplayPreferences Get(IReadOnlyList row) - => _jsonSerializer.DeserializeFromString(row.GetString(0)); + => JsonSerializer.Deserialize(row[0].ToBlob(), _jsonOptions); public void SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken) => SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken); -- cgit v1.2.3