using System.Collections.Generic; using Newtonsoft.Json; using LeanCloud.Storage.Internal.Codec; using LeanCloud.Storage.Internal; namespace LeanCloud.Realtime { public abstract class LCIMTypedMessage : LCIMMessage { private Dictionary customProperties; internal virtual int MessageType { get; private set; } public object this[string key] { get { if (customProperties == null) { return null; } return customProperties[key]; } set { if (customProperties == null) { customProperties = new Dictionary(); } customProperties[key] = value; } } protected LCIMTypedMessage() { } internal virtual Dictionary Encode() { Dictionary msgData = new Dictionary { { "_lctype", MessageType } }; if (customProperties != null && customProperties.Count > 0) { msgData["_lcattrs"] = LCEncoder.Encode(customProperties); } return msgData; } protected virtual void DecodeMessageData(Dictionary msgData) { MessageType = (int)msgData["_lctype"]; if (msgData.TryGetValue("_lcattrs", out object attrObj)) { customProperties = LCDecoder.Decode(attrObj) as Dictionary; } } internal static LCIMTypedMessage Deserialize(string json) { Dictionary msgData = JsonConvert.DeserializeObject>(json, new LCJsonConverter()); LCIMTypedMessage message = null; int msgType = (int)msgData["_lctype"]; switch (msgType) { case -1: message = new LCIMTextMessage(); break; case -2: message = new LCIMImageMessage(); break; case -3: message = new LCIMAudioMessage(); break; case -4: message = new LCIMVideoMessage(); break; case -5: message = new LCIMLocationMessage(); break; case -6: message = new LCIMFileMessage(); break; default: // TODO 用户自定义类型消息 break; } message.DecodeMessageData(msgData); return message; } } }