Replace JSON.NET with System.Text.json (#840)

This commit is contained in:
RobertBeekman authored and GitHub committed 2024-02-28 19:31:38 +01:00
1 parent e112ca9da8
commit a340f8822e
63 files changed
+466 -584

No files matched your search

@@ -1,15 +1,14 @@
using System.Text.Json;
using System.Threading;
using Artemis.UI.Extensions;
using Artemis.UI.Shared;
using Artemis.WebClient.Workshop.Services;
using Newtonsoft.Json;
using PropertyChanged.SourceGenerator;
namespace Artemis.UI.Screens.Debugger.Workshop;
public partial class WorkshopDebugViewModel : ActivatableViewModelBase
{
[Notify] private string? _token;
[Notify] private bool _emailVerified;
[Notify] private string? _claims;
@@ -18,12 +17,12 @@ public partial class WorkshopDebugViewModel : ActivatableViewModelBase
public WorkshopDebugViewModel(IWorkshopService workshopService, IAuthenticationService authenticationService)
{
DisplayName = "Workshop";
this.WhenActivatedAsync(async _ =>
{
Token = await authenticationService.GetBearer();
EmailVerified = authenticationService.GetIsEmailVerified();
Claims = JsonConvert.SerializeObject(authenticationService.Claims, Formatting.Indented);
Claims = JsonSerializer.Serialize(authenticationService.Claims, new JsonSerializerOptions {WriteIndented = true});
WorkshopStatus = await workshopService.GetWorkshopStatus(CancellationToken.None);
});
}
@@ -14,7 +14,6 @@ using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.ProfileEditor;
using Newtonsoft.Json;
using PropertyChanged.SourceGenerator;
using ReactiveUI;
@@ -6,6 +6,7 @@ using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile;
using Artemis.UI.DryIoc.Factories;
using Artemis.UI.Extensions;
using Artemis.UI.Models;
using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.ProfileEditor;
using Artemis.UI.Shared.Services.ProfileEditor.Commands;
@@ -36,11 +37,9 @@ public class FolderTreeItemViewModel : TreeItemViewModel
{
await ProfileEditorService.SaveProfileAsync();
FolderEntity copy = CoreJson.DeserializeObject<FolderEntity>(CoreJson.SerializeObject(Folder.FolderEntity, true), true)!;
copy.Id = Guid.NewGuid();
copy.Name = Folder.Parent.GetNewFolderName(copy.Name + " - copy");
FolderClipboardModel copy = CoreJson.Deserialize<FolderClipboardModel>(CoreJson.Serialize(new FolderClipboardModel(Folder)))!;
Folder copied = copy.Paste(Folder.Profile, Folder.Parent);
Folder copied = new(Folder.Profile, Folder.Parent, copy);
ProfileEditorService.ExecuteCommand(new AddProfileElement(copied, Folder.Parent, Folder.Order - 1));
}
@@ -37,7 +37,7 @@ public class LayerTreeItemViewModel : TreeItemViewModel
{
await ProfileEditorService.SaveProfileAsync();
LayerEntity copy = CoreJson.DeserializeObject<LayerEntity>(CoreJson.SerializeObject(Layer.LayerEntity, true), true)!;
LayerEntity copy = CoreJson.Deserialize<LayerEntity>(CoreJson.Serialize(Layer.LayerEntity))!;
copy.Id = Guid.NewGuid();
copy.Name = Layer.Parent.GetNewFolderName(copy.Name + " - copy");
@@ -135,7 +135,7 @@ public partial class TimelineKeyframeViewModel<T> : ActivatableViewModelBase, IT
else
keyframes.AddRange(_profileEditorService.SelectedKeyframes.Select(k => new KeyframeClipboardModel(k)));
string copy = CoreJson.SerializeObject(keyframes, true);
string copy = CoreJson.Serialize(keyframes);
DataObject dataObject = new();
dataObject.Set(KeyframeClipboardModel.ClipboardDataFormat, copy);
await Shared.UI.Clipboard.SetDataObjectAsync(dataObject);
@@ -2,12 +2,10 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.Core.Services;
@@ -18,7 +16,6 @@ using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.Builders;
using DynamicData;
using DynamicData.Binding;
using Newtonsoft.Json;
using PropertyChanged.SourceGenerator;
using ReactiveUI;
@@ -151,7 +148,7 @@ public partial class SidebarCategoryViewModel : ActivatableViewModelBase
private async Task ExecuteImportProfile()
{
string[]? result = await _windowService.CreateOpenFileDialog()
.HavingFilter(f => f.WithExtension("zip").WithExtension("json").WithName("Artemis profile"))
.HavingFilter(f => f.WithExtension("zip").WithName("Artemis profile"))
.ShowAsync();
if (result == null)
@@ -159,24 +156,8 @@ public partial class SidebarCategoryViewModel : ActivatableViewModelBase
try
{
// Removing this at some point in the future
if (result[0].EndsWith("json"))
{
ProfileConfigurationExportModel? exportModel = JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(await File.ReadAllTextAsync(result[0]), IProfileService.ExportSettings);
if (exportModel == null)
{
await _windowService.ShowConfirmContentDialog("Import profile", "Failed to import this profile, make sure it is a valid Artemis profile.", "Confirm", null);
return;
}
await using Stream convertedFileStream = await ConvertLegacyExport(exportModel);
await _profileService.ImportProfile(convertedFileStream, ProfileCategory, true, true);
}
else
{
await using FileStream fileStream = File.OpenRead(result[0]);
await _profileService.ImportProfile(fileStream, ProfileCategory, true, true);
}
await using FileStream fileStream = File.OpenRead(result[0]);
await _profileService.ImportProfile(fileStream, ProfileCategory, true, true);
}
catch (Exception e)
{
@@ -230,38 +211,4 @@ public partial class SidebarCategoryViewModel : ActivatableViewModelBase
_profileService.SaveProfileCategory(categories[i]);
}
}
private async Task<Stream> ConvertLegacyExport(ProfileConfigurationExportModel exportModel)
{
MemoryStream archiveStream = new();
string configurationJson = JsonConvert.SerializeObject(exportModel.ProfileConfigurationEntity, IProfileService.ExportSettings);
string profileJson = JsonConvert.SerializeObject(exportModel.ProfileEntity, IProfileService.ExportSettings);
// Create a ZIP archive
using (ZipArchive archive = new(archiveStream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry configurationEntry = archive.CreateEntry("configuration.json");
await using (Stream entryStream = configurationEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(configurationJson));
}
ZipArchiveEntry profileEntry = archive.CreateEntry("profile.json");
await using (Stream entryStream = profileEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(profileJson));
}
if (exportModel.ProfileImage != null)
{
ZipArchiveEntry iconEntry = archive.CreateEntry("icon.png");
await using Stream entryStream = iconEntry.Open();
await exportModel.ProfileImage.CopyToAsync(entryStream);
}
}
archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
}
}
@@ -15,6 +15,7 @@ using Artemis.UI.DryIoc.Factories;
using Artemis.UI.Models;
using Artemis.UI.Screens.VisualScripting.Pins;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Extensions;
using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Avalonia;
@@ -277,18 +278,14 @@ public partial class NodeScriptViewModel : ActivatableViewModelBase
{
List<INode> nodes = NodeViewModels.Where(vm => vm.IsSelected).Select(vm => vm.Node).Where(n => !n.IsDefaultNode && !n.IsExitNode).ToList();
DataObject dataObject = new();
string copy = CoreJson.SerializeObject(new NodesClipboardModel(NodeScript, nodes), true);
string copy = CoreJson.Serialize(new NodesClipboardModel(NodeScript, nodes));
dataObject.Set(CLIPBOARD_DATA_FORMAT, copy);
await Shared.UI.Clipboard.SetDataObjectAsync(dataObject);
}
private async Task ExecutePasteSelected()
{
byte[]? bytes = (byte[]?) await Shared.UI.Clipboard.GetDataAsync(CLIPBOARD_DATA_FORMAT);
if (bytes == null!)
return;
NodesClipboardModel? nodesClipboardModel = CoreJson.DeserializeObject<NodesClipboardModel>(Encoding.Unicode.GetString(bytes), true);
NodesClipboardModel? nodesClipboardModel = await Shared.UI.Clipboard.GetJsonAsync<NodesClipboardModel>(CLIPBOARD_DATA_FORMAT);
if (nodesClipboardModel == null)
return;
@@ -55,7 +55,7 @@ public partial class PluginSelectionStepViewModel : SubmissionViewModel
throw new ArtemisPluginException("Couldn't find a plugin.json in " + files[0]);
using StreamReader reader = new(metaDataFileEntry.Open());
PluginInfo pluginInfo = CoreJson.DeserializeObject<PluginInfo>(reader.ReadToEnd())!;
PluginInfo pluginInfo = CoreJson.Deserialize<PluginInfo>(reader.ReadToEnd())!;
if (!pluginInfo.Main.EndsWith(".dll"))
throw new ArtemisPluginException("Main entry in plugin.json must point to a .dll file");