diff --git a/AdamController.Services/AdamController.Services.csproj b/AdamController.Services/AdamController.Services.csproj index d25918a..1d184a5 100644 --- a/AdamController.Services/AdamController.Services.csproj +++ b/AdamController.Services/AdamController.Services.csproj @@ -15,7 +15,6 @@ - diff --git a/AdamController.Services/WebViewProviderDependency/WebMessageJsonReceived.cs b/AdamController.Services/WebViewProviderDependency/WebMessageJsonReceived.cs index b7132fb..44516d5 100644 --- a/AdamController.Services/WebViewProviderDependency/WebMessageJsonReceived.cs +++ b/AdamController.Services/WebViewProviderDependency/WebMessageJsonReceived.cs @@ -1,14 +1,14 @@ -using Newtonsoft.Json; -using System; +using System; +using System.Text.Json.Serialization; namespace AdamController.Services.WebViewProviderDependency { public class WebMessageJsonReceived : EventArgs { - [JsonProperty("action")] + [JsonPropertyName("action")] public string Action { get; set; } - [JsonProperty("data")] + [JsonPropertyName("data")] public string Data { get; set; } } } diff --git a/Legacy/AdamBlocklyLibrary/AdamBlocklyLibrary.csproj b/Legacy/AdamBlocklyLibrary/AdamBlocklyLibrary.csproj index a1d44e5..11d4c2d 100644 --- a/Legacy/AdamBlocklyLibrary/AdamBlocklyLibrary.csproj +++ b/Legacy/AdamBlocklyLibrary/AdamBlocklyLibrary.csproj @@ -47,7 +47,4 @@ Resources.Designer.cs - - - \ No newline at end of file diff --git a/Legacy/AdamBlocklyLibrary/Enum/BlocklyTheme.cs b/Legacy/AdamBlocklyLibrary/Enum/BlocklyTheme.cs index b6f596d..30ad9d4 100644 --- a/Legacy/AdamBlocklyLibrary/Enum/BlocklyTheme.cs +++ b/Legacy/AdamBlocklyLibrary/Enum/BlocklyTheme.cs @@ -1,9 +1,8 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; +using System.Text.Json.Serialization; namespace AdamBlocklyLibrary.Enum { - [JsonConverter(typeof(StringEnumConverter))] + [JsonConverter(typeof(JsonStringEnumConverter))] public enum BlocklyTheme { Classic, diff --git a/Legacy/AdamBlocklyLibrary/Enum/CategoryStyle.cs b/Legacy/AdamBlocklyLibrary/Enum/CategoryStyle.cs index 74b0f75..ad971be 100644 --- a/Legacy/AdamBlocklyLibrary/Enum/CategoryStyle.cs +++ b/Legacy/AdamBlocklyLibrary/Enum/CategoryStyle.cs @@ -1,9 +1,8 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; +using System.Text.Json.Serialization; namespace AdamBlocklyLibrary.Enum { - [JsonConverter(typeof(StringEnumConverter))] + [JsonConverter(typeof(JsonStringEnumConverter))] public enum CategoryStyle { colour_category, diff --git a/Legacy/AdamBlocklyLibrary/Enum/Render.cs b/Legacy/AdamBlocklyLibrary/Enum/Render.cs index b87dc3a..dd678f7 100644 --- a/Legacy/AdamBlocklyLibrary/Enum/Render.cs +++ b/Legacy/AdamBlocklyLibrary/Enum/Render.cs @@ -1,9 +1,8 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; +using System.Text.Json.Serialization; namespace AdamBlocklyLibrary.Enum { - [JsonConverter(typeof(StringEnumConverter))] + [JsonConverter(typeof(JsonStringEnumConverter))] public enum Render { geras, diff --git a/Legacy/AdamBlocklyLibrary/Scripts.cs b/Legacy/AdamBlocklyLibrary/Scripts.cs index 38ee836..31df03c 100644 --- a/Legacy/AdamBlocklyLibrary/Scripts.cs +++ b/Legacy/AdamBlocklyLibrary/Scripts.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using System.Text.Json; namespace AdamBlocklyLibrary { @@ -56,7 +56,8 @@ public static string SerealizeObjectToJsonString(string functionName, params obj string script = functionName + "('"; for (int i = 0; i < parameters.Length; i++) { - script += JsonConvert.SerializeObject(parameters[i]); + script += JsonSerializer.Serialize(parameters[i]); + if (i < parameters.Length - 1) { script += ", "; @@ -72,7 +73,8 @@ public static string SerealizeObject(string functionName, params object[] parame string script = functionName + "("; for (int i = 0; i < parameters.Length; i++) { - script += JsonConvert.SerializeObject(parameters[i]); + script += JsonSerializer.Serialize(parameters[i]); + if (i < parameters.Length - 1) { script += ", "; diff --git a/Legacy/AdamBlocklyLibrary/Toolbox/CategoryToolbox.cs b/Legacy/AdamBlocklyLibrary/Toolbox/CategoryToolbox.cs index 00b08e1..ef3a72c 100644 --- a/Legacy/AdamBlocklyLibrary/Toolbox/CategoryToolbox.cs +++ b/Legacy/AdamBlocklyLibrary/Toolbox/CategoryToolbox.cs @@ -1,33 +1,33 @@  using AdamBlocklyLibrary.Enum; -using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Text.Json.Serialization; namespace AdamBlocklyLibrary.Toolbox { public class CategoryToolbox { - [JsonProperty("kind")] + [JsonPropertyName("kind")] public string Kind { get; } = "category"; /// /// Name toolbox /// - [JsonProperty("name")] + [JsonPropertyName("name")] public string Name { get; set; } - [JsonProperty("toolboxitemid")] - internal string ToolboxItemId => Guid.NewGuid().ToString(); + [JsonPropertyName("toolboxitemid")] + public string ToolboxItemId => Guid.NewGuid().ToString(); - [JsonProperty("categorystyle")] + [JsonPropertyName("categorystyle")] public CategoryStyle CategoryStyle { get; set; } /// /// For set contents use or . /// If is given, then in will not be serialized /// - [JsonProperty("contents")] + [JsonPropertyName("contents")] public object Contents => CategoryToolboxContents ?? (object)SimpleToolboxContents; /// @@ -47,7 +47,7 @@ public class CategoryToolbox /// Note the British spelling. The colour is a number (0-360) defining the hue /// By default categry null, is visible without color /// - [JsonProperty("colour")] + [JsonPropertyName("colour")] public string Colour { get; set; } = null; /// @@ -60,7 +60,7 @@ public class CategoryToolbox /// /// For set ExpandedCategory use Expanded /// - [JsonProperty("expanded")] + [JsonPropertyName("expanded")] internal string ExpandedCategory => Expanded ? Expanded.ToString().ToLower() : null; /// @@ -72,7 +72,7 @@ public class CategoryToolbox /// /// For set HiddenCategory use Hidden /// - [JsonProperty("hidden")] + [JsonPropertyName("hidden")] internal string HiddenCategory => Hidden.ToString().ToLower(); } diff --git a/Legacy/AdamBlocklyLibrary/Toolbox/SimpleToolbox.cs b/Legacy/AdamBlocklyLibrary/Toolbox/SimpleToolbox.cs index d5034db..738de5a 100644 --- a/Legacy/AdamBlocklyLibrary/Toolbox/SimpleToolbox.cs +++ b/Legacy/AdamBlocklyLibrary/Toolbox/SimpleToolbox.cs @@ -1,17 +1,17 @@ using AdamBlocklyLibrary.Enum; -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace AdamBlocklyLibrary.Toolbox { public class SimpleToolbox { - [JsonProperty("kind")] + [JsonPropertyName("kind")] public string Kind { get; } = "block"; [JsonIgnore] public BlockType Type { get; set; } - [JsonProperty("type")] + [JsonPropertyName("type")] public string TypeString => Type.ToString(); } } diff --git a/Legacy/AdamBlocklyLibrary/Toolbox/Toolbox.cs b/Legacy/AdamBlocklyLibrary/Toolbox/Toolbox.cs index d721cee..e7f3b9d 100644 --- a/Legacy/AdamBlocklyLibrary/Toolbox/Toolbox.cs +++ b/Legacy/AdamBlocklyLibrary/Toolbox/Toolbox.cs @@ -1,19 +1,19 @@ -using Newtonsoft.Json; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Text.Json.Serialization; namespace AdamBlocklyLibrary.Toolbox { public class Toolbox { - [JsonProperty("kind")] + [JsonPropertyName("kind")] public string Kind { get; private set; } = "categoryToolbox"; /// /// For set contents use or . /// If is given, then in will not be serialized /// - [JsonProperty("contents")] - internal object Contents => CategoryToolboxContents ?? (object)SimpleToolboxContents; + [JsonPropertyName("contents")] + public object Contents => CategoryToolboxContents ?? (object)SimpleToolboxContents; /// /// If is given, then in will not be serialized diff --git a/Modules/AdamController.Modules.ContentRegion/Views/ScratchControlView.xaml.cs b/Modules/AdamController.Modules.ContentRegion/Views/ScratchControlView.xaml.cs index c79de2f..f51bca2 100644 --- a/Modules/AdamController.Modules.ContentRegion/Views/ScratchControlView.xaml.cs +++ b/Modules/AdamController.Modules.ContentRegion/Views/ScratchControlView.xaml.cs @@ -4,9 +4,10 @@ using AdamController.Services.Interfaces; using AdamController.Services.WebViewProviderDependency; using Microsoft.Web.WebView2.Core; -using Newtonsoft.Json; using System; using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -19,19 +20,21 @@ public partial class ScratchControlView : UserControl #region Services private readonly IWebViewProvider mWebViewProvider; - + private readonly IStatusBarNotificationDeliveryService mStatusBarNotification; + #endregion private readonly string mPathToSource = Path.Combine(FolderHelper.CommonDirAppData, "BlocklySource"); private readonly string mPath = Path.Combine(Path.GetTempPath(), "AdamBrowser"); - public ScratchControlView(IWebViewProvider webViewProvider) + public ScratchControlView(IWebViewProvider webViewProvider, IStatusBarNotificationDeliveryService statusBarNotification) { InitializeComponent(); InitializeWebViewCore(); mWebViewProvider = webViewProvider; + mStatusBarNotification = statusBarNotification; WebView.CoreWebView2InitializationCompleted += WebViewCoreWebView2InitializationCompleted; WebView.NavigationCompleted += WebViewNavigationCompleted; @@ -72,18 +75,34 @@ private async void InitializeWebViewCore() private void WebViewCoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) { - WebView.CoreWebView2.Settings.AreDevToolsEnabled = true; + //unused options? + //WebView.CoreWebView2.Settings.AreDevToolsEnabled = true; + //WebView.CoreWebView2.Settings.AreHostObjectsAllowed = true; + WebView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = !Settings.Default.DontShowBrowserMenuInBlockly; - WebView.CoreWebView2.Settings.AreHostObjectsAllowed = true; WebView.CoreWebView2.SetVirtualHostNameToFolderMapping("localhost", mPathToSource, CoreWebView2HostResourceAccessKind.Allow); WebView.CoreWebView2.Navigate("https://localhost/index.html"); } private void WebViewWebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) { - dynamic jsonClean = JsonConvert.DeserializeObject(e.WebMessageAsJson); - WebMessageJsonReceived receivedResult = JsonConvert.DeserializeObject(jsonClean); - if (receivedResult == null) return; + JsonSerializerOptions options = new() + { + NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString + }; + + WebMessageJsonReceived receivedResult; + + try + { + string receivedString = e.TryGetWebMessageAsString(); + receivedResult = JsonSerializer.Deserialize(receivedString, options); + } + catch + { + mStatusBarNotification.AppLogMessage = "Error reading blokly code"; + receivedResult = new WebMessageJsonReceived { Action = string.Empty, Data = string.Empty }; + } mWebViewProvider.WebViewMessageReceived(receivedResult); }