From 908d1c412498fe5ca3fc3d67950af085a9e656bb Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 11:03:07 +0800 Subject: [PATCH 1/3] JsonRPC camelCase and Case Insensitive --- Flow.Launcher.Core/Plugin/JsonPRCModel.cs | 19 ++++++++----------- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 9 +++++---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonPRCModel.cs b/Flow.Launcher.Core/Plugin/JsonPRCModel.cs index 2b3bd61b0..5232e46da 100644 --- a/Flow.Launcher.Core/Plugin/JsonPRCModel.cs +++ b/Flow.Launcher.Core/Plugin/JsonPRCModel.cs @@ -30,12 +30,8 @@ namespace Flow.Launcher.Core.Plugin public string Data { get; set; } } - public class JsonRPCModelBase - { - public int Id { get; set; } - } - public class JsonRPCResponseModel : JsonRPCModelBase + public class JsonRPCResponseModel { public string Result { get; set; } @@ -49,18 +45,20 @@ namespace Flow.Launcher.Core.Plugin public string DebugMessage { get; set; } } - - public class JsonRPCRequestModel : JsonRPCModelBase + + public class JsonRPCRequestModel { - [JsonPropertyName("method")] public string Method { get; set; } - [JsonPropertyName("parameters")] public object[] Parameters { get; set; } + private static readonly JsonSerializerOptions options = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; public override string ToString() { - return JsonSerializer.Serialize(this); + return JsonSerializer.Serialize(this, options); } } @@ -77,7 +75,6 @@ namespace Flow.Launcher.Core.Plugin /// public class JsonRPCClientRequestModel : JsonRPCRequestModel { - [JsonPropertyName("dontHideAfterAction")] public bool DontHideAfterAction { get; set; } } diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 65f8fc608..152173749 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -47,8 +47,9 @@ namespace Flow.Launcher.Core.Plugin } } - private static readonly JsonSerializerOptions _options = new() + private static readonly JsonSerializerOptions options = new() { + PropertyNameCaseInsensitive = true, Converters = { new JsonObjectConverter() @@ -60,7 +61,7 @@ namespace Flow.Launcher.Core.Plugin if (output == Stream.Null) return null; var queryResponseModel = await - JsonSerializer.DeserializeAsync(output, _options); + JsonSerializer.DeserializeAsync(output, options); return ParseResults(queryResponseModel); } @@ -70,7 +71,7 @@ namespace Flow.Launcher.Core.Plugin if (string.IsNullOrEmpty(output)) return null; var queryResponseModel = - JsonSerializer.Deserialize(output, _options); + JsonSerializer.Deserialize(output, options); return ParseResults(queryResponseModel); } @@ -110,7 +111,7 @@ namespace Flow.Launcher.Core.Plugin return !result.JsonRPCAction.DontHideAfterAction; } - var jsonRpcRequestModel = JsonSerializer.Deserialize(actionResponse, _options); + var jsonRpcRequestModel = JsonSerializer.Deserialize(actionResponse, options); if (jsonRpcRequestModel?.Method?.StartsWith("Flow.Launcher.") ?? false) { From c7cfd2581773b2fe8a9039532eb57f31115720c6 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 11:47:56 +0800 Subject: [PATCH 2/3] Ignore null value in deserialization to avoid unexpected nullreference issue --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 152173749..431458881 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -50,6 +50,7 @@ namespace Flow.Launcher.Core.Plugin private static readonly JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true, + IgnoreNullValues = true, Converters = { new JsonObjectConverter() @@ -63,6 +64,8 @@ namespace Flow.Launcher.Core.Plugin var queryResponseModel = await JsonSerializer.DeserializeAsync(output, options); + await output.DisposeAsync(); + return ParseResults(queryResponseModel); } From b3c64660535039dd324f9902c2828b0a52d4a2c5 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 11:48:30 +0800 Subject: [PATCH 3/3] Check null and return input value in UserSelectedRecord.cs to avoid error thrown. --- Flow.Launcher/Storage/UserSelectedRecord.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index 8d8956cfa..23d8c4faf 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -28,6 +28,11 @@ namespace Flow.Launcher.Storage private static int GenerateStaticHashCode(string s, int start = HASH_INITIAL) { + if (s == null) + { + return start; + } + unchecked { // skip the empty space @@ -101,4 +106,4 @@ namespace Flow.Launcher.Storage return selectedCount; } } -} +} \ No newline at end of file