public ReceiveMessage FromJson(string json)
        {
            dynamic jsonObj = new JsonReader().Read(json);

            if (jsonObj == null)
            {
                throw new ParamParserException("invalid json object", null);
            }
            var fromUser   = jsonObj.fromUser;
            var createTime = jsonObj.createTime;
            var packageId  = jsonObj.packageId;
            var msgType    = jsonObj.msgType;

            if (fromUser is string &&
                (createTime is int || createTime is long) &&
                packageId is string &&
                msgType is string)
            {
                string type = msgType;
                switch (type)
                {
                case MessageTypeText:
                    m_msgBody = new TextBody().FromJsonObject(jsonObj.text);
                    break;

                case MessageTypeImage:
                    m_msgBody = new ImageBody().FromJsonObject(jsonObj.image);
                    break;

                case MessageTypeFile:
                    m_msgBody = new FileBody().FromJsonObject(jsonObj.file);
                    break;

                default:
                    throw new ParamParserException(String.Format("unknown message type {0}", type), null);
                }
                m_fromUser   = Convert.ToString(fromUser);
                m_createTime = Convert.ToInt64(createTime);
                m_packageId  = Convert.ToString(packageId);
                m_msgType    = type;
                return(this);
            }
            throw new ParamParserException(String.Format("invalid json content: {0}", jsonObj.ToString()), null);
        }
Beispiel #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="toUser">发送目标用户,格式为"user1 | user2 | user3"</param>
 /// <param name="msgType">消息类型</param>
 /// <param name="msgBody">消息体</param>
 public Message(string toUser, string msgType, MessageBody msgBody)
 {
     m_toUser  = toUser != null ? toUser : "";
     m_msgType = msgType != null ? msgType : "";
     m_msgBody = msgBody;
 }