mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #466 from Flow-Launcher/fixFlowCallback
Add Customized Object Converter, fix Length issue
This commit is contained in:
commit
a5f5c091be
3 changed files with 53 additions and 6 deletions
|
|
@ -13,6 +13,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
using Flow.Launcher.Core.Resource;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
|
|
@ -108,6 +109,7 @@ namespace Flow.Launcher.Core.Plugin
|
|||
/// </summary>
|
||||
public class JsonRPCClientRequestModel : JsonRPCRequestModel
|
||||
{
|
||||
[JsonPropertyName("dontHideAfterAction")]
|
||||
public bool DontHideAfterAction { get; set; }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using Flow.Launcher.Core.Resource;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
|
@ -45,13 +46,14 @@ namespace Flow.Launcher.Core.Plugin
|
|||
}
|
||||
}
|
||||
|
||||
private static readonly JsonSerializerOptions _options = new() {Converters = {new JsonObjectConverter()}};
|
||||
|
||||
private async Task<List<Result>> DeserializedResultAsync(Stream output)
|
||||
{
|
||||
if (output == Stream.Null) return null;
|
||||
|
||||
JsonRPCQueryResponseModel queryResponseModel = await
|
||||
JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output);
|
||||
var queryResponseModel = await
|
||||
JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, _options);
|
||||
|
||||
return ParseResults(queryResponseModel);
|
||||
}
|
||||
|
|
@ -60,11 +62,12 @@ namespace Flow.Launcher.Core.Plugin
|
|||
{
|
||||
if (string.IsNullOrEmpty(output)) return null;
|
||||
|
||||
JsonRPCQueryResponseModel queryResponseModel =
|
||||
JsonSerializer.Deserialize<JsonRPCQueryResponseModel>(output);
|
||||
var queryResponseModel =
|
||||
JsonSerializer.Deserialize<JsonRPCQueryResponseModel>(output, _options);
|
||||
return ParseResults(queryResponseModel);
|
||||
}
|
||||
|
||||
|
||||
private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
|
|
@ -85,7 +88,7 @@ namespace Flow.Launcher.Core.Plugin
|
|||
{
|
||||
if (result.JsonRPCAction.Method.StartsWith("Flow.Launcher."))
|
||||
{
|
||||
ExecuteFlowLauncherAPI(result.JsonRPCAction.Method.Substring(4),
|
||||
ExecuteFlowLauncherAPI(result.JsonRPCAction.Method[14..],
|
||||
result.JsonRPCAction.Parameters);
|
||||
}
|
||||
else
|
||||
|
|
|
|||
42
Flow.Launcher.Core/Resource/JsonObjectConverter.cs
Normal file
42
Flow.Launcher.Core/Resource/JsonObjectConverter.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Flow.Launcher.Core.Resource
|
||||
{
|
||||
public class JsonObjectConverter : JsonConverter<object>
|
||||
{
|
||||
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonTokenType.True:
|
||||
return true;
|
||||
case JsonTokenType.False:
|
||||
return false;
|
||||
case JsonTokenType.Number when reader.TryGetInt32(out var i):
|
||||
return i;
|
||||
case JsonTokenType.Number when reader.TryGetInt64(out var l):
|
||||
return l;
|
||||
case JsonTokenType.Number:
|
||||
return reader.GetDouble();
|
||||
case JsonTokenType.String when reader.TryGetDateTime(out DateTime datetime):
|
||||
return datetime;
|
||||
case JsonTokenType.String:
|
||||
return reader.GetString();
|
||||
default:
|
||||
// Use JsonElement as fallback.
|
||||
// Newtonsoft uses JArray or JObject.
|
||||
using (var document = JsonDocument.ParseValue(ref reader))
|
||||
{
|
||||
return document.RootElement.Clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
|
||||
{
|
||||
throw new InvalidOperationException("Should not get here.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue