Ooui-tws-port/Ooui/Message.cs

177 lines
5.2 KiB
C#
Raw Normal View History

2017-06-12 20:19:18 +00:00
using System;
2017-06-13 07:03:01 +00:00
using System.Runtime.Serialization;
2017-06-13 03:31:47 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2017-06-12 20:19:18 +00:00
namespace Ooui
{
public class Message
{
2017-06-13 07:03:01 +00:00
[JsonProperty("m")]
2017-06-12 20:19:18 +00:00
public MessageType MessageType = MessageType.Nop;
2017-06-13 03:31:47 +00:00
[JsonProperty("id")]
2017-06-12 20:19:18 +00:00
public string TargetId = "";
2017-06-13 03:31:47 +00:00
[JsonProperty("k")]
public string Key = "";
2017-06-19 06:42:13 +00:00
[JsonProperty("v", NullValueHandling = NullValueHandling.Ignore)]
2017-06-15 09:39:19 +00:00
public object Value = null;
2017-06-13 07:51:24 +00:00
2017-06-19 07:08:33 +00:00
[JsonProperty("rid", NullValueHandling = NullValueHandling.Ignore)]
public string ResultId = null;
2017-06-16 04:27:15 +00:00
public static Message Call (string targetId, string method, params object[] args) => new Message {
MessageType = MessageType.Call,
TargetId = targetId,
Key = method,
Value = args,
};
2017-06-24 20:34:47 +00:00
public static Message Event (string targetId, string eventType, object value = null) => new Message {
2017-06-15 06:10:58 +00:00
MessageType = MessageType.Event,
TargetId = targetId,
Key = eventType,
2017-06-24 20:34:47 +00:00
Value = value,
2017-06-15 06:10:58 +00:00
};
2018-03-03 07:44:28 +00:00
static void WriteJsonString (System.IO.TextWriter w, string s)
{
w.Write ('\"');
for (var i = 0; i < s.Length; i++) {
var c = s[0];
if (c == '\r') {
w.Write ("\\r");
}
else if (c == '\n') {
w.Write ("\\n");
}
else if (c == '\t') {
w.Write ("\\t");
}
else if (c == '\b') {
w.Write ("\\b");
}
else if (c == '\\') {
w.Write ("\\");
}
else {
w.Write (c);
}
}
w.Write ('\"');
}
static void WriteJsonValue (System.IO.TextWriter w, object value)
{
if (value == null) {
w.Write ("null");
return;
}
var s = value as string;
if (s != null) {
WriteJsonString (w, s);
return;
}
var a = value as Array;
if (a != null) {
w.Write ('[');
var head = "";
foreach (var o in a) {
w.Write (head);
WriteJsonValue (w, o);
head = ",";
}
w.Write (']');
return;
}
var e = value as EventTarget;
if (e != null) {
w.Write ('\"');
w.Write (e.Id);
w.Write ('\"');
return;
}
if (value is Color) {
WriteJsonString (w, ((Color)value).ToString ());
return;
}
var icult = System.Globalization.CultureInfo.InvariantCulture;
if (value is double) {
w.Write (((double)value).ToString (icult));
}
if (value is int) {
w.Write (((int)value).ToString (icult));
}
if (value is float) {
w.Write (((float)value).ToString (icult));
}
WriteJsonString (w, Convert.ToString (value, icult));
}
public void WriteJson (System.IO.TextWriter w)
{
w.Write ('{');
switch (MessageType) {
case MessageType.Call: w.Write ("\"m\":\"call\",\"id\":\""); break;
case MessageType.Create: w.Write ("\"m\":\"create\",\"id\":\""); break;
case MessageType.Event: w.Write ("\"m\":\"event\",\"id\":\""); break;
case MessageType.Listen: w.Write ("\"m\":\"listen\",\"id\":\""); break;
case MessageType.Nop: w.Write ("\"m\":\"nop\",\"id\":\""); break;
case MessageType.RemoveAttribute: w.Write ("\"m\":\"remAttr\",\"id\":\""); break;
case MessageType.Set: w.Write ("\"m\":\"set\",\"id\":\""); break;
case MessageType.SetAttribute: w.Write ("\"m\":\"setAttr\",\"id\":\""); break;
}
w.Write (TargetId);
w.Write ("\",\"k\":\"");
w.Write (Key);
if (Value != null) {
w.Write ("\",\"v\":");
WriteJsonValue (w, Value);
w.Write ('}');
}
else {
w.Write ("\"}");
}
}
public string ToJson ()
{
using (var sw = new System.IO.StringWriter ()) {
WriteJson (sw);
return sw.ToString ();
}
}
2017-06-12 20:19:18 +00:00
}
2017-06-13 07:03:01 +00:00
[JsonConverter (typeof (StringEnumConverter))]
2017-06-12 20:19:18 +00:00
public enum MessageType
{
2017-06-13 07:03:01 +00:00
[EnumMember(Value = "nop")]
2017-06-12 20:19:18 +00:00
Nop,
2017-06-13 07:03:01 +00:00
[EnumMember(Value = "create")]
2017-06-12 20:19:18 +00:00
Create,
2017-06-13 07:03:01 +00:00
[EnumMember(Value = "set")]
2017-06-12 20:45:27 +00:00
Set,
2017-11-26 17:28:06 +00:00
[EnumMember (Value = "setAttr")]
SetAttribute,
2018-02-02 02:43:23 +00:00
[EnumMember(Value = "remAttr")]
RemoveAttribute,
2017-06-13 07:03:01 +00:00
[EnumMember(Value = "call")]
2017-06-12 20:45:27 +00:00
Call,
2017-06-15 06:10:58 +00:00
[EnumMember(Value = "listen")]
Listen,
[EnumMember(Value = "event")]
Event,
2017-06-12 20:19:18 +00:00
}
}