1-
2- /* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Flow Launcher and other plugins,
1+ /* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Flow Launcher and other plugins,
32 * like python or other self-execute program. But, we added addtional infos (proxy and so on) into rpc request. Also, we didn't use the
43 * "id" and "jsonrpc" in the request, since it's not so useful in our request model.
54 *
1312 *
1413 */
1514
15+ using Flow . Launcher . Core . Resource ;
1616using System . Collections . Generic ;
1717using System . Linq ;
1818using System . Text . Json . Serialization ;
1919using Flow . Launcher . Plugin ;
20+ using System . Text . Json ;
2021
2122namespace Flow . Launcher . Core . Plugin
2223{
@@ -29,12 +30,8 @@ public class JsonRPCErrorModel
2930 public string Data { get ; set ; }
3031 }
3132
32- public class JsonRPCModelBase
33- {
34- public int Id { get ; set ; }
35- }
3633
37- public class JsonRPCResponseModel : JsonRPCModelBase
34+ public class JsonRPCResponseModel
3835 {
3936 public string Result { get ; set ; }
4037
@@ -48,45 +45,20 @@ public class JsonRPCQueryResponseModel : JsonRPCResponseModel
4845
4946 public string DebugMessage { get ; set ; }
5047 }
51-
52- public class JsonRPCRequestModel : JsonRPCModelBase
48+
49+ public class JsonRPCRequestModel
5350 {
5451 public string Method { get ; set ; }
5552
5653 public object [ ] Parameters { get ; set ; }
5754
58- public override string ToString ( )
59- {
60- string rpc = string . Empty ;
61- if ( Parameters != null && Parameters . Length > 0 )
62- {
63- string parameters = $ "[{ string . Join ( ',' , Parameters . Select ( GetParameterByType ) ) } ]";
64- rpc = $@ "{{\""method\"":\""{ Method } \"",\""parameters\"":{ parameters } ";
65- }
66- else
67- {
68- rpc = $@ "{{\""method\"":\""{ Method } \"",\""parameters\"":[]";
69- }
70-
71- return rpc ;
72-
73- }
74-
75- private string GetParameterByType ( object parameter )
76- => parameter switch
55+ private static readonly JsonSerializerOptions options = new ( )
7756 {
78- null => "null" ,
79- string _ => $@ "\""{ ReplaceEscapes ( parameter . ToString ( ) ) } \""",
80- bool _ => $@ "{ parameter . ToString ( ) . ToLower ( ) } ",
81- _ => parameter . ToString ( )
57+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase
8258 } ;
83-
84-
85- private string ReplaceEscapes ( string str )
59+ public override string ToString ( )
8660 {
87- return str . Replace ( @"\" , @"\\" ) //Escapes in ProcessStartInfo
88- . Replace ( @"\" , @"\\" ) //Escapes itself when passed to client
89- . Replace ( @"""" , @"\\""""" ) ;
61+ return JsonSerializer . Serialize ( this , options ) ;
9062 }
9163 }
9264
@@ -95,11 +67,7 @@ private string ReplaceEscapes(string str)
9567 /// </summary>
9668 public class JsonRPCServerRequestModel : JsonRPCRequestModel
9769 {
98- public override string ToString ( )
99- {
100- string rpc = base . ToString ( ) ;
101- return rpc + "}" ;
102- }
70+
10371 }
10472
10573 /// <summary>
@@ -108,12 +76,6 @@ public override string ToString()
10876 public class JsonRPCClientRequestModel : JsonRPCRequestModel
10977 {
11078 public bool DontHideAfterAction { get ; set ; }
111-
112- public override string ToString ( )
113- {
114- string rpc = base . ToString ( ) ;
115- return rpc + "}" ;
116- }
11779 }
11880
11981 /// <summary>
@@ -125,4 +87,4 @@ public class JsonRPCResult : Result
12587 {
12688 public JsonRPCClientRequestModel JsonRPCAction { get ; set ; }
12789 }
128- }
90+ }
0 commit comments