Skip to content

Commit

Permalink
Merge pull request #273 from Founntain/removal-of-newtonsoft-json
Browse files Browse the repository at this point in the history
Added new IJsonService and replaced Newtonsoft.Json with it
  • Loading branch information
Founntain committed Apr 30, 2024
2 parents 78d5b15 + 6afee14 commit 96f6e19
Show file tree
Hide file tree
Showing 27 changed files with 230 additions and 92 deletions.
2 changes: 1 addition & 1 deletion OsuPlayer.CrashHandler/OsuPlayer.CrashHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.10" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.10" />
<PackageReference Include="Founntain.Nein" Version="2023.1115.0" />
<PackageReference Include="Founntain.Nein" Version="2024.427.0" />
<PackageReference Include="ReactiveUI" Version="19.6.1" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion OsuPlayer.Data/LazerModels/Beatmaps/BeatmapInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using Newtonsoft.Json;
using OsuPlayer.Data.LazerModels.Extensions;
using OsuPlayer.Data.LazerModels.Files;
using Realms;
Expand Down
10 changes: 5 additions & 5 deletions OsuPlayer.Data/LazerModels/Beatmaps/BeatmapMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using OsuPlayer.Data.LazerModels.Extensions;
using OsuPlayer.Data.LazerModels.Interfaces;
using Realms;
Expand All @@ -16,17 +16,17 @@ public class BeatmapMetadata : RealmObject, IBeatmapMetadataInfo

public string Title { get; set; } = string.Empty;

[JsonProperty("title_unicode")]
[JsonPropertyName("title_unicode")]
public string TitleUnicode { get; set; } = string.Empty;

public string Artist { get; set; } = string.Empty;

[JsonProperty("artist_unicode")]
[JsonPropertyName("artist_unicode")]
public string ArtistUnicode { get; set; } = string.Empty;

public string Source { get; set; } = string.Empty;

[JsonProperty(@"tags")]
[JsonPropertyName(@"tags")]
public string Tags { get; set; } = string.Empty;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions OsuPlayer.Data/LazerModels/Beatmaps/BeatmapSetInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using OsuPlayer.Data.LazerModels.Extensions;
using OsuPlayer.Data.LazerModels.Files;
using Realms;
Expand Down
2 changes: 1 addition & 1 deletion OsuPlayer.Data/LazerModels/Files/IHasGuidPrimaryKey.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Realms;

namespace OsuPlayer.Data.LazerModels.Files;
Expand Down
3 changes: 1 addition & 2 deletions OsuPlayer.Data/OsuPlayer.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Founntain.Nein" Version="2023.1115.0" />
<PackageReference Include="Founntain.Nein" Version="2024.427.0" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="ManagedBass" Version="3.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OsuPlayer.Api.Data" Version="1.0.12" />
</ItemGroup>

Expand Down
5 changes: 1 addition & 4 deletions OsuPlayer.IO/DbReader/RealmReader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Newtonsoft.Json;
using OsuPlayer.Data.DataModels;
using OsuPlayer.Data.DataModels;
using OsuPlayer.Data.DataModels.Interfaces;
using OsuPlayer.Data.LazerModels.Beatmaps;
using OsuPlayer.Data.LazerModels.Collections;
using OsuPlayer.Data.LazerModels.Files;
using OsuPlayer.Interfaces.Service;
using OsuPlayer.Services;
using Realms;
using Realms.Dynamic;
using Splat;
Expand Down
3 changes: 1 addition & 2 deletions OsuPlayer.IO/OsuPlayer.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="DynamicData" Version="8.4.1" />
<PackageReference Include="Founntain.Nein" Version="2023.1115.0" />
<PackageReference Include="Founntain.Nein" Version="2024.427.0" />
<PackageReference Include="MongoDB.Bson" Version="2.25.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Realm" Version="11.6.1" >
<IncludeAssets>native</IncludeAssets>
</PackageReference>
Expand Down
10 changes: 2 additions & 8 deletions OsuPlayer.IO/Storage/Blacklist/Blacklist.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using Newtonsoft.Json;
using OsuPlayer.Data.DataModels.Interfaces;
using OsuPlayer.Data.DataModels.Interfaces;
using OsuPlayer.Data.OsuPlayer.StorageModels;

namespace OsuPlayer.IO.Storage.Blacklist;

