diff --git a/Flow.Launcher/Storage/QueryHistory.cs b/Flow.Launcher/Storage/QueryHistory.cs index 2b21036059d..a5383b179ab 100644 --- a/Flow.Launcher/Storage/QueryHistory.cs +++ b/Flow.Launcher/Storage/QueryHistory.cs @@ -1,13 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; -using Flow.Launcher.Plugin; +using System.Text.Json.Serialization; namespace Flow.Launcher.Storage { public class History { - public List Items { get; set; } = new List(); + [JsonInclude] + public List Items { get; private set; } = new List(); private int _maxHistory = 300; diff --git a/Flow.Launcher/Storage/TopMostRecord.cs b/Flow.Launcher/Storage/TopMostRecord.cs index c92ef49562a..052c296cdde 100644 --- a/Flow.Launcher/Storage/TopMostRecord.cs +++ b/Flow.Launcher/Storage/TopMostRecord.cs @@ -1,6 +1,4 @@ using System.Collections.Generic; -using System.Linq; -using System.Text.Json; using System.Text.Json.Serialization; using Flow.Launcher.Plugin; @@ -9,14 +7,8 @@ namespace Flow.Launcher.Storage // todo this class is not thread safe.... but used from multiple threads. public class TopMostRecord { - /// - /// You should not directly access this field - /// - /// It is public due to System.Text.Json limitation in version 3.1 - /// - /// - /// TODO: Set it to private - public Dictionary records { get; set; } = new Dictionary(); + [JsonInclude] + public Dictionary records { get; private set; } = new Dictionary(); internal bool IsTopMost(Result result) { diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index bc7a2da73ce..fd3d87fc4c6 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -7,15 +7,8 @@ namespace Flow.Launcher.Storage { public class UserSelectedRecord { - /// - /// You should not directly access this field - /// - /// It is public due to System.Text.Json limitation in version 3.1 - /// - /// - /// TODO: Set it to private - [JsonPropertyName("records")] - public Dictionary records { get; set; } + [JsonInclude] + public Dictionary records { get; private set; } public UserSelectedRecord() { diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index 28868100f22..966a6ca16db 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -17,7 +17,6 @@ class Bing : SuggestionSource { public override async Task> Suggestions(string query, CancellationToken token) { - JsonElement json; try { @@ -25,7 +24,20 @@ public override async Task> Suggestions(string query, CancellationT using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false); - json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)).RootElement.GetProperty("AS"); + using var json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)); + var root = json.RootElement.GetProperty("AS"); + + if (root.GetProperty("FullResults").GetInt32() == 0) + return new List(); + + return root.GetProperty("Results") + .EnumerateArray() + .SelectMany(r => r.GetProperty("Suggests") + .EnumerateArray() + .Select(s => s.GetProperty("Txt").GetString())) + .ToList(); + + } catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) @@ -41,18 +53,7 @@ public override async Task> Suggestions(string query, CancellationT { Log.Exception("|Bing.Suggestions|can't parse suggestions", e); return new List(); - } - - if (json.GetProperty("FullResults").GetInt32() == 0) - return new List(); - - return json.GetProperty("Results") - .EnumerateArray() - .SelectMany(r => r.GetProperty("Suggests") - .EnumerateArray() - .Select(s => s.GetProperty("Txt").GetString())) - .ToList(); - + } } public override string ToString() diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index a06deea296b..91ab921b634 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -16,15 +16,20 @@ public class Google : SuggestionSource { public override async Task> Suggestions(string query, CancellationToken token) { - JsonDocument json; - try { const string api = "https://www.google.com/complete/search?output=chrome&q="; using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeUriString(query)).ConfigureAwait(false); - json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token); + using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token); + + if (json == null) + return new List(); + + var results = json.RootElement.EnumerateArray().ElementAt(1); + + return results.EnumerateArray().Select(o => o.GetString()).ToList(); } catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) @@ -41,11 +46,6 @@ public override async Task> Suggestions(string query, CancellationT Log.Exception("|Google.Suggestions|can't parse suggestions", e); return new List(); } - - var results = json?.RootElement.EnumerateArray().ElementAt(1); - - return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List(); - } public override string ToString() diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json index 996c9b686e2..e9f237a586e 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json @@ -25,7 +25,7 @@ "Name": "Web Searches", "Description": "Provide the web search ability", "Author": "qianlifeng", - "Version": "1.3.3", + "Version": "1.3.4", "Language": "csharp", "Website": "https://github.com/Flow-Launcher/Flow.Launcher", "ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",