PowerToys/Wox/JsonRPC/JsonPRCModel.cs

78 lines
2.1 KiB
C#
Raw Normal View History

2014-07-07 23:05:06 +08:00
using System.Collections.Generic;
2014-07-10 23:57:08 +08:00
using System.Linq;
2014-07-07 23:05:06 +08:00
using Wox.Plugin;
2014-07-05 23:10:34 +08:00
2014-07-09 23:44:57 +08:00
namespace Wox.JsonRPC
2014-07-05 23:10:34 +08:00
{
2014-07-07 23:05:06 +08:00
public class JsonRPCErrorModel
2014-07-05 23:10:34 +08:00
{
2014-07-07 23:05:06 +08:00
public int Code { get; set; }
2014-07-05 23:10:34 +08:00
2014-07-07 23:05:06 +08:00
public string Message { get; set; }
public string Data { get; set; }
}
public class JsonRPCModelBase
{
public int Id { get; set; }
public string JsonRPC { get; set; }
}
public class JsonRPCResponseModel : JsonRPCModelBase
{
public string Result { get; set; }
public JsonRPCErrorModel Error { get; set; }
2014-07-05 23:10:34 +08:00
}
2014-07-07 23:05:06 +08:00
public class JsonRPCQueryResponseModel : JsonRPCResponseModel
2014-07-05 23:10:34 +08:00
{
2014-07-09 18:15:23 +08:00
public new List<JsonRPCResult> Result { get; set; }
2014-07-07 23:05:06 +08:00
}
public class JsonRPCRequestModel : JsonRPCModelBase
{
public string Method { get; set; }
2014-07-10 23:57:08 +08:00
public object[] Parameters { get; set; }
public bool DontHideAfterAction { get; set; }
2014-07-09 18:15:23 +08:00
public override string ToString()
{
2014-07-10 23:57:08 +08:00
if (Parameters != null && Parameters.Length > 0)
2014-07-09 18:15:23 +08:00
{
2014-07-10 23:57:08 +08:00
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
return string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}}}", Method, parameters);
2014-07-09 18:15:23 +08:00
}
2014-07-10 23:57:08 +08:00
return string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]}}",Method);
}
private string GetParamterByType(object paramter)
{
if (paramter is string)
{
return string.Format(@"\""{0}\""", paramter);
}
if (paramter is int || paramter is float || paramter is double)
{
return string.Format(@"{0}", paramter);
}
if (paramter is bool)
{
return string.Format(@"{0}", paramter.ToString().ToLower());
}
return paramter.ToString();
2014-07-09 18:15:23 +08:00
}
2014-07-07 23:05:06 +08:00
}
public class JsonRPCResult : Result
{
2014-07-09 18:15:23 +08:00
public JsonRPCRequestModel JsonRPCAction { get; set; }
2014-07-05 23:10:34 +08:00
}
}