public class Blacklist : Storable<BlacklistContainer>
public sealed class Blacklist : Storable<BlacklistContainer>
{
private static BlacklistContainer? _blacklistContainer;

protected override JsonSerializerSettings SerializerSettings { get; } = new()
{
Formatting = Formatting.None
};

public override string? Path => System.IO.Path.Combine("data", "blacklist.json");

public Blacklist()
Expand Down
10 changes: 2 additions & 8 deletions OsuPlayer.IO/Storage/Playlists/PlaylistStorage.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using Newtonsoft.Json;
using OsuPlayer.Data.OsuPlayer.StorageModels;
using OsuPlayer.Data.OsuPlayer.StorageModels;

namespace OsuPlayer.IO.Storage.Playlists;

/// <summary>
/// The PlaylistStorage reads and writes playlist data to the playlists.json file located in the data folder.
/// </summary>
public class PlaylistStorage : Storable<PlaylistContainer>
public sealed class PlaylistStorage : Storable<PlaylistContainer>
{
private static PlaylistContainer? _playlistContainer;

protected override JsonSerializerSettings SerializerSettings { get; } = new()
{
Formatting = Formatting.None
};

public override string Path => System.IO.Path.Combine("data", "playlists.json");

public PlaylistStorage()
Expand Down
38 changes: 18 additions & 20 deletions OsuPlayer.IO/Storage/Storable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using Nein.Extensions;
using OsuPlayer.Data.OsuPlayer.StorageModels;
using OsuPlayer.Interfaces.Service;
using Splat;

namespace OsuPlayer.IO.Storage;

Expand All @@ -12,11 +14,7 @@ namespace OsuPlayer.IO.Storage;
public abstract class Storable<T> : IDisposable, IAsyncDisposable where T : IStorableContainer, new()
{
private T? _storableContainer;

protected virtual JsonSerializerSettings SerializerSettings { get; } = new()
{
Formatting = Formatting.Indented
};
private readonly IJsonService _jsonService;

/// <summary>
/// The path in which the <see cref="IStorableContainer" /> is to be stored
Expand All @@ -36,6 +34,11 @@ public T Container
set => _storableContainer = value;
}

protected Storable()
{
_jsonService = Locator.Current.GetRequiredService<IJsonService>();
}

public async ValueTask DisposeAsync()
{
if (_storableContainer != null)
Expand Down Expand Up @@ -67,14 +70,11 @@ public virtual T Read()
{
return _storableContainer ??= (string.IsNullOrWhiteSpace(data)
? (T) new T().Init()
: JsonConvert.DeserializeObject<T>(data, SerializerSettings))!;
: _jsonService.Deserialize<T>(data))!;
}
catch (JsonReaderException e)
catch (JsonException)
{
var rawJson = JObject.Parse(data);
rawJson.Remove(e.Path ?? string.Empty);

return _storableContainer = JsonConvert.DeserializeObject<T>(rawJson.ToString(), SerializerSettings)!;
return _storableContainer ??= (T) new T().Init();
}
}

Expand All @@ -93,13 +93,11 @@ public virtual async Task<T> ReadAsync()
{
return _storableContainer ??= (string.IsNullOrWhiteSpace(data)
? (T) new T().Init()
: JsonConvert.DeserializeObject<T>(data, SerializerSettings))!;
: await _jsonService.DeserializeAsync<T>(data))!;
}
catch (JsonReaderException e)
catch (JsonException)
{
var rawJson = JObject.Parse(data);
rawJson.Remove(e.Path ?? string.Empty);
return _storableContainer = JsonConvert.DeserializeObject<T>(rawJson.ToString(), SerializerSettings)!;
return _storableContainer ??= (T)new T().Init();
}
}

Expand All @@ -123,7 +121,7 @@ private void TrySave(T container)
for (var i = 0; i < 3; i++)
try
{
File.WriteAllText(Path!, JsonConvert.SerializeObject(container, SerializerSettings));
_jsonService.SerializeToJsonFile(Path!, container);
break;
}
catch (Exception e)
Expand Down Expand Up @@ -153,7 +151,7 @@ private async Task TrySaveAsync(T container)
for (var i = 0; i < 3; i++)
try
{
await File.WriteAllTextAsync(Path!, JsonConvert.SerializeObject(container, SerializerSettings));
await _jsonService.SerializeToJsonFileAsync(Path!, container);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion OsuPlayer.Interfaces/OsuPlayer.Interfaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<ItemGroup>
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="Founntain.Nein" Version="2023.1115.0" />
<PackageReference Include="Founntain.Nein" Version="2024.427.0" />
<PackageReference Include="LiveChartsCore" Version="2.0.0-rc2" />
<PackageReference Include="OsuPlayer.Api.Data" Version="1.0.12" />
</ItemGroup>
Expand Down
58 changes: 58 additions & 0 deletions OsuPlayer.Interfaces/Service/IJsonService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace OsuPlayer.Interfaces.Service;

public interface IJsonService
{
/// <summary>
/// Deserializes the JSON string to an object of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize to.</typeparam>
/// <param name="json">The JSON string to deserialize.</param>
/// <returns>An object of type <typeparamref name="T"/>.</returns>
T Deserialize<T>(string json);

/// <summary>
/// Deserializes the JSON string to an object of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize to.</typeparam>
/// <param name="jsonStream">The JSON string to deserialize.</param>
/// <returns>An object of type <typeparamref name="T"/>.</returns>
T Deserialize<T>(Stream jsonStream);

/// <summary>
/// Deserializes the JSON string to an object of type <typeparamref name="T"/> asynchronously.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize to.</typeparam>
/// <param name="json">The JSON string to deserialize.</param>
/// <returns>A task representing the asynchronous operation that returns an object of type <typeparamref name="T"/>.</returns>
Task<T> DeserializeAsync<T>(string json);

/// <summary>
/// Deserializes the JSON string to an object of type <typeparamref name="T"/> asynchronously.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize to.</typeparam>
/// <param name="jsonStream">The JSON string to deserialize.</param>
/// <returns>A <see cref="Task{T}"/> representing the asynchronous operation that returns an object of type <typeparamref name="T"/>.</returns>
Task<T> DeserializeAsync<T>(Stream jsonStream);

/// <summary>
/// Serializes an object to a JSON string asynchronously.
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <returns>A <see cref="Task{T}"/> representing the asynchronous operation. The task result contains the JSON string.</returns>
Task<string> SerializeToJsonStringAsync(object obj);

/// <summary>
/// Serializes the object to a JSON file at the specified file path.
/// </summary>
/// <param name="filePath">The file path to save the JSON file.</param>
/// <param name="obj">The object to serialize.</param>
void SerializeToJsonFile(string filePath, object obj);

/// <summary>
/// Serializes an object to JSON and writes it to a file asynchronously.
/// </summary>
/// <param name="filePath">The path to the file where the JSON should be written.</param>
/// <param name="obj">The object to serialize and write to the file.</param>
/// <returns>A <see cref="Task{T}"/> representing the asynchronous operation.</returns>
Task SerializeToJsonFileAsync(string filePath, object obj);
}

0 comments on commit 96f6e19

Please sign in to comment.